本文介绍了Android:使用SimpleCursorAdapter从数据库获取数据到ListView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 我编程一个Android应用程序,应该使用数据库来存储数据和从中读取。使用本教程(在archive.org上)我得到了应用程序来创建数据库,我可以创建新的条目,但是,我不知道,如何读取数据库在ListView中获取存储的数据。我知道在这个网站有很多类似的问题,但似乎没有一个适用的方式,数据库从教程的工作。I am programming an android app that should use a database to store data and read from it. Using this tutorial (on archive.org) I got the app to create a database and I'm able to create new entries, however, I don't know, how to read the database to get the stored data in a ListView. I know there are many similar questions on this website but it seems none of them apply to the way, the database from the tutorial works.代码:import java.util.Calendar;import maturarbeit.nicola_pfister.studenttools.database.DBAdapter;import android.app.AlertDialog.Builder;import android.app.ListActivity;import android.content.DialogInterface;import android.content.DialogInterface.OnClickListener;import android.database.Cursor;import android.os.Bundle;import android.support.v4.widget.SimpleCursorAdapter;import android.view.Menu;import android.view.MenuInflater;import android.view.MenuItem;import android.widget.ListView;public class Marks extends ListActivity {DBAdapter db = new DBAdapter(this);@Overridepublic void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.marks);}@Overrideprotected void onPause() { super.onPause(); db.close();}@Overrideprotected void onResume() { super.onResume(); db.open(); getData();}@SuppressWarnings("deprecation")private void getData() { SimpleCursorAdapter adapter = new SimpleCursorAdapter(this, android.R.layout.simple_list_item_1, db.getAllMarks(), new String[] { "value" }, new int[] { android.R.id.text1 }); ListView listView = (ListView) findViewById(R.id.marks_list); listView.setAdapter(adapter);}@Overridepublic boolean onCreateOptionsMenu(Menu menu) { MenuInflater inflater = getMenuInflater(); inflater.inflate(R.menu.marks, menu); return true;}public boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()) { case R.id.menu_add: Calendar cal = Calendar.getInstance(); int day = cal.get(Calendar.DAY_OF_MONTH); int month = cal.get(Calendar.MONTH); final String date = day + "." + month; Builder builder = new Builder(this); builder .setTitle(R.string.dialog_addmarks) .setItems(R.array.markslist, new OnClickListener() { public void onClick(DialogInterface dialog, int which) { @SuppressWarnings("unused") long id; String selection = getResources().getStringArray(R.array.markslist)[which]; id = db.insertMark(date, "Default", selection); } }) .show(); getData(); break; case R.id.menu_delete: //Deleting function yet to be implemented. break; } return super.onOptionsItemSelected(item);}} 编辑:ListView ID错误,因为它必须 The ListView ID was wrong since it had to be android:list.推荐答案使用你链接到的教程中的数据库格式,每一行都有一个 _id , isbn , title / code>。现在让我们假设你想在ListView中显示每个标题:Using the database format in the tutorial that you linked to, every row has an _id, isbn, title, and publisher. Now let's assume that you want to display every title in a ListView:db = new DBAdapter(this);SimpleCursorAdapter adapter = new SimpleCursorAdapter(this, android.R.layout.simple_list_item_1, db.getAllTitles(), new String[] { "title" }, new int[] { android.R.id.text1 });ListView listView = (ListView) findViewById(R.id.list);listView.setAdapter(adapter);(你不需要自己循环光标,适配器为你工作! )(You don't need to loop through the Cursor yourself, an adapter does this work for you!)你的SimpleCursorAdapter构造函数中最后两个参数是你缺少的。它们是from和to参数:The last two parameters in your SimpleCursorAdapter constructor are what you are missing. They are the "from" and "to" parameters: 我们要获取存储在列名称中的每本书的名称 title ,这是我们获取信息from的地方。 接下来我们需要告诉它在哪里去: android.R.id.text1 是 android.R.layout.simple_list_item_1 布局。 (您可以深入了解SDK,并自行查看simple_list_item_1.xml文件,或者暂时只信任我。)We want to get the name of each book which is stored in the column name title, this is where we get the information "from".Next we need to tell it where "to" go: android.R.id.text1 is a TextView in the android.R.layout.simple_list_item_1 layout. (You can dig through your SDK and see the simple_list_item_1.xml file yourself or just trust me for the moment.) from和to参数是数组,因为我们可以传递多个信息列,请尝试这个适配器:Now both the "from" and "to" parameters are arrays because we can pass more than one column of information, try this adapter as well:SimpleCursorAdapter adapter = new SimpleCursorAdapter(this, android.R.layout.simple_list_item_2, db.getAllTitles(), new String[] { "title", "publisher" }, new int[] { android.R.id.text1, android.R.id.text2 });使用此适配器,数据库中的图书将按标题显示,然后显示发布者。我们要做的是使用一个布局 android.R.layout.simple_list_item_2 ,它需要两个字段,并定义哪些列到哪个TextViews。With this adapter the books in their database will displayed by title, then publisher. All we had to do is use a layout android.R.layout.simple_list_item_2 that takes two fields and define which columns go to which TextViews.我希望有一点帮助。 最后评论在我的头顶部,在添加新数据后刷新ListView:Off the top of my head, to refresh the ListView after adding new data try this:public void onClick(DialogInterface dialog, int which) { String selection = getResources().getStringArray(R.array.markslist)[which]; db.insertMark(date, "Default", selection); cursor.requery(); adapter.notifyDataSetChanged();}您必须定义 adapter 并为 cursor 创建一个变量,但很简单:You'll have to define adapter and create a variable for cursor but that's simple:public class Marks extends ListActivity { SimpleCursorAdapter adapter; Cursor cursor; DBAdapter db = new DBAdapter(this); ...并相应地更改getData():And change getData() accordingly:private void getData() { cursor = db.getAllMarks(); adapter = new SimpleCursorAdapter(this, android.R.layout.simple_list_item_1, cursor, new String[] { "value" }, new int[] { android.R.id.text1 }); ...}祝你好运! 这篇关于Android:使用SimpleCursorAdapter从数据库获取数据到ListView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
05-26 22:43
查看更多