使用片段按下按钮时如何创建列表视图

时间:2014-10-15 12:12:55

标签: android list listview android-fragments fragment

在我的应用程序中有一个导航抽屉。当在抽屉中单​​击任何项​​目时,将显示文本视图。我是用碎片做的。它工作正常,直到这一点。 现在我想在导航抽屉中单击第二个项目时显示一个列表。但它一直在崩溃应用程序。该列表是使用自定义适配器自定义的,并使用列表片段创建。

MainActivity.java中的

private class DrawerItemClickListener implements ListView.OnItemClickListener
{
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id)
    {
        selectItem(position);

selectItem方法

private void selectItem(int position)
{
    int pos = position;
    Fragment fragment = null;
    switch (pos) {
    case 0: 
        fragment = new FragmentSell();
        break;

    case 1: 
        fragment = new FragmentBuy();
        break;
    }
    FragmentManager fragmentManager = getFragmentManager();
    fragmentManager.beginTransaction().replace(R.id.content_frame, fragment).commit();

适配器

public class FragmentBuyAdapter extends ArrayAdapter<ItemBuy> {

private ArrayList<ItemBuy> objects;
public ArrayAdapter<Integer> adapter_weight;
public String weight;
public Context context;

public FragmentBuyAdapter(Context context, int textViewResourceId,
        ArrayList<ItemBuy> objects) {
    super(context, textViewResourceId, objects);
    this.objects = objects;
    this.context = context;
}

public View getView(int position, View convertView, ViewGroup parent) {

    View v = convertView;

    if (v == null) {
        LayoutInflater inflater = (LayoutInflater) getContext()
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        v = inflater.inflate(R.layout.fragment_buy_list_item, null);
    }

    ItemBuy i = objects.get(position);

    if (i != null) {

        TextView name = (TextView) v.findViewById(R.id.tv_shop_name);
        TextView desc = (TextView) v.findViewById(R.id.tv_shop_description);

            if (name != null) {
                name.setText(i.getName());
            }
            if (desc != null) {
                desc.setText(i.getDesc());
            }
        }

        return v;
    }
}

的logcat -

10-15 11:37:05.860: W/dalvikvm(973): threadid=1: thread exiting with uncaught exception group=0xb4aebb90)
10-15 11:37:05.890: E/AndroidRuntime(973): FATAL EXCEPTION: main
10-15 11:37:05.890: E/AndroidRuntime(973): Process: com.coach.zudash, PID: 973
10-15 11:37:05.890: E/AndroidRuntime(973): java.lang.RuntimeException: Content has view with id attribute 'android.R.id.list' that is not a ListView class
10-15 11:37:05.890: E/AndroidRuntime(973):  at android.app.ListFragment.ensureList(ListFragment.java:402)
10-15 11:37:05.890: E/AndroidRuntime(973):  at android.app.ListFragment.onViewCreated(ListFragment.java:203)
10-15 11:37:05.890: E/AndroidRuntime(973):  at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:904)
10-15 11:37:05.890: E/AndroidRuntime(973):  at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1062)
10-15 11:37:05.890: E/AndroidRuntime(973):  at android.app.BackStackRecord.run(BackStackRecord.java:684)
10-15 11:37:05.890: E/AndroidRuntime(973):  at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1447)
10-15 11:37:05.890: E/AndroidRuntime(973):  at android.app.FragmentManagerImpl$1.run(FragmentManager.java:443)
10-15 11:37:05.890: E/AndroidRuntime(973):  at android.os.Handler.handleCallback(Handler.java:733)
10-15 11:37:05.890: E/AndroidRuntime(973):  at android.os.Handler.dispatchMessage(Handler.java:95)
10-15 11:37:05.890: E/AndroidRuntime(973):  at android.os.Looper.loop(Looper.java:137)
10-15 11:37:05.890: E/AndroidRuntime(973):  at android.app.ActivityThread.main(ActivityThread.java:4998)
10-15 11:37:05.890: E/AndroidRuntime(973):  at java.lang.reflect.Method.invokeNative(Native Method)
10-15 11:37:05.890: E/AndroidRuntime(973):  at java.lang.reflect.Method.invoke(Method.java:515)
10-15 11:37:05.890: E/AndroidRuntime(973):  at     com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:777)
10-15 11:37:05.890: E/AndroidRuntime(973):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:593)
10-15 11:37:05.890: E/AndroidRuntime(973):  at dalvik.system.NativeStart.main(Native Method)

2 个答案:

答案 0 :(得分:0)

您的视图持有者模式实现已经过了。

试试此链接。 http://www.codeofaninja.com/2013/09/android-viewholder-pattern-example.html

应用视图持有者模式将解决您的问题,并使列表滚动更顺畅。 你的getView方法有滞后,因为你发现每行的textView引用。这些都可以通过查看Holder模式来克服

答案 1 :(得分:0)

哦,我明白了...... 我没有在我的布局(xml文件)中使用列表视图的默认ID .. 它应该是android:id =&#34; @ + id / android:list&#34;,

感谢每个人的时间和帮助...