Android 用sqlite数据库填充listview
我正在尝试用sqlite数据库填充listview,到目前为止,我认为我已经正确地在数据库中创建/添加了项。我不知道如何获取这些项目并将它们放在我的列表视图中 以下是我的数据库创建(RecordsDatabase.java): 这是我在按钮点击时添加按钮的地方(MyMaxes.java): 这里是我希望使用sqlite db和我的custom.xml(MyProgress.java)填充的listview的位置: 这是我的custom_record.xml:Android 用sqlite数据库填充listview,android,sqlite,listview,android-sqlite,android-cursoradapter,Android,Sqlite,Listview,Android Sqlite,Android Cursoradapter,我正在尝试用sqlite数据库填充listview,到目前为止,我认为我已经正确地在数据库中创建/添加了项。我不知道如何获取这些项目并将它们放在我的列表视图中 以下是我的数据库创建(RecordsDatabase.java): 这是我在按钮点击时添加按钮的地方(MyMaxes.java): 这里是我希望使用sqlite db和我的custom.xml(MyProgress.java)填充的listview的位置: 这是我的custom_record.xml: <RelativeLayout
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Date"
android:textSize="18sp"
android:id="@+id/txtDate"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Bench"
android:id="@+id/txtBench"
android:paddingRight="5dp"
android:textSize="18sp"
android:layout_gravity="center_horizontal"
android:layout_alignParentTop="true"
android:paddingLeft="5dp"
android:layout_toLeftOf="@+id/txtSquat"
android:layout_toStartOf="@+id/txtSquat"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Squat"
android:textSize="18sp"
android:paddingRight="5dp"
android:id="@+id/txtSquat"
android:layout_alignParentTop="true"
android:paddingLeft="5dp"
android:layout_toLeftOf="@+id/txtDead"
android:layout_toStartOf="@+id/txtDead"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Dead"
android:paddingRight="5dp"
android:textSize="18sp"
android:id="@+id/txtDead"
android:layout_alignParentTop="true"
android:paddingLeft="5dp"
android:layout_toLeftOf="@+id/txtTotal"
android:layout_toStartOf="@+id/txtTotal"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Total"
android:textSize="18sp"
android:paddingRight="5dp"
android:paddingLeft="5dp"
android:id="@+id/txtTotal"
android:layout_marginRight="34dp"
android:layout_marginEnd="34dp"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"/>
我不确定如何使用sqlite db填充listview。。不过,我认为我正在以正确的方式向db添加项目。谢谢你的帮助 您需要为adapter创建一个对象,并将其设置为ListView的适配器
listViewRec.setAdapter(adapter);
在这一行之后的活动中
ListView listViewRec = (ListView) findViewById(R.id.listViewRecord);
把这个密码
首先从RecordsDatabase类获取所有记录
Cursor recordsCursor = recordsDatabase.getRecords();
然后制作一个TodoCursorAdapter对象
TodoCursorAdapter adapter = new TodoCursorAdapter(getContext(), recordsCursor,
CursorAdapter.FLAG_REGISTER_CONTENT_OBSERVER);
根据developer.android.com的说法
关于标志\寄存器\内容\观察者
如果设置,适配器将在光标上注册一个内容观察者,并在收到通知时调用onContentChanged()。使用此标志时要小心:您需要从适配器中取消设置当前光标,以避免由于其注册的观察者而导致泄漏。将游标适配器与游标装入器一起使用时,不需要此标志
然后将此适配器设置为ListView的适配器
listViewRec.setAdapter(adapter);
您需要为adapter创建一个对象,并将其设置为ListView的适配器
listViewRec.setAdapter(adapter);
在这一行之后的活动中
ListView listViewRec = (ListView) findViewById(R.id.listViewRecord);
把这个密码
首先从RecordsDatabase类获取所有记录
Cursor recordsCursor = recordsDatabase.getRecords();
然后制作一个TodoCursorAdapter对象
TodoCursorAdapter adapter = new TodoCursorAdapter(getContext(), recordsCursor,
CursorAdapter.FLAG_REGISTER_CONTENT_OBSERVER);
根据developer.android.com的说法
关于标志\寄存器\内容\观察者
如果设置,适配器将在光标上注册一个内容观察者,并在收到通知时调用onContentChanged()。使用此标志时要小心:您需要从适配器中取消设置当前光标,以避免由于其注册的观察者而导致泄漏。将游标适配器与游标装入器一起使用时,不需要此标志
然后将此适配器设置为ListView的适配器
listViewRec.setAdapter(adapter);
首先,我建议更改
TodoCursorAdapter
构造函数:
public TodoCursorAdapter(Context context, Cursor c) {
super(context, c, 0);
}
我自己也被flags参数弄糊涂了。尤其是由于不带标志的游标构造函数已经被弃用了一段时间。据我所知,这里的0
值可以正常工作
接下来,您需要从putInformation()
和getRecords()
中删除RecordsDatabase rdb
参数。这个参数完全没有必要,因为Java已经将它作为关键字This
自动发送。但是,您实际上不需要在任何地方使用这个。您只需调用任何希望直接使用的方法。所以不是
rdb.getWritableDatabase();
照办
getWritableDatabase();
或者,你也可以这样做
this.getWritableDatabase();
但是如果您将其禁用,则会自动假定此
现在您需要一个光标
,以便传递到TodoCursorAdapter
构造函数。您可以使用扩展了SQLiteOpenHelper的RecordsDatabase
来执行此操作:
public class MyProgress {
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
// ...snip
RecordsDatabase helper = new RecordsDatabase(this);
Cursor c = db.getRecords();
ListView listViewRec = (ListView) findViewById(R.id.listViewRecord);
Adapter adapter = new TodoRecordsAdapter(this, c);
listViewRec.setAdapter(adapter);
}
}
也可以考虑从<代码> TodoCursorAdapter < /代码>构造函数中删除<代码>光标< /代码>参数。其他的选择是
直接在TodoCursorAdapter
构造函数中创建光标
在MyProgress
中创建Cursor
,并在适配器上调用changecoursor()
首先,我建议更改TodoCursorAdapter
构造函数:
public TodoCursorAdapter(Context context, Cursor c) {
super(context, c, 0);
}
我自己也被flags参数弄糊涂了。尤其是由于不带标志的游标构造函数已经被弃用了一段时间。据我所知,这里的0
值可以正常工作
接下来,您需要从putInformation()
和getRecords()
中删除RecordsDatabase rdb
参数。这个参数完全没有必要,因为Java已经将它作为关键字This
自动发送。但是,您实际上不需要在任何地方使用这个。您只需调用任何希望直接使用的方法。所以不是
rdb.getWritableDatabase();
照办
getWritableDatabase();
或者,你也可以这样做
this.getWritableDatabase();
但是如果您将其禁用,则会自动假定此
现在您需要一个光标
,以便传递到TodoCursorAdapter
构造函数。您可以使用扩展了SQLiteOpenHelper的RecordsDatabase
来执行此操作:
public class MyProgress {
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
// ...snip
RecordsDatabase helper = new RecordsDatabase(this);
Cursor c = db.getRecords();
ListView listViewRec = (ListView) findViewById(R.id.listViewRecord);
Adapter adapter = new TodoRecordsAdapter(this, c);
listViewRec.setAdapter(adapter);
}
}
也可以考虑从<代码> TodoCursorAdapter < /代码>构造函数中删除<代码>光标< /代码>参数。其他的选择是
直接在TodoCursorAdapter
构造函数中创建光标
在MyProgress
中创建Cursor
,并在适配器上调用changecoursor()
您的适配器看起来正确。您只需创建一个TodoCursorAdapter
的实例,并将其发送到列表视图上的setAdapter()
。您也可以考虑使用<代码> SimuleCurraseCux<代码>,因为它执行了您在 TodoCursorAdapter < /COD>中所做的一切。请您给出一个例子,当我试图在MyProgress.java中创建ToDoCurrSoCar适配器的实例时,我不知道要为预期的参数“光标”和“标志”设置什么。您的适配器看起来正确。您只需创建一个TodoCursorAdapter
的实例,并将其发送到列表视图上的setAdapter()
。你也可以考虑使用<代码> SimuleCurraseAdult<代码>,因为它执行了你在 TodoCursorAdapter < /Cord>中所做的一切。请给出一个例子,当我试图在MyProgress.java创建ToDoCurrSoad实例时,我不知道该如何为预期的参数设置“CurSO”。