Click here to Skip to main content
15,895,011 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am trying to fetch all the records from SQLite Database in Android and Fill it in A String Array and set the Listview With the String array in a custom arrayadapter class but I am not able to show all the records only the last record is displayed on the ListView. But i checked fetching is perfect.

Code For Fetching Data:
Java
public ArrayList<GetterSetter> Search(String data){
        Log.d("DATA",data);
        ArrayList<GetterSetter> arr_gs=new ArrayList<GetterSetter>();
        GetterSetter gs=new GetterSetter();
        SQLiteDatabase db=this.getReadableDatabase();
        String qry="Select * from "+tbl_profile_details+" where firstname like '%"+data+"%'";
        Log.d("QUERY",qry);
        Cursor cursor=db.rawQuery(qry, null);
        if(cursor.moveToFirst())
        {
            do{
                Log.d("USERNAME",cursor.getString(1)+"//"+cursor.getString(6)+" "+cursor.getString(2)+" "+cursor.getString(3)+" "+cursor.getString(5)+"//"+cursor.getString(7));
                gs.set_Username(cursor.getString(1));
                gs.set_Name(cursor.getString(6)+" "+cursor.getString(2)+" "+cursor.getString(3)+" "+cursor.getString(5));
                gs.set_Mobile(cursor.getString(7));
                arr_gs.add(gs);
            }while(cursor.moveToNext());
        }

        return arr_gs;
    }


Code For Storing Data in ArrayList an Set to ListView:
Java
ArrayList<GetterSetter> gettersetter=new ArrayList<>();
       gettersetter.clear();
       DatabaseHandler db = new DatabaseHandler(getApplicationContext());
       gettersetter=db.Search(search_term);

       Log.d("Size", "" + gettersetter.size());
      /*final ArrayList<String> username=new ArrayList<>();
      ArrayList<String>  name=new  ArrayList<>();
      ArrayList<String> mobile=new  ArrayList<>();*/

       final String[] username=new String[gettersetter.size()];
       final String[] name=new String[gettersetter.size()];
       final String[] mobile=new String[gettersetter.size()];


       for (int i = 0; i < gettersetter.size(); i++) {
           GetterSetter gs1;
           gs1 = gettersetter.get(i);
           username[i]=gs1.get_Username();
           name[i]=gs1.get_Name();
           mobile[i]=gs1.get_Mobile();
       }

       /*Object[] objuser = username.toArray();
       String[] un=new String[objuser.length];

       Object[] objname = name.toArray();
       String[] n=new String[objname.length];

       Object[] objmobile = mobile.toArray();
       String[] m=new String[objmobile.length];
       for(int i=0;i<un.length;i++){
           un[i]=(String)objuser[i];
           n[i]=(String)objname[i];
           m[i]=(String)objmobile[i];
       }*/

       lv = (ListView) findViewById(R.id.listview1);

       customSearchList = new CustomSearchList(this, name, mobile);
       lv.setAdapter(customSearchList);


Custom Class Code For Setting Data to ListView:
Java
public CustomSearchList(Activity context,String[] name,String[] mobile){
        super(context, R.layout.list_search,name);
        this.context=context;
        this.name=name;
        this.mobile=mobile;
    }

    @Override
    public View getView(int position,View view,ViewGroup parent){
        LayoutInflater inflater=context.getLayoutInflater();
        View rowView= inflater.inflate(R.layout.list_search, null, true);
        TextView txtName = (TextView) rowView.findViewById(R.id.txtName);
        TextView txtMobile = (TextView) rowView.findViewById(R.id.txtMobile);
        txtName.setText(name[position]);
        txtMobile.setText(mobile[position]);
        return rowView;
    }
Posted
Comments
Krunal Rohit 2-Nov-15 2:08am    
Looks good to me. Try to debug the adapter.

-KR

1 solution

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900