0

我正在开发第一个 android 应用程序,并且我有一个扩展的片段类SherlockListFragment。当我设置setOnItemClickListeneronCreateView实施onItemClick时,当我单击行(列表)时没有得到任何响应。列表在我的应用程序中正确显示。我希望当用户单击行表单时,listview我的另一个活动将调用。我看了很多,但没有得到正确的答案。我不明白问题出在哪里。谁能告诉我我在下面的代码中做错了什么?

以下是我的代码

public class ResidentialFragActivity extends SherlockListFragment {

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle saved) {

    View view = inflater.inflate(R.layout.project_list,container, false);
    resListviewId = (ListView)view.findViewById(android.R.id.list);
    linearProgress = (LinearLayout)view.findViewById(R.id.linlaHeaderProgress);
    projectList = new ArrayList<HashMap<String,String>>();

    new LoadProjects().execute();
    resListviewId.setOnItemClickListener(new android.widget.AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> arg0, View view, int arg2,
                long arg3) {
            Intent href = new Intent(getSherlockActivity(), ProjectDetailActivity.class);

            // send project id to project detail activity to get list of Phases under that project
            String proj_id = ((TextView) view.findViewById(R.id.projectId)).getText().toString();

            href.putExtra("proj_id", proj_id);     
            getSherlockActivity().startActivity(href);
        }
    });
    return view;
}


//inner class for network operation
private class LoadProjects extends AsyncTask<String, String, String> {

    protected void onPreExecute() {
        linearProgress.setVisibility(View.VISIBLE);
    }

    @Override
    protected String doInBackground(String... params) {

        //call webservice
    }

    @Override
    protected void onPostExecute(String result) {
        linearProgress.setVisibility(View.GONE);

        ListAdapter projAdapter = new SimpleAdapter(getSherlockActivity(),
                projectList, R.layout.project_list_item, 
                new String[] {PROJ_ID,PROJ_NAME,EDIT_RIGHTS}, new int[] 
                        {R.id.projectId,R.id.projectName,R.id.projectSubName});
        //updating the UI
        setListAdapter(projAdapter);
    }
        }
}

以下是列表视图的布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_marginTop="10dp"
    android:background="@color/White"
    android:orientation="vertical" >

    <ListView
        android:id="@android:id/list"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="1dp"
        android:divider="@drawable/divider"
        android:focusableInTouchMode="true"     
        android:smoothScrollbar="true"
        android:dividerHeight="1.5dp"
        android:scrollbarThumbVertical="@drawable/divider" 
        android:drawSelectorOnTop="false"
        android:listSelector="@drawable/list_selector">
    </ListView>

</LinearLayout>

提前致谢..

4

1 回答 1

1

您应该覆盖onListItemClick而不是resListviewId.setOnItemClickListener.. 因为您将适配器设置为默认值 setListAdapter(projAdapter);

@Override
public void onListItemClick(ListView l, View v, int position, long id) {
        Intent href = new Intent(getSherlockActivity(), ProjectDetailActivity.class);

        // send project id to project detail activity to get list of Phases under that project
        String proj_id = ((TextView) view.findViewById(R.id.projectId)).getText().toString();

        href.putExtra("proj_id", proj_id);     
        startActivity(href);
}
于 2013-05-06T09:50:06.540 回答