gpt4 book ai didi

android - 自定义适配器不显示数据

转载 作者:数据小太阳 更新时间:2023-10-29 02:51:23 27 4
gpt4 key购买 nike

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

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

我有些疑惑。

1-在我的 customAdapter 类中。当我在调用 findviewById() 时为适配器中的自定义行膨胀 XML 时,只有一个 TextView 显示“它可能产生空指针异常”。为什么?我的 ListView 行的 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. 在我的 fragment 类中,上面还显示了膨胀 ListView 元素的警告。下面给出了我的 fragment 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" />

我的适配器代码

    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;

}

}

fragment 代码为

 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;
}
}

最佳答案

试试这个方法:

 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(....) 中删除所有代码并给我反馈。

关于android - 自定义适配器不显示数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22317195/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com