您的位置:首页 > 移动开发 > Android开发

Android 自定义Listview 如何绑定Sqlite数据库数据

2016-04-02 19:04 393 查看
首先我们需要有个加载的文件,这个布局文件里面的bills.xml,这个布局里面有个Listview控件。

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:background="#FFFFFF">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="50dp"
android:id="@+id/linearLayout3"
android:orientation="horizontal"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="false"
android:background="#444eb9"
android:weightSum="1"
android:gravity="center_vertical">

<ImageView
android:layout_width="80dp"
android:layout_height="30dp"
android:id="@+id/imageviewBack"
android:src="@drawable/back"
android:background="#444eb9"/>

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="账单"
android:id="@+id/textView2"
android:layout_gravity="center_vertical"
android:background="#444eb9"
android:layout_marginLeft="80dp"
android:textColor="#FFFFFF"
android:textStyle="bold"
android:editable="false"
android:enabled="false"
android:hint="size"
android:textIsSelectable="false"
android:textSize="20dp"/>
</LinearLayout>
<ListView
android:id="@+id/list_bills"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:divider="#ffffff"
android:dividerHeight="1dip"
android:layout_below="@+id/linearLayout3"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:background="#fbf5f5"/>
</RelativeLayout>


接下来就是自定义的listview的布局文件billsitem.xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
>

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="no data"
android:id="@+id/texttime"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginRight="28dp"
android:layout_marginTop="20dp"
android:layout_marginLeft="20dp"
android:gravity="center"/>

<ImageView
android:layout_width="55dp"
android:layout_height="55dp"
android:id="@+id/imagetype"
android:layout_alignParentTop="true"
android:layout_toRightOf="@+id/texttime"
android:padding="10dp"/>

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="no data"
android:id="@+id/textfee"
android:layout_marginLeft="61dp"
android:layout_marginStart="61dp"
android:layout_alignParentTop="true"
android:layout_alignLeft="@+id/textremarks"
android:layout_alignStart="@+id/textremarks"
android:layout_marginTop="10dp"
android:gravity="center"/>

<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Large Text"
android:id="@+id/textremarks"
android:layout_alignBottom="@+id/imagetype"
android:layout_toRightOf="@+id/imagetype"
android:layout_toEndOf="@+id/imagetype"
android:layout_marginLeft="34dp"
android:layout_marginStart="34dp"
android:gravity="center"/>
</RelativeLayout>


接下来就是Android里面的activity了,这接上代码:

package com.bank;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.util.Log;
import android.view.ContextMenu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.SimpleAdapter;

import java.util.ArrayList;
import java.util.HashMap;

/**
* Created by zhouchenglin on 2016/4/1.
*/
public class BillsActivity extends Activity implements View.OnClickListener {

//列举数据的ListView
private ListView mlistbills;
// 适配器
private SimpleAdapter mlistbillsAdapter;
//数据库
private MySQLiteHelper mMysql;
private SQLiteDatabase mDataBase;
private ImageView imageviewback;

// 存储数据的数组列表
ArrayList<HashMap<String, Object>> listData = new ArrayList<HashMap<String, Object>>();

public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.bills);

mlistbills = (ListView) this.findViewById(R.id.list_bills);
imageviewback = (ImageView) this.findViewById(R.id.imageviewBack);
imageviewback.setOnClickListener(this);

GetData();
mlistbillsAdapter = new SimpleAdapter(
this,
listData,
R.layout.billsitem,
new String[]{"Time", "Type", "Fee", "Remarks"},
new int[]{R.id.texttime, R.id.imagetype, R.id.textfee, R.id.textremarks}
);
//赋予数据
mlistbills.setAdapter(mlistbillsAdapter);

//响应
mlistbills.setOnCreateContextMenuListener(listviewLongPress);
}

//从数据库获得适配器数据

public void GetData() {
mMysql = new MySQLiteHelper(this, "finance.db", null, 1);
mDataBase = mMysql.getReadableDatabase();

Cursor cursor = mDataBase.rawQuery("select * from finance", null);
cursor.moveToFirst();
int columnsSize = cursor.getColumnCount();
int number = 0;
while (number < cursor.getCount()) {

//  cursor.move(i);
HashMap<String, Object> map = new HashMap<String, Object>();
String budget = cursor.getString(cursor.getColumnIndex("Budget"));
map.put("ID", cursor.getString(cursor.getColumnIndex("ID")));
map.put("Fee", cursor.getDouble(cursor.getColumnIndex("Fee")));
map.put("Time", cursor.getString(cursor.getColumnIndex("Time")));
if (budget.equals("收入"))
map.put("Fee", "+" + cursor.getString(cursor.getColumnIndex("Fee")));
else
map.put("Fee", "-" + cursor.getString(cursor.getColumnIndex("Fee")));
map.put("Remarks", cursor.getString(cursor.getColumnIndex("Remarks")));

if ((cursor.getString(cursor.getColumnIndex("Type"))).equals("衣")) {
map.put("Type", R.drawable.cloth);
} else if ((cursor.getString(cursor.getColumnIndex("Type"))).equals("食")) {
map.put("Type", R.drawable.chart);
} else if ((cursor.getString(cursor.getColumnIndex("Type"))).equals("住")) {
map.put("Type", R.drawable.zhu);
} else if ((cursor.getString(cursor.getColumnIndex("Type"))).equals("行")) {
map.put("Type", R.drawable.getmoney);
} else if ((cursor.getString(cursor.getColumnIndex("Type"))).equals("其他")) {
map.put("Type", R.drawable.getmoney);
}

cursor.moveToNext();
listData.add(map);
number++;
System.out.println(listData);
}

cursor.close();
mDataBase.close();
mMysql.close();
}

@Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.imageviewBack:
this.finish();
break;
default:
break;
}

}

// 长按事件响应
View.OnCreateContextMenuListener listviewLongPress = new View.OnCreateContextMenuListener() {
@Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
// TODO Auto-generated method stub
final AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) menuInfo;
new AlertDialog.Builder(BillsActivity.this)
/* 弹出窗口的最上头文字 */
.setTitle("删除当前数据")
/* 设置弹出窗口的图式 */
.setIcon(android.R.drawable.ic_dialog_info)
/* 设置弹出窗口的信息 */
.setMessage("确定删除当前记录")
.setPositiveButton("是",
new DialogInterface.OnClickListener() {
public void onClick(
DialogInterface dialoginterface, int i) {
// 获取位置索引
int mListPos = info.position;
// 获取对应HashMap数据内容
HashMap<String, Object> map = listData.get(mListPos);
// 获取id
int id = Integer.valueOf((map.get("ID").toString()));

// 获取数组具体值后,可以对数据进行相关的操作,例如更新数据
String[] whereArgs = new String[]{String.valueOf(id)};
//获取当前数据库
mMysql = new MySQLiteHelper(BillsActivity.this, "finance.db", null, 1);
mDataBase = mMysql.getReadableDatabase();
try {
mDataBase.delete("Finance", "ID=?", whereArgs);
listData.remove(mListPos);
mlistbillsAdapter.notifyDataSetChanged();
} catch (Exception e) {
Log.e("删除出错了", "error");
} finally {
mDataBase.close();
mMysql.close();
}
}
}
).setNegativeButton(
"否",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialoginterface, int i) {
}
}
).show();
}
};
}


对于数据里面的操作,我前面的文章有讲,这个实现的流程就是先获得数据,在将数据适配器的数据反映到控件上去。主要是要将对应关系搞清楚,控件的类型和内容。

后面的响应事件,常按listview就会弹出对话框,进行删除操作。其他的R.drawable.XX表示的是图片资源,可以自行添加.

http://blog.csdn.net/richnaly/article/details/7790246
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: