gpt4 book ai didi

android - 如何将 ListView 项加载和保存到 SQLiteDatabase?

转载 作者:搜寻专家 更新时间:2023-11-01 08:16:06 24 4
gpt4 key购买 nike

我想让它让用户单击一个按钮打开一个对话框,用户键入一个项目的名称,他们单击“确定”,然后该项目被添加到 ListView 中。到目前为止,我已经到了项目被添加到 ListView 的地步,但是没有办法保存数据。我有一个名为 DataHelper 的助手来创建 SQLiteDatabase,但我不知道如何将它保存到数据库中。 我需要一个代码示例,因为我经验不足。

这是我的 .java:

package com.shoppinglist;

import java.util.ArrayList;

import android.app.Dialog;
import android.app.ListActivity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;

public class ShoppingList extends ListActivity {
/** Called when the activity is first created. */
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

ListView lv = (ListView) findViewById(android.R.id.list);
final ArrayList<String> items = new ArrayList<String>();
final ArrayAdapter<String> adapter;
adapter = new ArrayAdapter<String>(
this, android.R.layout.simple_list_item_1, items);
lv.setAdapter(adapter);

Button button1main = (Button) findViewById(R.id.add);
button1main.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
final Dialog additem = new Dialog(ShoppingList.this);
additem.setContentView(R.layout.maindialog);
final EditText et = (EditText)additem.findViewById(R.id.edittext);
additem.setTitle("Type your item");
additem.setCancelable(true);
et.setHint("Type the name of an item...");

Button button = (Button) additem.findViewById(R.id.cancel);
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
additem.dismiss();
}
});
additem.show();

Button ok = (Button) additem.findViewById(R.id.ok);
ok.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
items.add(et.getText().toString());
adapter.notifyDataSetChanged();
additem.dismiss();
et.setText("");
}
});
}
});
}
}

我的 DataHelper 类:

package com.shoppinglist;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.database.sqlite.SQLiteStatement;
import android.util.Log;

public class DataHelper {

private static final String DATABASE_NAME = "items.db";
private static final int DATABASE_VERSION = 1;
private static final String TABLE_NAME = "table1";

private Context context;
private SQLiteDatabase db;

private SQLiteStatement insertStmt;
private static final String INSERT = "insert into "
+ TABLE_NAME + "(name) values (?)";

public DataHelper(Context context) {
this.context = context;
OpenHelper openHelper = new OpenHelper(this.context);
this.db = openHelper.getWritableDatabase();
this.insertStmt = this.db.compileStatement(INSERT);
}

public long insert(String name) {
this.insertStmt.bindString(1, name);
return this.insertStmt.executeInsert();
}

public void deleteAll() {
this.db.delete(TABLE_NAME, null, null);
}

public List<String> selectAll() {
List<String> list = new ArrayList<String>();
Cursor cursor = this.db.query(TABLE_NAME, new String[] { "name" },
null, null, null, null, "name desc");
if (cursor.moveToFirst()) {
do {
list.add(cursor.getString(0));
} while (cursor.moveToNext());
}
if (cursor !=null && !cursor.isClosed()) {
cursor.close();
}
return list;
}

private static class OpenHelper extends SQLiteOpenHelper {

OpenHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}

@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("CREATE TABLE " + TABLE_NAME + " (id INTEGER PRIMARY KEY, name TEXT)");
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
Log.w("Example", "Upgrading database, this will drop tables and recreate.");
db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
onCreate(db);
}
}
}

我的 main.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#FF9900"
android:padding="10dp"
android:orientation="vertical">
<Button
android:id="@+id/add"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Add item..."
android:layout_gravity="center"/>
<ListView android:id="@android:id/list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#FFCC00"/>
</LinearLayout>

还有我的 maindialog.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/layout_root"
android:orientation="vertical"
android:background="#FFFFFF"
android:minHeight="100dp"
android:minWidth="300dp">
<EditText
android:id="@+id/edittext"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
<LinearLayout
android:id="@+id/button_layout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal"
android:layout_gravity="bottom">
<Button
android:id="@+id/ok"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Ok"/>
<Button
android:id="@+id/cancel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Cancel"/>
</LinearLayout>
</LinearLayout>

提前致谢!

最佳答案

How do I load and save ListView items to an SQLiteDatabase?

这个问题问错了,恕我直言。正确的问题是:

How do I get a ListView to reflect what is in my SQLiteDatabase?

第一步是使用CursorSimpleCursorAdapter,而不是ArrayListArrayAdapter

第二步是当用户添加一个条目时,在 SQLiteDatabase 上执行一个 insert(),然后 requery() Cursor 来更新 ListView

Here is a sample project这证明了这一点。 Here is another .

关于android - 如何将 ListView 项加载和保存到 SQLiteDatabase?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4777003/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com