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:
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:
ArrayList<GetterSetter> gettersetter=new ArrayList<>();
gettersetter.clear();
DatabaseHandler db = new DatabaseHandler(getApplicationContext());
gettersetter=db.Search(search_term);
Log.d("Size", "" + gettersetter.size());
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();
}
lv = (ListView) findViewById(R.id.listview1);
customSearchList = new CustomSearchList(this, name, mobile);
lv.setAdapter(customSearchList);
Custom Class Code For Setting Data to ListView:
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;
}