3

我在我的应用程序中使用 ListFragment 并且我想这样做,以便列表中的每个项目都有一个 OnItemClick 侦听器,但是当我添加时ListView lv = getListView();出现错误。如果我删除了列表视图的工作但是我的应用程序将没有我需要的 onclick 侦听器。

下面是我的代码

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    // TODO Auto-generated method stub

    ArrayList<HashMap<String, String>> menuItems = new ArrayList<HashMap<String, String>>();

//...       

    ListAdapter adapter = new SimpleAdapter(getActivity(), menuItems,
            R.layout.list_item,
            new String[] { KEY_NAME, KEY_DESC, KEY_COST }, new int[] {
                    R.id.name, R.id.desciption, R.id.cost });

    setListAdapter(adapter);

    ListView lv = getListView();
//HERE IS WHERE I AM GETTING THE ERROR

    lv.setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> parent, View view,
                int position, long id) {
            // getting values from selected ListItem
            String name = ((TextView) view.findViewById(R.id.name)).getText().toString();
            String cost = ((TextView) view.findViewById(R.id.cost)).getText().toString();
            String description = ((TextView) view.findViewById(R.id.desciption)).getText().toString();

            // Starting new intent
            Intent in = new Intent(getActivity().getApplicationContext(), SingleMenuItemActivity.class);
            in.putExtra(KEY_NAME, name);
            in.putExtra(KEY_COST, cost);
            in.putExtra(KEY_DESC, description);
            startActivity(in);

        }
    });

    return super.onCreateView(inflater, container, savedInstanceState);
}

Logcat 在下面

08-01 23:24:47.152: E/AndroidRuntime(18883): FATAL EXCEPTION: main
08-01 23:24:47.152: E/AndroidRuntime(18883): java.lang.IllegalStateException: Content view not yet created
08-01 23:24:47.152: E/AndroidRuntime(18883):    at android.support.v4.app.ListFragment.ensureList(ListFragment.java:328)
08-01 23:24:47.152: E/AndroidRuntime(18883):    at android.support.v4.app.ListFragment.getListView(ListFragment.java:222)
08-01 23:24:47.152: E/AndroidRuntime(18883):    at com.OptimusApps.stayhealthy.AndroidXMLParsingActivity.onCreateView(AndroidXMLParsingActivity.java:74)
08-01 23:24:47.152: E/AndroidRuntime(18883):    at android.support.v4.app.Fragment.performCreateView(Fragment.java:1470)
08-01 23:24:47.152: E/AndroidRuntime(18883):    at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:925)
08-01 23:24:47.152: E/AndroidRuntime(18883):    at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1102)
08-01 23:24:47.152: E/AndroidRuntime(18883):    at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
08-01 23:24:47.152: E/AndroidRuntime(18883):    at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1458)
08-01 23:24:47.152: E/AndroidRuntime(18883):    at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:438)
08-01 23:24:47.152: E/AndroidRuntime(18883):    at android.os.Handler.handleCallback(Handler.java:605)
08-01 23:24:47.152: E/AndroidRuntime(18883):    at android.os.Handler.dispatchMessage(Handler.java:92)
08-01 23:24:47.152: E/AndroidRuntime(18883):    at android.os.Looper.loop(Looper.java:137)
08-01 23:24:47.152: E/AndroidRuntime(18883):    at android.app.ActivityThread.main(ActivityThread.java:4575)
08-01 23:24:47.152: E/AndroidRuntime(18883):    at java.lang.reflect.Method.invokeNative(Native Method)
08-01 23:24:47.152: E/AndroidRuntime(18883):    at java.lang.reflect.Method.invoke(Method.java:511)
08-01 23:24:47.152: E/AndroidRuntime(18883):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789)
08-01 23:24:47.152: E/AndroidRuntime(18883):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:556)
08-01 23:24:47.152: E/AndroidRuntime(18883):    at dalvik.system.NativeStart.main(Native Method)

谢谢

4

3 回答 3

9

只需将您的代码放入 onActivityCreated() 并使用简单的 getListView() 方法从那里管理您的列表视图..

于 2013-08-02T07:09:11.880 回答
1

您需要先膨胀视图,然后才能使用它。

@Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        v = inflater.inflate(R.layout.list, container, false);
            // Set adapter etc

        return v;
    }

布局列表.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingLeft="2dp"
    android:paddingRight="2dp" >

    <ListView
        android:id="@id/android:list"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:drawSelectorOnTop="false" />

    <ProgressBar
        android:id="@id/android:empty"
        style="?android:attr/progressBarStyleLarge"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
         />

</RelativeLayout>

使用onListItemClick

@Override
public void onListItemClick(ListView l, View v, int position, long id) {
  //Responding to clicks
}
于 2013-08-02T03:45:17.430 回答
0

您直接调用

getListView().setOnItemClickListener(new OnItemClickListener() {

    @Override
    public void onItemClick(AdapterView<?> parent, View view,
            int position, long id) {
        // getting values from selected ListItem
        String name = ((TextView) view.findViewById(R.id.name)).getText().toString();
        String cost = ((TextView) view.findViewById(R.id.cost)).getText().toString();
        String description = ((TextView) view.findViewById(R.id.desciption)).getText().toString();

        // Starting new intent
        Intent in = new Intent(getActivity().getApplicationContext(), SingleMenuItemActivity.class);
        in.putExtra(KEY_NAME, name);
        in.putExtra(KEY_COST, cost);
        in.putExtra(KEY_DESC, description);
        startActivity(in);

    }
});
于 2013-08-02T04:05:01.200 回答