首先我们需要有个加载的文件,这个布局文件里面的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

更多相关文章

  1. Android的数据存储
  2. [读书笔记]intent.putExtra的使用与原理分析
  3. android DecorView的使用
  4. 利用drozer进行Android渗透测试
  5. Android:系统信息(内存、cpu、sd卡、电量、版本)的获取
  6. Android获取点击屏幕的位置坐标
  7. Android(安卓)sqlite 数据库操作
  8. mybatisplus的坑 insert标签insert into select无参数问题的解决
  9. python起点网月票榜字体反爬案例

随机推荐

  1. eclipse无法导入Android工程的现象与解决
  2. Android 原始资源文件的使用详解
  3. Android 学习 笔记_08. 广播机制
  4. 面向 Android* Jelly Bean 4.2 的英特尔
  5. Android中TouchEvent触摸事件机制
  6. 2012第一季度智能手机发货量报表
  7. android studio打jar包、aar包
  8. Android(安卓)APK升级代码编写心得
  9. [置顶] Android实训课程之三 这次课老师
  10. 如何编译运行Android系统自带桌面Launche