0

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

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

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;

    }

}

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

4

1 回答 1

2

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
   }
}

我非常简要地写下来给你一个概述。有很多代码要写,但我给你的是“热门文章”。

于 2012-07-12T08:33:29.870 回答