android中如何实现listview
这篇文章主要介绍了android中如何实现listview,具有一定借鉴价值,感兴趣的朋友可以参考下,希望大家阅读完这篇文章之后大有收获,下面让小编带着大家一起了解一下。
10多年的明水网站建设经验,针对设计、前端、开发、售后、文案、推广等六对一服务,响应快,48小时及时工作处理。成都全网营销的优势是能够根据用户设备显示端的尺寸不同,自动调整明水建站的显示方式,使网站能够适用不同显示终端,在浏览器中调整网站的宽度,无论在任何一种浏览器上浏览网站,都能展现优雅布局与设计,从而大程度地提升浏览体验。创新互联公司从事“明水网站设计”,“明水网站推广”以来,每个客户项目都认真落实执行。
目录结构:
MainActivity2
package com.example1.listviewpracticvce; /* * 本activity实现的功能: * 将数据库中的数据用listview显示出来 */ import com.example1.listviewdao.PersonDAO; import android.app.Activity; import android.content.Context; import android.database.Cursor; import android.os.Bundle; import android.view.View; import android.widget.AdapterView; import android.widget.ImageView; import android.widget.ListView; import android.widget.SimpleCursorAdapter; import android.widget.TextView; import android.widget.Toast; import android.widget.AdapterView.OnItemClickListener; import android.widget.SimpleCursorAdapter.ViewBinder; public class MainActivity2 extends Activity { ListView lvPerson; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.person); PersonDAO personDAO = new PersonDAO(this); Cursor cursor = personDAO.getPersons(); //cursor类似一个指针 lvPerson = (ListView) findViewById(R.id.lvPerson); //SimpleCursorAdapter(context, layout, c, from, to ) // listview的布局 cursor 需要显示的列 在哪个控件中显示 //数组开头的列必须是"_id" SimpleCursorAdapter adapter = new PersonAdapter(this, R.layout.person_item, cursor, new String[]{ "_id", "pname", "pgender" }, new int[]{ R.id.tvPid, R.id.tvPname, R.id.ivPgender }); // SimpleCursorAdapter adapter = new SimpleCursorAdapter(this, R.layout.person_item, cursor, // new String[]{ "_id", "pname", "pgender" }, //要显示的列 // new int[]{ R.id.tvPid, R.id.tvPname, R.id.ivPgender });//显示每行所用控件 //为了将性别显示为图片,这里复写了SimpleCursorAdapter这个类 lvPerson.setAdapter(adapter); lvPerson.setOnItemClickListener(new OnItemClickListener() { @Override public void onItemClick(AdapterView> parent, View view, int position, long id) { Cursor cursor = (Cursor) parent.getItemAtPosition(position); Toast.makeText(getApplicationContext(), cursor.getString(1), Toast.LENGTH_sHORT).show(); } } ); } } //利用源代码定制 class PersonAdapter extends SimpleCursorAdapter { private Cursor mCursor; protected int[] mFrom; protected int[] mTo; private ViewBinder mViewBinder; public PersonAdapter(Context context, int layout, Cursor c, String[] from, int[] to) { super(context, layout, c, from, to); mCursor = c; mTo = to; findColumns(from); } @Override public void bindView(View view, Context context, Cursor cursor) { final ViewBinder binder = mViewBinder; final int count = mTo.length; final int[] from = mFrom; final int[] to = mTo; for (int i = 0; i < count; i++) { final View v = view.findViewById(to[i]); if (v != null) { Boolean bound = false; if (binder != null) { bound = binder.setViewValue(v, cursor, from[i]); } if (!bound) { String text = cursor.getString(from[i]); if (text == null) { text = ""; } if (v instanceof TextView) { setViewText((TextView) v, text); } else if (v instanceof ImageView) { if (text.equals("男")) { setViewImage((ImageView) v, String.valueOf(R.drawable.boy)); } else { setViewImage((ImageView) v, String.valueOf(R.drawable.girl)); } } else { throw new IllegalStateException(v.getClass().getName() + " is not a " + " view that can be bounds by this SimpleCursorAdapter"); } } } } } private void findColumns(String[] from) { if (mCursor != null) { int i; int count = from.length; if (mFrom == null || mFrom.length != count) { mFrom = new int[count]; } for (i = 0; i < count; i++) { mFrom[i] = mCursor.getColumnIndexOrThrow(from[i]); } } else { mFrom = null; } } }
DBOpenHelper
package com.example1.listviewdao; import android.content.Context; import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteOpenHelper; public class DBOpenHelper extends SQLiteOpenHelper { private static final int VERSION = 1; private static final String DBNAME = "data.db"; private static final String PERSON="t_person"; public DBOpenHelper(Context context) { super(context, DBNAME, null, VERSION); } @Override public void onCreate(SQLiteDatabase db) { db.execSQL("create table "+PERSON+" (_id varchar(4) primary key,pname varchar(20),pgender varchar(2))"); db.execSQL("insert into t_person (_id, pname, pgender) values ('1001','张三','男')"); db.execSQL("insert into t_person (_id, pname, pgender) values ('1002','李四','男')"); db.execSQL("insert into t_person (_id, pname, pgender) values ('1003','王五','女')"); db.execSQL("insert into t_person (_id, pname, pgender) values ('1004','赵钱','男')"); db.execSQL("insert into t_person (_id, pname, pgender) values ('1005','孙李','女')"); db.execSQL("insert into t_person (_id, pname, pgender) values ('1006','周吴','男')"); db.execSQL("insert into t_person (_id, pname, pgender) values ('1007','郑王','男')"); db.execSQL("insert into t_person (_id, pname, pgender) values ('1008','冯陈','男')"); db.execSQL("insert into t_person (_id, pname, pgender) values ('1009','褚卫','女')"); db.execSQL("insert into t_person (_id, pname, pgender) values ('1010','蒋沈','男')"); db.execSQL("insert into t_person (_id, pname, pgender) values ('1011','韩杨','男')"); db.execSQL("insert into t_person (_id, pname, pgender) values ('1012','朱秦','男')"); db.execSQL("insert into t_person (_id, pname, pgender) values ('1013','尤许','男')"); } @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { } }
Person
package com.example1.listviewdao; public class Person { private String pid; private String pname; private String pgender; public Person() { super(); } public Person(String pid, String pname, String pgender) { super(); this.pid = pid; this.pname = pname; this.pgender = pgender; } public String getPid() { return pid; } public void setPid(String pid) { this.pid = pid; } public String getPname() { return pname; } public void setPname(String pname) { this.pname = pname; } public String getPgender() { return pgender; } public void setPgender(String pgender) { this.pgender = pgender; } @Override public String toString() { return "pid=" + pid + ";pname=" + pname + ";pgender=" + pgender; } }
PersonDAO
package com.example1.listviewdao; import android.content.Context; import android.database.Cursor; import android.database.sqlite.SQLiteDatabase; public class PersonDAO { private DBOpenHelper helper; private SQLiteDatabase db; public PersonDAO(Context context) { helper = new DBOpenHelper(context); } public Cursor getPersons(int start, int count) { db = helper.getWritableDatabase(); Cursor cursor=db.query("t_person", new String[]{"_id","pname","pgender"}, null, null, null, null, "_id desc",start+","+count); return cursor; } public Cursor getPersons() { db = helper.getWritableDatabase(); Cursor cursor=db.query("t_person", new String[]{"_id,pname,pgender"}, null, null, null, null, null); return cursor; } public long getCount() { db = helper.getWritableDatabase(); Cursor cursor = db.rawQuery("select count(_id) from t_person", null); if (cursor.moveToNext()) { return cursor.getlong(0); } return 0; } }
person_item.xml
person.xml
结果展示
感谢你能够认真阅读完这篇文章,希望小编分享的“android中如何实现listview”这篇文章对大家有帮助,同时也希望大家多多支持创新互联,关注创新互联行业资讯频道,更多相关知识等着你来学习!
网站题目:android中如何实现listview
当前路径:http://pwwzsj.com/article/jgsghh.html