gpt4 book ai didi

android - 使用 Cursor 中的数据填充 ListView ?

转载 作者:行者123 更新时间:2023-11-29 02:08:56 25 4
gpt4 key购买 nike

我在弄清楚如何将数据从 sqlite 绑定(bind)到 ListView 时遇到了一些麻烦。
这是我的全部代码,非常感谢您提供一些帮助或提示。
这里的问题是我遇到了以下异常:
非法参数异常:在 SimpleCursorAdapter 中调用 GetCursor 时,列“_id”不存在。

主.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ListView
android:id="@android:id/android:list"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />

</LinearLayout>

item_list.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="@+id/name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="24dp"
android:padding="6dp" />
<TextView
android:id="@+id/time"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="18dp"
android:padding="1dp" />
<TextView
android:id="@+id/date"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="18dp"
android:padding="4dp"
android:gravity="right" />
</LinearLayout>

实现 SQLite 的类

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 = "example.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 Cursor cursor;

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

public long insert(String name, String time, String date) {
insertStmt = db.compileStatement("insert into table1 values(?,?,?)");
this.insertStmt.bindString(1, name);
this.insertStmt.bindString(2, time);
this.insertStmt.bindString(3, date);
return this.insertStmt.executeInsert();
}

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

public Cursor GetCursor() {
cursor = this.db.query(TABLE_NAME, new String[] { "_id", "name", "time", "date" }, null, null, null, null, "date desc");

return cursor;
}

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 AUTOINCREMENT, name TEXT, time TEXT, date 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);
}
}
}

创建类

import java.text.SimpleDateFormat;
import java.util.Date;
import android.app.ListActivity;
import android.os.Bundle;
import android.widget.SimpleCursorAdapter;

public class SqlTest extends ListActivity {

private DataHelper dh;

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

Date now = new Date();
String time = now.getHours() + ":" + now.getMinutes();
SimpleDateFormat df = new SimpleDateFormat("yyyy/MM/dd");
String date = df.format(now);

this.dh = new DataHelper(this);
this.dh.deleteAll();
this.dh.insert("Porky Pig", time, date);
this.dh.insert("Foghorn Leghorn", time, date);
this.dh.insert("Yosemite Sam", time, date);
this.dh.insert("SD", time, date);

String[] columns = new String[] { "_id", "name", "time", "date" };
int[] to = new int[] {R.id.name, R.id.time, R.id.date};

SimpleCursorAdapter mAdap = new SimpleCursorAdapter(this, R.layout.main, this.dh.GetCursor(), columns, to);

this.setListAdapter(mAdap);
}
}

最佳答案

试试这个:

import java.text.SimpleDateFormat;
import java.util.Date;
import android.app.ListActivity;
import android.os.Bundle;
import android.widget.SimpleCursorAdapter;

public class SqlTest extends ListActivity {

private DataHelper dh;

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

Date now = new Date();
String time = now.getHours() + ":" + now.getMinutes();
SimpleDateFormat df = new SimpleDateFormat("yyyy/MM/dd");
String date = df.format(now);

this.dh = new DataHelper(this);
this.dh.deleteAll();
this.dh.insert("Porky Pig", time, date);
this.dh.insert("Foghorn Leghorn", time, date);
this.dh.insert("Yosemite Sam", time, date);
this.dh.insert("SD", time, date);

// Don't put _id here
String[] columns = new String[] {"name", "time", "date" };
int[] to = new int[] {R.id.name, R.id.time, R.id.date};

SimpleCursorAdapter mAdap = new SimpleCursorAdapter(this, R.layout.main, this.dh.GetCursor(), columns, to);

this.setListAdapter(mAdap);
}
}

关于android - 使用 Cursor 中的数据填充 ListView ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8553603/

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