android listview進(jìn)階實(shí)例分享
上一篇《android listview初步學(xué)習(xí)實(shí)例代碼》分享了一個(gè)listview初級實(shí)例,本文我們看看一個(gè)進(jìn)階實(shí)例。
目錄結(jié)構(gòu):

MainActivity2
package com.example1.listviewpracticvce;
/*
* 本activity實(shí)現(xiàn)的功能:
* 將數(shù)據(jù)庫中的數(shù)據(jù)用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類似一個(gè)指針
lvPerson = (ListView) findViewById(R.id.lvPerson);
//SimpleCursorAdapter(context, layout, c, from, to )
// listview的布局 cursor 需要顯示的列 在哪個(gè)控件中顯示
//數(shù)組開頭的列必須是"_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 });//顯示每行所用控件
//為了將性別顯示為圖片,這里復(fù)寫了SimpleCursorAdapter這個(gè)類
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','褚衛(wèi)','女')");
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
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:id="@+id/tvPid"
android:layout_width="70dp"
android:layout_height="50dp"
android:gravity="center"
android:textSize="15sp"
/>
<TextView
android:id="@+id/tvPname"
android:layout_width="190dp"
android:layout_height="50dp"
android:gravity="center"
android:textSize="15sp"
/>
<ImageView
android:id="@+id/ivPgender"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<!--
<TextView
android:id="@+id/ivPgender"
android:layout_width="wrap_content"
android:layout_height="50dp"
android:gravity="center"
android:textSize="15sp"
/>
-->
</LinearLayout>
person.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
>
<TextView
android:layout_width="70dp"
android:layout_height="wrap_content"
android:gravity="center"
android:text="編號(hào)"
android:textSize="20sp"
android:textStyle="bold"
/>
<TextView
android:layout_width="190dp"
android:layout_height="wrap_content"
android:gravity="center"
android:text="姓名"
android:textSize="20sp"
android:textStyle="bold"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="性別"
android:textSize="20sp"
android:textStyle="bold"
/>
</LinearLayout>
<ListView
android:id="@+id/lvPerson"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/bg"
android:scrollingCache="false"
android:divider="@drawable/line"
/>
</LinearLayout>
結(jié)果展示

總結(jié)
以上就是本文關(guān)于android listview進(jìn)階實(shí)例分享的全部內(nèi)容,希望對大家有所幫助。感興趣的朋友可以繼續(xù)參閱本站其他相關(guān)專題,如有不足之處,歡迎留言指出。感謝朋友們對本站的支持!
- android listview初步學(xué)習(xí)實(shí)例代碼
- Android ListView實(shí)現(xiàn)下拉頂部圖片變大效果
- Android ListView與RecycleView的對比使用解析
- Android開發(fā)實(shí)現(xiàn)仿QQ消息SwipeMenuListView滑動(dòng)刪除置頂功能【附源碼下載】
- android使用SwipeRefreshLayout實(shí)現(xiàn)ListView下拉刷新上拉加載
- android使用PullToRefresh框架實(shí)現(xiàn)ListView下拉刷新上拉加載更多
- Android開發(fā)listview選中高亮簡單實(shí)現(xiàn)代碼分享
相關(guān)文章
Android實(shí)現(xiàn)網(wǎng)易新聞客戶端側(cè)滑菜單(1)
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)網(wǎng)易新聞客戶端側(cè)滑菜單第一篇,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-11-11
Android編程實(shí)現(xiàn)WebView自適應(yīng)全屏方法小結(jié)
這篇文章主要介紹了Android編程實(shí)現(xiàn)WebView自適應(yīng)全屏方法,結(jié)合實(shí)例形式總結(jié)了三種常用的WebView自適應(yīng)全屏實(shí)現(xiàn)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-12-12
ListView的View回收引起的checkbox狀態(tài)改變監(jiān)聽等問題解決方案
之前講到了自定義Adapter傳遞給ListView時(shí),因?yàn)長istView的View回收,需要注意當(dāng)ListView列表項(xiàng)中包含有帶有狀態(tài)標(biāo)識(shí)控件的問題,感興趣的朋友可以祥看本文,或許會(huì)有意外的收獲哦2013-01-01
Android自定義PopWindow帶動(dòng)畫向下彈出效果
這篇文章主要為大家詳細(xì)介紹了Android自定義PopWindow帶動(dòng)畫向下彈出效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-11-11
Android 深入探究自定義view之事件的分發(fā)機(jī)制與處理詳解
對于安卓程序員來說,自定義view簡直不要太重要,畢竟有很多功能,譬如圓形頭像這些,用單純的原生非常難以實(shí)現(xiàn),而用自定義view,簡直分分鐘2021-11-11
Android編程基礎(chǔ)之Menu功能菜單設(shè)計(jì)實(shí)例
這篇文章主要介紹了Android編程基礎(chǔ)之Menu功能菜單,結(jié)合實(shí)例形式分析了基本的Menu功能菜單原理、定義與響應(yīng)機(jī)制,需要的朋友可以參考下2016-10-10
往Android系統(tǒng)中添加服務(wù)的方法教程
最近因?yàn)槠脚_(tái)升級,需要在系統(tǒng)中添加一些服務(wù),所以將整個(gè)過程總結(jié)一下,下面這篇文章主要給大家介紹了往Android系統(tǒng)中添加服務(wù)的方法教程,需要的朋友可以參考借鑒,下面來一起看看吧。2017-05-05
Android編程實(shí)現(xiàn)自定義ImageView圓圖功能的方法
這篇文章主要介紹了Android編程實(shí)現(xiàn)自定義ImageView圓圖功能的方法,結(jié)合實(shí)例形式分析了Android自定義ImageView及實(shí)現(xiàn)圓圖效果的具體步驟與相關(guān)操作技巧,需要的朋友可以參考下2017-08-08

