'Custom Adapter Not showing data
I have developed an custom adapter which takes data from SQLite. Data is selecting and adapter also gets data. But its not showing in the list view. I don't get any errors. I printed data get in the adapter but only one set of data is shown. Not all data.
I have developed and follow normal custom adapter implementation example.
I have some doubts.
1-In my customAdapter class. when i am inflating XML for the custom rows in adapter when findviewById() is called only one Text View shows 'it may produce null pointer Exception'.Why ? my xml for list view row.
<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>
In my fragment class also above warning shows for inflated list view element. my fragments xml is given below.
<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" />
my Adapter code
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 code is
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;
}
}
Solution 1:[1]
You can simple implement onCreateView() for Fragment.
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;
}
And in your Adapter class change here also
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()+"");
}
Remove all the code from onActivityCreated() method.
Solution 2:[2]
Try this way:
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;
}
And remove all the code from onActivityCreated(....)
and give me feedback on this.
Solution 3:[3]
At the bottom of your adapter you could be missing this
@Override
public boolean isViewFromObject(@NonNull View view, @NonNull Object o) {
return view == o;
}
@Override
public void destroyItem(@NonNull ViewGroup container, int position, @NonNull Object object) {
container.removeView((View)object);
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | |
Solution 2 | M D |
Solution 3 | Kipruto |