2

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>
  1. 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;
}
}
Top Cat
  • 2,473
  • 3
  • 22
  • 34
  • Post your Adapter code then. – M D Mar 11 '14 at 05:32
  • Please post the relevant part of the code sa well. – Szymon Mar 11 '14 at 05:32
  • now post your Fragment code – M D Mar 11 '14 at 05:35
  • Where is code of binding adapter into your ListView ? – GrIsHu Mar 11 '14 at 05:36
  • menuList.setAdapter(adapter); – Top Cat Mar 11 '14 at 05:37
  • Where you have added the data into your ArrayList ? What does your `helper.getMenuItem();` contains ? – GrIsHu Mar 11 '14 at 05:39
  • listMenu = helper.getMenuItem(); // function to get data from database which returns list menudata is a bean class to hold data – Top Cat Mar 11 '14 at 05:41
  • I suggest you to keep the code which you have written in your `onActivityCreated` into the `onCreateView` method. As the `onActivityCreated` always gets called before `onCreateView`. – GrIsHu Mar 11 '14 at 05:41
  • and also if you are working with the same code then you need to `Extends` your `Fragment` to `ListFragment` – M D Mar 11 '14 at 05:43
  • @GrIsHu ur comment is right. – Piyush Mar 11 '14 at 05:44
  • @GrIsHu is fragment class have onCreateView method?? – Top Cat Mar 11 '14 at 05:45
  • @Rashid yes it has.... – Piyush Mar 11 '14 at 05:45
  • @Rashid The `Fragments` `onCreateView` method is similar to the method of `onCreate` method of `Activity`. Which is used to inflate the layout. So the thing is happening is like as you have written all the code in `onActivityCreate`method and then its inflating your layout. So you must have kept `ListView` default invisible in your layout file. That is why its not showing you listview. – GrIsHu Mar 11 '14 at 05:46
  • What do you mean by "only one set of data is shown"? Are you _certain_ that all of the expected data is being retrieved from the db, and that the Adapter is getting it? – Mike M. Mar 11 '14 at 05:46
  • its showing error for me. thats why i ask so. – Top Cat Mar 11 '14 at 05:49
  • @Rashid You just write all your code of `onActivityCreate` method and `onCreate` method into the `onCreateView` method. And then check – GrIsHu Mar 11 '14 at 05:49
  • What is the error you're receiving? – Mike M. Mar 11 '14 at 05:50
  • @MikeM. listMenu = helper.getMenuItem(); gets the all values. also adapter is getting it. – Top Cat Mar 11 '14 at 05:51

3 Answers3

4

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.

M D
  • 47,665
  • 9
  • 93
  • 114
3

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.

Piyush
  • 18,895
  • 5
  • 32
  • 63
  • I got error!!- java.lang.NullPointerException at artha.ordermaking.KOT.CreateOrderFragment.onCreateView(CreateOrderFragment.java:36) – Top Cat Mar 11 '14 at 05:58
  • At which line you got Error? Show me that one. – Piyush Mar 11 '14 at 05:58
  • ListView menuList=(ListView) view.findViewById(R.id.bearer_menu_order_list); in this line got null pointer exception – Top Cat Mar 11 '14 at 06:01
  • 1
    @Rashid just see my answer properly.. here (ListView) view.findViewById(R.id.bearer_menu_order_list) not view its rootView. so change (ListView) rootView.findViewById(R.id.bearer_menu_order_list) – Piyush Mar 11 '14 at 06:02
  • 1
    @Rashid Refer your `Listview` in context of `rootView` not `view` as `ListView menuList=(ListView) rootView.findViewById(R.id.bearer_menu_order_list);` – GrIsHu Mar 11 '14 at 06:02
  • @PiyushGupta +1 from my side ;) – GrIsHu Mar 11 '14 at 06:04
  • @Rashid Just see from this **Log.e("list menu--",listMenu.size()+"");** have you got proper size when you r retrieving data from database? – Piyush Mar 11 '14 at 06:07
  • E/list menu--﹕ 5 ya.. I got all 5 values from db. – Top Cat Mar 11 '14 at 06:10
  • 1
    @Rashid Do one thing move this line **menuList.setVisibility(View.VISIBLE);** to after this line **menuList=(ListView) rootView.findViewById(R.id.bearer_menu_order_list);** – Piyush Mar 11 '14 at 06:13
  • @Rashid Update your question with the changes which you have made in your code. – GrIsHu Mar 11 '14 at 06:13
  • @GrIsHu Kem hamna thi on9 nathi hoti? – Piyush Mar 11 '14 at 06:13
  • 1
    @Rashid Just check when you are fetching data from database if is it getting same data or problem is in your adaper? after that i will tell you.! – Piyush Mar 11 '14 at 06:26
  • @PiyushGupta can i ask one thing?? i have read that by giving list view height as ' android:layout_height="0dp" ' will be performance oriented. other than giving "wrap content" ?? – Top Cat Mar 11 '14 at 06:27
  • 1
    This will be used android:layout_height="0dp" when you have given weight to ListView at that time its height or width must be 0px depends on orientation of Linear layout. And if you set wrap_content means it shows its original height or width. – Piyush Mar 11 '14 at 06:29
  • Thank you. I am a self learner and i was working on this for two days. Once again thank you. – Top Cat Mar 11 '14 at 06:31
  • @PiyushGupta Cursor getting 5 elements. but cursor in not moving to next. Cursor points only to first one always – Top Cat Mar 11 '14 at 07:03
  • if (cursor.moveToFirst()) { do { MenuData menuData= new MenuData(); //Function to set Bean class values. menuList.add(menuData); } while (cursor.moveToNext()); } – Top Cat Mar 11 '14 at 07:03
0

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

}
Kipruto
  • 721
  • 6
  • 16