自定义适配器不显示数据

时间:2014-03-11 05:31:15

标签: android xml fragment custom-adapter

我开发了一个从SQLite获取数据的自定义适配器。数据正在选择,适配器也获取数据。但它没有显示在列表视图中。我没有得到任何错误。我在适配器中打印了数据,但只显示了一组数据。并非所有数据。

我已经开发并遵循正常的自定义适配器实现示例。

我有些疑惑。

1 - 在我的customAdapter类中。当我在调用findviewById()时为适配器中的自定义行充气XML时,只有一个文本视图显示'它可能会产生空指针异常'。为什么?我的xml列表视图行。

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                android:orientation="vertical"
                android:paddingTop="4dip"
                android:paddingBottom="6dip"
                android:paddingLeft="10dip"
                android:paddingRight="10dip"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:weightSum="1">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/row_item_name"
        android:textColor="@color/item_name_color"
        android:layout_alignParentLeft="true"
        />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/row_item_price"

        android:textColor="@color/item_price_color"
        android:layout_alignParentTop="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/row_item_contains"

        android:layout_gravity="left"
        android:layout_below="@id/row_item_name"
        android:layout_toRightOf="@id/row_item_name" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"

        android:id="@+id/row_item_quantity"
        android:layout_above="@id/row_item_contains"
        android:paddingRight="10dip"
        android:layout_toLeftOf="@id/row_item_price" />


</RelativeLayout>
  1. 在我的片段类中,上面的警告显示了膨胀列表视图元素。 我的片段xml如下所示。

    <TextView
        android:id="@+id/tableTextview"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="left"
        android:text="@string/table"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true" />
    
    <TextView
        android:id="@+id/userName"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentEnd="true"
        android:layout_alignParentRight="true"
        android:layout_gravity="right"
        android:textColor="@color/bg" />
    
    
    
    <EditText
        android:paddingTop="6dp"
        android:id="@+id/tableNumber"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:inputType="number"
        android:maxWidth="@dimen/tableNoSize"
        android:layout_below="@id/tableTextview"
        android:layout_alignRight="@id/tableTextview"
        android:layout_alignEnd="@id/tableTextview" />
    
    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@id/tableNumber"
        android:layout_alignStart="@id/tableNumber"
        android:layout_below="@id/tableNumber"
        android:layout_gravity="center"
        android:paddingTop="6dip"
        android:paddingBottom="4dip"
        android:text="@string/Menu" />
    
    <ListView
        android:layout_below="@id/textView"
        android:id="@+id/bearer_menu_order_list"
        android:layout_width="wrap_content"
        android:layout_height="0dp">
    </ListView>
    
    <Button
        android:id="@+id/placeButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/textView"
        android:layout_gravity="center_horizontal"
        android:background="@color/buttonColor"
        android:text="@string/placeOrder" />
    

  2. 我的适配器代码

        public class MenuListAdapter extends BaseAdapter {
        private List<MenuData> list;
        private Context context;
    
        public MenuListAdapter(Context context, List<MenuData> list) {
            this.context = context;
            this.list = list;
            Log.e("Hee",list.size()+"");
        }
        @Override
        public int getCount() {
            return list.size();
        }
    
        @Override
        public Object getItem(int position) {
            return list.get(position);
        }
    
        @Override
        public long getItemId(int position) {
            return position;
        }
    
        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
    
            //Log.e("list got--",""+list.size());
            Log.e("position ",""+position);
    
            ViewHolder holder;
            LayoutInflater inflater =(LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    
            if (convertView == null){
                convertView = inflater.inflate(R.layout.row_list_create_new,null);
                holder = new ViewHolder();
                try{
                    holder.txtName=(TextView)convertView.findViewById(R.id.row_item_name);
                    holder.txtContains=(TextView)convertView.findViewById(R.id.row_item_contains);
                    holder.txtPrice=(TextView)convertView.findViewById(R.id.row_item_price);
                    holder.txtQuantity=(TextView)convertView.findViewById(R.id.row_item_quantity);
                    convertView.setTag(holder);
                    Log.e("covertView-","Null");
                }
                catch (NullPointerException e){
                    e.printStackTrace();
                }
    
            }
            else {
                Log.e("covertView-","Not Null");
                holder = (ViewHolder) convertView.getTag();
            }
    
            MenuData currentRow=list.get(position);
            Log.e("item ",currentRow.getKEY_ITEM_NAME());
            holder.txtName.setText(currentRow.getKEY_ITEM_NAME());
            holder.txtContains.setText(currentRow.getKEY_ITEM_CONTENTS());
            holder.txtPrice.setText(currentRow.getKEY_PRICE());
    
            return convertView;
        }
    
    
        class ViewHolder {
            public TextView txtName;
            public TextView txtPrice;
            public TextView txtContains;
            public TextView txtQuantity;
    
        }
    
    }
    

    片段代码是

     public class CreateOrderFragment extends Fragment {
    
        List<MenuData> listMenu;
        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                                 Bundle savedInstanceState) {
            View rootView;
            listMenu=new ArrayList<MenuData>();
            rootView = inflater.inflate(R.layout.fragment_create_new_order_list_sections, container, false);
            Context context=getActivity();
            //View view=getView();
            ListView menuList;
            menuList=(ListView) rootView.findViewById(R.id.bearer_menu_order_list);
            menuList.setVisibility(View.VISIBLE);
            DatabaseHelper helper=new DatabaseHelper(context);
            listMenu = helper.getMenuItem();
            Log.e("list menu--",listMenu.size()+"");
            MenuListAdapter adapter=new MenuListAdapter(context,listMenu);
            Log.e("list adapter--",""+adapter.getCount());
            menuList.setAdapter(adapter);
            menuList.setVisibility(View.VISIBLE);
    
            return rootView;
        }
    
    
    
        @Override
        public void onCreate(Bundle savedInstanceState) {
    
            super.onCreate(savedInstanceState);
    
            String user;
            TextView tv;
    }
    }
    

2 个答案:

答案 0 :(得分:4)

尝试这种方式:

 ListView menuList;
 public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    View rootView;
    rootView = inflater.inflate(R.layout.fragment_create_new_order_list_sections, container, false);

    menuList=(ListView) rootView.findViewById(R.id.bearer_menu_order_list);
    DatabaseHelper helper=new DatabaseHelper(getActivity());
    listMenu = helper.getMenuItem();
    Log.e("list menu--",listMenu.size()+"");
    MenuListAdapter adapter=new MenuListAdapter(getActivity(),listMenu);
    Log.e("list adapter--",""+adapter.getCount());
    menuList.setAdapter(adapter);
    menuList.setVisibility(View.VISIBLE);

    return rootView;
}

并移除onActivityCreated(....)中的所有代码并向我提供反馈。

答案 1 :(得分:3)

您可以简单地为片段实现onCreateView()。

 ListView menuList;

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

  // Inflate the layout for this fragment

    View rootView;
    rootView = inflater.inflate(R.layout.fragment_create_new_order_list_sections, container, false); 
    menuList=(ListView) rootView.findViewById(R.id.bearer_menu_order_list);
    DatabaseHelper helper=new DatabaseHelper(getActivity());
    listMenu = helper.getMenuItem();
    Log.e("list menu--",listMenu.size()+"");
    MenuListAdapter adapter=new MenuListAdapter(getActivity(),listMenu);
    Log.e("list adapter--",""+adapter.getCount());
    menuList.setAdapter(adapter);
    menuList.setVisibility(View.VISIBLE);
   return rootView;
 }

在你的Adapter类中也改变了

 public class MenuListAdapter extends BaseAdapter {
 private List<MenuData> list;
 private Context context;

 public MenuListAdapter(Context context, List<MenuData> list) {
    this.context = context;
    this.list = list;
    Log.e("Hee",list.size()+"");
 }

从onActivityCreated()方法中删除所有代码。