我创建了一个包含三个表的数据库,并将该数据库放入资产文件夹中。现在我想显示列表视图中一列的值和复选框,然后用户选择想要将它们插入到另一个表中。那怎么能这样做呢。
到目前为止,我所做的是获取数据并显示在列表中。但是如何放置复选框然后获取我不知道的值。我使用的代码在
下面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;
}
}
请建议我,我该怎么做...任何帮助必须得到赞赏
答案 0 :(得分: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
}
}
我非常简短地写下来给你一个概述。有很多代码要写,但我给你的是“热门篇章”。