【问题标题】:How can i use the Check Boxes with ListView and Insert the value in Sqlite Database in android我如何使用带有 ListView 的复选框并在 android 的 Sqlite 数据库中插入值
【发布时间】:2012-07-11 22:10:48
【问题描述】:

我创建了一个包含三个表的数据库,并将该数据库放入资产文件夹中。现在我想在带有复选框的列表视图中显示一列的值,然后在用户选择时将它们插入到另一个表中。那么我该怎么做。

到目前为止,我所做的是获取数据并显示在列表中。但是我怎样才能放置复选框,然后获取我不知道的值。我使用的代码如下

1.主要活动

package com.example;

import android.app.ListActivity;
import android.database.Cursor;
import android.os.Bundle;
import android.widget.ListAdapter;
import android.widget.SimpleCursorAdapter;

public class MainActivity extends ListActivity {

    private Cursor employees;
    private MyDatabase db;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        db = new MyDatabase(this);
        employees = db.getEmployees(); // you would not typically call this on the main thread

        ListAdapter adapter = new SimpleCursorAdapter(this, 
                android.R.layout.simple_list_item_1, 
                employees, 
                new String[] {"FullName"}, 
                new int[] {android.R.id.text1});

        getListView().setAdapter(adapter);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        employees.close();
        db.close();
    }

}

2.Mydatabase.java

package com.example;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteQueryBuilder;

import com.readystatesoftware.sqliteasset.SQLiteAssetHelper;

public class MyDatabase extends SQLiteAssetHelper {

    private static final String DATABASE_NAME = "northwind";
    private static final int DATABASE_VERSION = 2;

    public MyDatabase(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);  
        //setForcedUpgradeVersion(2);
    }

    public Cursor getEmployees() {

        SQLiteDatabase db = getReadableDatabase();
        SQLiteQueryBuilder qb = new SQLiteQueryBuilder();

        String [] sqlSelect = {"0 _id", "FullName"}; 
        String sqlTables = "Employees";

        qb.setTables(sqlTables);
        Cursor c = qb.query(db, sqlSelect, null, null,
                null, null, null);

        c.moveToFirst();
        return c;

    }

}

请建议我如何做到这一点。任何帮助都必须感谢

【问题讨论】:

  • 你在向列表视图添加复选框时遇到问题吗???
  • 就像我使用了列表适配器,然后我如何使用其中的复选框,然后我想将该选定的值插入到另一个表中。我是新手,所以如果我的问题很愚蠢@Goofy,我不太抱歉
  • 所以你想要列表视图中的复选框...对吗?
  • 是的,我想然后我想在表中插入选定的值@Goofy
  • 您可以参考此链接查看列表视图中的复选框windrealm.org/tutorials/android/…

标签: android sqlite checkbox android-assets


【解决方案1】:

在我看来,您应该为 ListView 创建一个自定义适配器并手动迭代结果数据集,更新一个 ArrayList<MyObject>(您也应该创建)。

然后在您的自定义适配器中创建一些函数以获得“选定”项,或创建一些函数,如 public void setSelected(int position) 相应地更改复选框,private boolean isSelected(int position) 返回复选框是否被选中。

首先我们需要对象类来保存数据集:

  class MyObject {
      //properties with setters and getters
      boolean selected;
  }

活动中:

private MyActivity extends Activity {
  ...
  ArrayList<MyObject> data = new ArrayList<MyObject>();
  ...
  //get dataset
  for(int i=0; i<cursor.getCount(); i++) {
     //create and set MyObject
     data.add(MyObject); 
  }     
  ...
  CustomAdapter myAdapter = new CustomAdapter(..., data);
  listView.setAdapter(myAdapter);
  ...
}

在 CustomAdapter 类中:

class CustomAdapter extends BaseAdapter {
   ArrayList<MyObject> data;
   //constructor, properties, getters, setters
   //implement methods

   public void setSelected(int position) {
      data.get(position).setSelected(true); //boolean selected property defines whether the checkbox is selected or not
      notifyDatasetChanged(); //update the UI
   }

   @Override
   getView() {
      CheckBox checkBox; //the check box of the list row
      ...
      if(data.get(position).isSelected()) {
         checkBox.setSelected(true);
      } else {
         checkBox.setSelected(false);
      } //update the checkbox accordingly
   }
}

我将它们简要地写下来给你一个概述。有很多代码要写,但我给你的是“热门片”。

【讨论】:

  • 在我的答案中添加了代码。我想这会帮助您理解,但此代码不适用于复制粘贴。
猜你喜欢
  • 1970-01-01
  • 2014-05-28
  • 2012-11-19
  • 2021-08-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-07-17
相关资源
最近更新 更多