自定义列表适配器类

编程入门 行业动态 更新时间:2024-10-25 00:34:33
本文介绍了自定义列表适配器类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在尝试从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( 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; }

解决方案

参见www.brainysolutions/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 www.brainysolutions/Course/Section/483/learn-android-development-for-beginners-listview[^].

更多推荐

自定义列表适配器类

本文发布于:2023-11-26 21:29:47,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1635257.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:自定义   适配器   列表

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!