且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

自定义列表适配器类

更新时间:2022-10-19 13:34:53

参见http://www.brainysolutions.org/Course/Section/483/learn-android-development-for-beginners-listview [ ^ ]。

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 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:

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

See http://www.brainysolutions.org/Course/Section/483/learn-android-development-for-beginners-listview[^].