自定义列表适配器类 [英] Custom List Adapter Class

查看:132
本文介绍了自定义列表适配器类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从Android中的SQLite数据库中获取所有记录并在A String Array中填充它并在自定义arrayadapter类中设置Listview和String数组但是我无法仅显示所有记录的最后一条记录显示在ListView上。但我检查取物是完美的。



获取数据的代码:

  public  ArrayList< GetterSetter>搜索(字符串数据){
Log.d( DATA,数据);
ArrayList< GetterSetter> arr_gs = new ArrayList< GetterSetter>();
GetterSetter gs = new GetterSetter();
SQLiteDatabase db = this.getReadableDatabase();
字符串 qry = 从*中选择* + tbl_profile_details + 其中firstname如'% + data + %';
Log.d( QUERY,qry);
Cursor cursor = db.rawQuery(qry,null);
if (cursor.moveToFirst())
{
do {
Log.d( USERNAME,cursor.getString( 1 )+ // + cursor.getString(< span class =code-digit> 6
)+ + cursor.getString ( 2 )+ +游标.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;
}





在ArrayList中存储数据的代码设置为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);





将数据设置为ListView的自定义类代码:

  public  CustomSearchList(活动上下文,字符串 [] name, String  [] mobile){
super (context,R.layout.list_search,name);
.context = context;
.name = name;
.mobile = mobile;
}

@覆盖
public 查看getView ( int position,View view,ViewGroup parent){
LayoutInflater inflater = context.getLayoutInflater();
查看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;
}

解决方案

参见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[^].


这篇关于自定义列表适配器类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆