我正在尝试制作一个ToDo应用,该应用在自定义列表项中显示用户输入的任务名称和任务描述。这些数据将来自SQLite数据库。
我面临的问题是我不知道如何将任务名称和任务描述都放置到此自定义列表项中,因为我所做的所有尝试都只是显示一个空白屏幕。
这是自定义list_item.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:background="@color/colorWhite">
<TextView
android:id="@+id/task_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginStart="55dp"
android:layout_marginTop="25dp"
android:text="TextView" />
<View
android:id="@+id/viewbutton"
android:layout_width="16dp"
android:layout_height="16dp"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginStart="21dp"
android:layout_marginTop="24dp"
android:background="@layout/round" />
<TextView
android:id="@+id/task_desc"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/task_name"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginStart="55dp"
android:layout_marginTop="53dp"
android:layout_marginBottom="10dp"
android:text="Undefined"
android:textStyle="bold" />
</RelativeLayout>
这是我的DatabaseHelper.java
public class DatabaseHelper extends SQLiteOpenHelper {
private static final String TAG = "DatabaseHelper";
private static final String TABLE_NAME = "task_table";
private static final String COL1 = "ID";
private static final String COL2 = "name";
private static final String COL3 = "priority";
private static final String COL4 = "desc";
public DatabaseHelper(Context context) {
super(context, TABLE_NAME, null, 1);
}
@Override
public void onCreate(SQLiteDatabase db) {
String createTable = "CREATE TABLE " + TABLE_NAME + " (ID INTEGER PRIMARY KEY AUTOINCREMENT, " +
COL2 +" TEXT, " + COL3 + " INTEGER, '" + COL4 + "' TEXT )";
db.execSQL(createTable);
}
@Override
public void onUpgrade(SQLiteDatabase db, int i, int i1) {
db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
onCreate(db);
}
public boolean addData(String item, String desc) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put(COL2, item);
contentValues.put(COL4, desc);
Log.d(TAG, "addData: Adding " + item + " to " + TABLE_NAME);
long result = db.insert(TABLE_NAME, null, contentValues);
if (result == -1) {
return false;
} else {
return true;
}
}
public ArrayList<String> getTaskList(){
ArrayList<String> taskList = new ArrayList<>();
SQLiteDatabase td = this.getReadableDatabase();
Cursor cursor = td.query(TABLE_NAME,new String[]{COL2},null,null,null,null,null);
while(cursor.moveToNext()){
int index = cursor.getColumnIndex(COL2);
taskList.add(cursor.getString(index));
}
cursor.close();
td.close();
return taskList;
}
public Cursor getData(){
SQLiteDatabase db = this.getWritableDatabase();
String query = "SELECT * FROM " + TABLE_NAME;
Cursor data = db.rawQuery(query, null);
return data;
}
public Cursor getItemID(String name){
SQLiteDatabase db = this.getWritableDatabase();
String query = "SELECT " + COL1 + " FROM " + TABLE_NAME +
" WHERE " + COL2 + " = '" + name + "'";
Cursor data = db.rawQuery(query, null);
return data;
}
这是我的ListDataActivity.java
public class ListDataActivity extends AppCompatActivity {
private static final String TAG = "ListDataActivity";
DatabaseHelper mDatabaseHelper;
TextView mTaskName;
TextView mTaskDesc;
CustomAdapter customAdapter;
ArrayAdapter<String> adapter;
private ListView mListView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.list_layout);
mListView = (ListView) findViewById(R.id.listView);
mDatabaseHelper = new DatabaseHelper(this);
mTaskDesc = (TextView) findViewById(R.id.task_desc);
mTaskName = (TextView) findViewById(R.id.task_name);
populateListView();
}
private void populateListView() {
Log.d(TAG, "populateListView: Displaying data in the ListView.");
//get the data and append to a list
ArrayList<String> taskList = mDatabaseHelper.getTaskList();
if (adapter == null) {
adapter = new ArrayAdapter<String>(this, R.layout.list_item, R.id.task_name, taskList);
mListView.setAdapter(adapter);
} else {
adapter.clear();
adapter.addAll(taskList);
adapter.notifyDataSetChanged();
}
如果我不清楚的是什么,我很乐意对此进行详细说明,并预先感谢您!
我是Java的新手,所以如果我犯了任何愚蠢的错误,请放轻松!
答案 0 :(得分:1)
使用现有的ArrayAdapter,它仅使用对象的toString方法填充列表中的视图。没有范围为多个视图传递多种方法。
您可以创建一个自定义适配器(子类ArrayAdapter),或者由于数据来自SQLite数据库,您可以利用Cursor适配器(例如SimpleCursorAdapter)。后者适合显示多列。
SimpleCursorAdapter的构造函数为SimpleCursorAdapter(Context context, int layout, Cursor c, String[] from, int[] to, int flags)
第4个和第5个参数用于指定成对的值(字符串和整数),每对值由列名称和视图ID(来自布局(第2个参数))组成,列中的数据进入该视图将被放置。
注意! CursorAdapters要求列名称为 _id
,并且该列应为 rowid 的别名(例如_id INTEGER PRIMARY KEY
或_id INTEGER PRIMARY KEY AUTOINCREMENT
)。如果不存在此类列,则可以使用rowid AS _id
作为列之一,在查询中创建一个列,例如SELECT *, rowid AS _id FROM mytable
(请注意,如果使用WITHOUT ROWID定义了表,则此方法将无效)。
答案 1 :(得分:0)
public ArrayList<String> getTaskList(){
List<TaskList> taskList = new ArrayList<>();
SQLiteDatabase td = this.getReadableDatabase();
Cursor cursor = td.query(TABLE_NAME,new String[]{COL2},null,null,null,null,null);
while(cursor.moveToNext()){
int index = cursor.getColumnIndex(COL2);
int desc = cursor.getColumnIndex(COL4);
taskList.add(cursor.getString(index),cursor.getString(desc));
}
cursor.close();
td.close();
return taskList;
}
private void populateListView() {
ArrayAdapter<TaskList> adapter;
List<TaskList> taskList = new ArrayList<>();
//get the data and append to a list
taskList = mDatabaseHelper.getTaskList();
if (adapter == null) {
adapter = new ArrayAdapter<TaskList>(this, R.layout.list_item,taskList);
mListView.setAdapter(adapter);
} else {
adapter.clear();
adapter.addAll(taskList);
adapter.notifyDataSetChanged();
}
public class TaskList {
private String TaskName;
private String TaskDesc;
public TaskList(String taskName, String taskDesc) {
TaskName = taskName;
TaskDesc = taskDesc;
}
public String getTaskName() {
return TaskName;
}
public void setTaskName(String taskName) {
TaskName = taskName;
}
public String getTaskDesc() {
return TaskDesc;
}
public void setTaskDesc(String taskDesc) {
TaskDesc = taskDesc;
}
}
请查看更新的方法以及列表的新模型类