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

MainActivity2
package com.example1.listviewpracticvce;
/*
* 本activity實現(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類似一個指針
lvPerson = (ListView) findViewById(R.id.lvPerson);
//SimpleCursorAdapter(context, layout, c, from, to )
// listview的布局 cursor 需要顯示的列 在哪個控件中顯示
//數(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這個類
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="編號"
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)階實例分享的全部內(nèi)容,希望對大家有所幫助。感興趣的朋友可以繼續(xù)參閱本站其他相關(guān)專題,如有不足之處,歡迎留言指出。感謝朋友們對本站的支持!
- android listview初步學(xué)習(xí)實例代碼
- Android ListView實現(xiàn)下拉頂部圖片變大效果
- Android ListView與RecycleView的對比使用解析
- Android開發(fā)實現(xiàn)仿QQ消息SwipeMenuListView滑動刪除置頂功能【附源碼下載】
- android使用SwipeRefreshLayout實現(xiàn)ListView下拉刷新上拉加載
- android使用PullToRefresh框架實現(xiàn)ListView下拉刷新上拉加載更多
- Android開發(fā)listview選中高亮簡單實現(xiàn)代碼分享
相關(guān)文章
詳解Android 基于TCP和UDP協(xié)議的Socket通信
這篇文章主要介紹了詳解Android 基于TCP和UDP協(xié)議的Socket通信,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-11-11
AndroidStudio升級到3.0的新特性和注意事項小結(jié)
這篇文章主要介紹了AndroidStudio升級到3.0的新特性和注意事項,非常不錯,具有參考借鑒價值,需要的朋友可以參考下2017-11-11
Android自定義ActionProvider ToolBar實現(xiàn)Menu小紅點(diǎn)
這篇文章主要介紹了Android自定義ActionProvider ToolBar實現(xiàn)Menu小紅點(diǎn),具有一定的參考價值,感興趣的小伙伴們可以參考一下2016-09-09
Android開發(fā)中使用achartengine繪制各種圖表的方法
這篇文章主要介紹了Android開發(fā)中使用achartengine繪制各種圖表的方法,結(jié)合具體實例形式分析了Android基于圖表生成類庫achartengine進(jìn)行圖表繪制的具體步驟與相關(guān)操作技巧,需要的朋友可以參考下2017-10-10
Android Init進(jìn)程對信號的處理流程詳細(xì)介紹
這篇文章主要介紹了Android Init進(jìn)程對信號的處理流程詳細(xì)介紹的相關(guān)資料,需要的朋友可以參考下2017-02-02

