gpt4 book ai didi

android - 带有 SQLite 数据库和自定义 ListView 的简单 coursor 适配器

转载 作者:行者123 更新时间:2023-11-29 01:49:32 25 4
gpt4 key购买 nike

我对实际错误在哪里感到困惑。我想使用简单的 coursor 适配器将 SQLite 数据库中的记录显示到 ListView 中。我的 SQLite 数据库中没有像 _id 这样的字段!但错误与此有关,我在下面提到:

错误:

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.blundell.tut/com.blundell.tut.ui.phone.update_page}: java.lang.IllegalArgumentException: column '_id' does not exist

这里提到了我的 Java 文件...

更新页面.java

public class update_page extends Activity
{
DatabaseHandler db = new DatabaseHandler(this);
public String tableName = db.TABLE_CONTACTS;
public String databaseName = db.DATABASE_NAME;

public String task_name = db.KEY_TASK;
public String dt = db.KEY_DATETIME;
private ArrayList<String> results = new ArrayList<String>();
private ArrayList<String> results2 = new ArrayList<String>();

Intent op_intent;
SQLiteDatabase database;
ListView myview;
Cursor c;
String o_name,o_no1;
//String o_id1 = null;
HttpClient client;
Integer op_id,status;
String url;
Context ctx;
Intent myintent;

final MainActivity act = new MainActivity();

public void onCreate(Bundle savedInstanceState)
{
ctx = this;
database = act.getInstance().openOrCreateDatabase("contactsManager",
SQLiteDatabase.CREATE_IF_NECESSARY, null);
Toast.makeText(getApplicationContext(), "Database is open", 1500).show();
final String[] columnsone = {"id", "task_name", "date_time"};
String[] columnstwo = {"task_name", "date_time"};
int to[] = {R.id.lbl_task,R.id.lbl_datetime};
c = database.query("contacts", columnsone, null, null, null, null, null);
status = c.getCount();

if (status == 0)
{
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(ctx);

// Set title
alertDialogBuilder.setTitle("No task are created !!");

// Set dialog message
alertDialogBuilder
.setMessage("Click cancle to exit!")
.setCancelable(false)
.setNegativeButton(
"Cancle",
new DialogInterface.OnClickListener()
{
public void onClick(DialogInterface dialog, int id) {
// If this button is clicked, just close
// the dialog box and do nothing
dialog.cancel();
myintent = new Intent(update_page.this,MainActivity.class);
startActivity(myintent);
}
});

// Create alert dialog
AlertDialog alertDialog = alertDialogBuilder.create();

// Show it
alertDialog.show();
}
else
{
Toast.makeText(getApplicationContext(), "records are available in courser", 1500).show();

if (c != null)
{
Toast.makeText(getApplicationContext(), "in courser", 1500).show();
SimpleCursorAdapter adapter = new
SimpleCursorAdapter(this.ctx,R.layout.update_listview, c, columnstwo, to);
Toast.makeText(getApplicationContext(), "records are in list", 1500).show();

myview.setAdapter(adapter);
}
}

更新列表.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="match_parent"
android:orientation="vertical"
>

<!-- <ScrollView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#404040"
android:fillViewport="true" > -->

<ListView
android:id="@+id/list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0.68"
android:divider="#000000"
android:dividerHeight="2dp"
android:background="#404040">
</ListView>

<!-- </ScrollView> -->

</LinearLayout>

update_listview.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="match_parent"
android:orientation="vertical" >

<TextView
android:id="@+id/lbl_task"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginTop="10dp"
android:text="Medium Text"
android:textColor="#1E90FF"
android:textAppearance="?android:attr/textAppearanceMedium" />

<TextView
android:id="@+id/lbl_datetime"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="3dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="10dp"
android:text="Medium Text"
android:textAppearance="?android:attr/textAppearanceMedium" />
</LinearLayout>

最佳答案

您需要有一个名为 _id 的列。在您的代码中,将您的 id 列更改为 _id。参见 About "_id" field in Android SQLite对于类似的帖子。另请参见“data design considerations” ' 在 Android 文档中

关于android - 带有 SQLite 数据库和自定义 ListView 的简单 coursor 适配器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19092567/

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