在 Android 文档中,关于如何执行此操作的说明相互矛盾。我需要将本地 sqlite db 中的数据连接到ListView
布局中的 a,并且我需要能够添加新项目并更新一行(例如按下时删除)。SimpleCursorAdapter
现已贬值,建议LoaderManager
与CursorLoader
. 如果我LoaderManager
与 a 一起使用,CursorLoader
是否需要创建内容提供者?
问问题
245 次
2 回答
0
看看Commonsware 的 LoaderEx。它提供了SQLiteCursorLoader
专门设计用于允许直接通过 Loaders 使用 SQLite,而无需创建 ContentProvider。
于 2013-05-21T15:50:11.613 回答
-1
希望这件事有所帮助。我已经使用列表行中的图标实现了罢工。
public class Home extends Activity {
ListView lv_home;
Cursor cursor;
static SQLiteDatabase database;
ArrayList<Task> al_task;
ArrayAdapter<Task> aa_task;
ImageView iv_big_add, iv_small_add;
LinearLayout ll;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
iv_big_add = (ImageView) findViewById(R.id.imageViewHomeAddBig);
iv_big_add.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
finish();
startActivity(new Intent(Home.this, AddTask.class));
}
});
ll = (LinearLayout) findViewById(R.id.linearLayoutHomeNote);
iv_small_add = (ImageView) findViewById(R.id.imageViewHomeAddSmall);
iv_small_add.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
finish();
startActivity(new Intent(Home.this, AddTask.class));
}
});
loadDatabase();
cursor = database.query("task_info", new String[] { "task_id",
"task_brief", "task_priority", "is_completed" }, null, null,
null, null, null);
if (cursor.getCount() == 0) {
iv_big_add.setVisibility(View.VISIBLE);
ll.setVisibility(View.VISIBLE);
iv_small_add.setVisibility(View.INVISIBLE);
cursor.close();
} else {
iv_big_add.setVisibility(View.INVISIBLE);
ll.setVisibility(View.INVISIBLE);
iv_small_add.setVisibility(View.VISIBLE);
build_listview(cursor);
cursor.close();
lv_home = (ListView) findViewById(R.id.listViewHome);
lv_home.setAdapter(aa_task);
lv_home.setVisibility(View.VISIBLE);
lv_home.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
// TODO Auto-generated method stub
Task task = (Task) lv_home.getItemAtPosition(arg2);
int task_id = task.get_task_id();
startActivity(new Intent(Home.this, TaskDetails.class).putExtra("task_id", task_id));
finish();
}
});
}
}
public void build_listview(Cursor tmp_cursor) {
// TODO Auto-generated method stub
al_task = new ArrayList<Task>();
while (tmp_cursor.moveToNext())
{
al_task.add(new Task(tmp_cursor.getInt(0), tmp_cursor.getString(1), tmp_cursor.getString(2), tmp_cursor.getString(3)));
}
tmp_cursor.close();
aa_task = new TaskAdapter(this, al_task);
}
void loadDatabase() {
database = openOrCreateDatabase("ToDoDatabase.db",
SQLiteDatabase.OPEN_READWRITE, null);
}
static class Task {
int task_id;
String task_brief;
String task_priority;
String is_completed = "false";
Task(int tmp_task_id, String tmp_task_brief, String tmp_task_priority,
String tmp_task_is_completed) {
task_id = tmp_task_id;
task_brief = tmp_task_brief;
task_priority = tmp_task_priority;
is_completed = tmp_task_is_completed;
}
int get_task_id() {
return task_id;
}
String get_task_brief() {
return task_brief;
}
String get_task_priority() {
return task_priority;
}
String get_task_is_completed() {
return is_completed;
}
void set_task_is_completed(String tmp_task_is_completed) {
is_completed = tmp_task_is_completed;
}
}
static class TaskViewHolder {
TextView tv_task_brief;
ImageView iv_task_is_completed;
public TaskViewHolder(TextView tmp_tv_task_brief,
ImageView tmp_iv_task_is_completed) {
tv_task_brief = tmp_tv_task_brief;
iv_task_is_completed = tmp_iv_task_is_completed;
}
TextView get_tv_task_brief() {
return tv_task_brief;
}
ImageView get_iv_task_is_completed() {
return iv_task_is_completed;
}
}
static class TaskAdapter extends ArrayAdapter<Task> {
LayoutInflater inflater;
public TaskAdapter(Context context, List<Task> tmp_al_task) {
super(context, R.layout.single_row_home,
R.id.textViewSingleRowHome, tmp_al_task);
inflater = LayoutInflater.from(context);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
Task task = (Task) this.getItem(position);
final ImageView imageView;
final TextView textView;
if (convertView == null) {
convertView = inflater.inflate(R.layout.single_row_home, null);
imageView = (ImageView) convertView
.findViewById(R.id.imageViewSingleRowHome);
textView = (TextView) convertView
.findViewById(R.id.textViewSingleRowHome);
convertView.setTag(new TaskViewHolder(textView, imageView));
} else {
TaskViewHolder viewHolder = (TaskViewHolder) convertView
.getTag();
imageView = viewHolder.get_iv_task_is_completed();
textView = viewHolder.get_tv_task_brief();
}
imageView.setTag(task);
textView.setText(task.get_task_brief());
if(task.get_task_priority().equals("High"))
textView.setTextColor(Color.RED);
else if(task.get_task_priority().equals("Medium"))
textView.setTextColor(Color.GREEN);
else
textView.setTextColor(Color.BLUE);
if (task.get_task_is_completed().equals("true")) {
imageView.setImageResource(R.drawable.action_cancel_icon);
textView.setPaintFlags(textView.getPaintFlags()
| Paint.STRIKE_THRU_TEXT_FLAG);
} else {
imageView.setImageResource(R.drawable.action_cancel_icon_2);
textView.setPaintFlags( textView.getPaintFlags() & (~ Paint.STRIKE_THRU_TEXT_FLAG));
}
imageView.setFocusable(false);
imageView.setFocusableInTouchMode(false);
imageView.setClickable(true);
imageView.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Task task = (Task) imageView.getTag();
if (task.get_task_is_completed().equals("false")) {
imageView.setImageResource(R.drawable.action_cancel_icon);
ContentValues values = new ContentValues();
values.put("is_completed", "true");
database.update("task_info", values, "task_id=?",
new String[] { task.get_task_id() + "" });
values.clear();
textView.setPaintFlags(textView.getPaintFlags()
| Paint.STRIKE_THRU_TEXT_FLAG);
task.set_task_is_completed("true");
} else {
imageView.setImageResource(R.drawable.action_cancel_icon_2);
ContentValues values = new ContentValues();
values.put("is_completed", "false");
database.update("task_info", values, "task_id=?",
new String[] { task.get_task_id() + "" });
values.clear();
textView.setPaintFlags( textView.getPaintFlags() & (~ Paint.STRIKE_THRU_TEXT_FLAG));
task.set_task_is_completed("false");
}
}
});
return convertView;
}
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
showExitDialog();
return true;
}
return super.onKeyDown(keyCode, event);
}
private void showExitDialog() {
// TODO Auto-generated method stub
AlertDialog.Builder adb = new Builder(Home.this);
adb.setTitle("Warning");
adb.setMessage("Are you sure you want to quit ?");
adb.setIcon(R.drawable.ic_launcher);
adb.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface arg0, int arg1) {
if (database.isOpen())
database.close();
finish();
}
});
adb.setNegativeButton("No", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface arg0, int arg1) {
arg0.dismiss();
}
});
AlertDialog ad = adb.create();
ad.show();
}
}
于 2013-05-21T17:41:48.597 回答