android ListView,SimpleCursorAdapter

SimpleCursorAdapter

从数据库中读取信息放到listview中的主要代码

xml:

main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_home"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.guoqing.diary.home"
  >

    <ImageButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:srcCompat="@android:drawable/ic_input_add"
        android:id="@+id/imageButton"
        android:layout_alignParentBottom="true"
        android:layout_alignParentEnd="true" />

    <ListView
        android:layout_width="match_parent"
        android:layout_height="fill_parent"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_above="@+id/imageButton"
        android:id="@+id/listview" />

</RelativeLayout>

home

layout.xml

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

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">

        <TextView
            android:id="@+id/title1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:paddingLeft="8dp"
            android:textColor="#1D1D1C"
            android:textSize="20sp"/>

        <TextView
            android:id="@+id/time1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:paddingLeft="8dp"
            android:textColor="#B4B4B9"
            android:textSize="14sp"/>
    </LinearLayout>


</LinearLayout>

java:

 public ListView listView;
  private static final String DATABASE_NAME = "Mydb";
    private static final String TABLE_NAME = "diary1";
    private SQLiteDatabase sqlDB;
     protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_home);

        sqlDB = openOrCreateDatabase(DATABASE_NAME, SQLiteDatabase.CREATE_IF_NECESSARY,null);
        //Toast.makeText(getApplicationContext(),"数据库打开成功",Toast.LENGTH_SHORT).show();
        try {
            sqlDB.execSQL("create table " + TABLE_NAME + " (_id text,title text  ,weather text ,time text, context text)");
        }catch(Exception e){
           // Toast.makeText(getApplicationContext(),"表格已经存在",Toast.LENGTH_SHORT).show();
        }




   final Cursor c = sqlDB.query(TABLE_NAME, null, null, null, null, null, null);

//SimpleCursorAdapter的参数(this,layout,cursor,String[],int[],flag)
//String[] 是数据表中的column,int[]是layout中放column的textview
//flag自行百度
        final SimpleCursorAdapter listAdapter = new SimpleCursorAdapter(this,
                R.layout.listviewlayout,
                c,
                new String[]{"title", "time"},
                new int[]{R.id.title1, R.id.time1},
                CursorAdapter.FLAG_REGISTER_CONTENT_OBSERVER);

        listView = (ListView) findViewById(R.id.listview);
       //添加适配器
        listView.setAdapter(listAdapter);
       //更新内容
        listView.invalidateViews();



//长按删除某一条记录
 listView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
            @Override
            public boolean onItemLongClick(AdapterView<?> parent, View view, final int position, final long id) {

                //Toast.makeText(getApplicationContext(),"Longpress",Toast.LENGTH_SHORT).show();

                AlertDialog.Builder builder = new AlertDialog.Builder(home.this);

                builder.setTitle("Prompt");
                builder.setMessage("Do you want to delete?");

                builder.setNegativeButton("Cancel",new DialogInterface.OnClickListener(){
                    public void onClick (DialogInterface arg0,int arg1)
                    {
                           // Toast.makeText(getApplicationContext(),"cancel",Toast.LENGTH_SHORT).show();
                    }

                });

                builder.setPositiveButton("OK",new DialogInterface.OnClickListener(){
                    public void onClick(DialogInterface arg0,int arg1)
                    {
                        //Toast.makeText(getApplicationContext(),"ok",Toast.LENGTH_SHORT).show();

                        c.moveToPosition(position);
                        String title_name = c.getString(1);
                        sqlDB.delete(TABLE_NAME,"title=?",new String[]{title_name});
                        Intent i=new Intent(getApplicationContext(),home.class);
                        startActivity(i);      
                }
                });

                builder.show();

                return true;
            }
        });

以上

新人练手,请大佬指教

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

rainbow oreo

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值