[英]Reading data from database and binding them to custom ListView
I try to read data from a database i have made and to show some of the data in a row at a custom ListView. 我尝试从我制作的数据库中读取数据,并在自定义ListView的行中显示一些数据。 I can not understand what is my mistake. 我不明白我的错误是什么。
This is my code: 这是我的代码:
public class EsodaMainActivity extends Activity
{
public static final String ROW_ID = "row_id"; //Intent extra key
private ListView esodaListView; // the ListActivitys ListView
private SimpleCursorAdapter esodaAdapter; // adapter for ListView
DatabaseConnector databaseConnector = new DatabaseConnector(EsodaMainActivity.this);
// called when the activity is first created
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_esoda_main);
esodaListView = (ListView)findViewById(R.id.esodaList);
esodaListView.setOnItemClickListener(viewEsodaListener);
databaseConnector.open();
//Cursor cursor= databaseConnector.query("esoda", new String[]
// {"name", "amount"}, null,null,null);
Cursor cursor=databaseConnector.getAllEsoda();
startManagingCursor(cursor);
// map each esoda to a TextView in the ListView layout
// The desired columns to be bound
String[] from = new String[] {"name","amount"}; // built an String array named "from"
//The XML defined views which the data will be bound to
int[] to = new int[] { R.id.esodaTextView, R.id.amountTextView}; // built an int array named "to"
// EsodaMainActivity.this = The context in which the ListView is running
// R.layout.esoda_list_item = Id of the layout that is used to display each item in ListView
// null =
// from = String array containing the column names to display
// to = Int array containing the column names to display
esodaAdapter = new SimpleCursorAdapter (this, R.layout.esoda_list_item, cursor, from, to);
esodaListView.setAdapter(esodaAdapter); // set esodaView's adapter
} // end of onCreate method
@Override
protected void onResume()
{
super.onResume(); // call super's onResume method
// create new GetEsodaTask and execute it
// GetEsodaTask is an AsyncTask object
new GetEsodaTask().execute((Object[]) null);
} // end of onResume method
// onStop method is executed when the Activity is no longer visible to the user
@Override
protected void onStop()
{
Cursor cursor= esodaAdapter.getCursor(); // gets current cursor from esodaAdapter
if (cursor != null)
cursor.deactivate(); // deactivate cursor
esodaAdapter.changeCursor(null); // adapter now has no cursor (removes the cursor from the CursorAdapter)
super.onStop();
} // end of onStop method
// this class performs db query outside the GUI
private class GetEsodaTask extends AsyncTask<Object, Object, Cursor>
{
// we create a new DatabaseConnector obj
// EsodaMainActivity.this = Context
DatabaseConnector databaseConnector = new DatabaseConnector(EsodaMainActivity.this);
// perform the db access
@Override
protected Cursor doInBackground(Object... params)
{
databaseConnector.open();
// get a cursor containing call esoda
return databaseConnector.getAllEsoda();
// the cursor returned by getAllContacts() is passed to method onPostExecute()
} // end of doInBackground method
// here we use the cursor returned from the doInBackground() method
@Override
protected void onPostExecute(Cursor result)
{
esodaAdapter.changeCursor(result); // set the adapter's Cursor
databaseConnector.close();
} // end of onPostExecute() method
} // end of GetEsodaTask class
// creates the Activity's menu from a menu resource XML file
@Override
public boolean onCreateOptionsMenu(Menu menu)
{
super.onCreateOptionsMenu(menu);
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.esoda_menu, menu); // inflates(εμφυσώ) esodamainactivity_menu.xml to the Options menu
return true;
} // end of onCreateOptionsMenu() method
//handles choice from options menu - is executed when the user touches a MenuItem
@Override
public boolean onOptionsItemSelected(MenuItem item)
{
// creates a new Intent to launch the AddEditEsoda Activity
// EsodaMainActivity.this = Context from which the Activity will be launched
// AddEditEsoda.class = target Activity
Intent addNewEsoda = new Intent(EsodaMainActivity.this, AddEditEsoda.class);
startActivity(addNewEsoda);
return super.onOptionsItemSelected(item);
} // end of method onPtionsItemSelected()
// event listener that responds to the user touching a esoda's name in the ListView
OnItemClickListener viewEsodaListener = new OnItemClickListener()
{
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3)
{
// create an intent to launch the ViewEsoda Activity
Intent viewEsoda = new Intent(EsodaMainActivity.this, ViewEsoda.class);
// pass the selected esoda's row ID as an extra with the Intent
viewEsoda.putExtra(ROW_ID, arg3);
startActivity(viewEsoda); // start viewEsoda.class Activity
} // end of onItemClick() method
}; // end of viewEsodaListener
} // end of EsodaMainActivity class
The statement: Cursor cursor=databaseConnector.getAllEsoda();
该语句: Cursor cursor=databaseConnector.getAllEsoda();
queries all data (columns) From the data I want to show at my custom ListView 2 of them: "name" and "amount". 从我想在自定义ListView中显示的数据中查询所有数据(列)2:“名称”和“金额”。 But I still get a debugger error. 但是我仍然收到调试器错误。 Please help. 请帮忙。
You mean that messages from debugger? 您的意思是来自调试器的消息?
Esoda [Android Application] DalvikVM[localhost:8603] Thread [<1> main] (Suspended (exception RuntimeException)) Esoda [Android应用程序] DalvikVM [localhost:8603]线程[<1> main](已暂停(RuntimeException异常))
ActivityThread.performLaunchActivity(ActivityThread$ActivityRecord, Intent) line: 2663 ActivityThread.performLaunchActivity(ActivityThread $ ActivityRecord,Intent)行:2663
ActivityThread.handleLaunchActivity(ActivityThread$ActivityRecord, Intent) line: 2679 ActivityThread.access$2300(ActivityThread, ActivityThread$ActivityRecord, Intent) line: 125 ActivityThread.handleLaunchActivity(ActivityThread $ ActivityRecord,Intent)行:2679 ActivityThread.access $ 2300(ActivityThread,ActivityThread $ ActivityRecord,Intent)行:125
ActivityThread$H.handleMessage(Message) line: 2033 ActivityThread $ H.handleMessage(Message)行:2033
ActivityThread$H(Handler).dispatchMessage(Message) line: 99 Looper.loop() line: 123 ActivityThread.main(String[]) line: 4627 Method.invokeNative(Object, Object[], Class, Class[], Class, int, boolean) line: not available [native method] Method.invoke(Object, Object...) line: 521 ActivityThread $ H(Handler).dispatchMessage(Message)行:99 Looper.loop()行:123 ActivityThread.main(String [])行:4627 Method.invokeNative(Object,Object [],Class,Class [],Class ,int,boolean)行:不可用[本机方法] Method.invoke(Object,Object ...)行:521
ZygoteInit$MethodAndArgsCaller.run() line: 868 ZygoteInit $ MethodAndArgsCaller.run()行:868
ZygoteInit.main(String[]) line: 626 NativeStart.main(String[]) line: not available [native method] Thread [<6> Binder Thread #2] (Running) Thread [<5> Binder Thread #1] (Running) ZygoteInit.main(String [])行:626 NativeStart.main(String [])行:不可用[本机方法]线程[<6> Binder线程#2](运行中)线程[<5> Binder线程#1] (运行中)
Here is the LogCat: 这是LogCat:
10-20 21:53:19.903: W/ActivityThread(1973): Application development.nk.esoda is waiting for the debugger on port 8100...
10-20 21:53:19.933: I/System.out(1973): Sending WAIT chunk
10-20 21:53:19.943: I/dalvikvm(1973): Debugger is active
10-20 21:53:19.953: I/System.out(1973): Debugger has connected
10-20 21:53:19.953: I/System.out(1973): waiting for debugger to settle...
10-20 21:53:20.215: I/System.out(1973): waiting for debugger to settle...
10-20 21:53:20.478: I/System.out(1973): waiting for debugger to settle...
10-20 21:53:20.673: I/System.out(1973): waiting for debugger to settle...
10-20 21:53:20.890: I/System.out(1973): waiting for debugger to settle...
10-20 21:53:21.133: I/System.out(1973): waiting for debugger to settle...
10-20 21:53:21.333: I/System.out(1973): waiting for debugger to settle...
10-20 21:53:21.573: I/System.out(1973): waiting for debugger to settle...
10-20 21:53:21.790: I/System.out(1973): waiting for debugger to settle...
10-20 21:53:21.995: I/System.out(1973): waiting for debugger to settle...
10-20 21:53:22.198: I/System.out(1973): debugger has settled (1497)
Ok solved the problem... When i called the getAllContacts() method i didnt read alla columns. 好了,解决了问题...当我调用getAllContacts()方法时,我没有读取alla列。 I changed that and added more columns to read and then i could map these values to my custom ListView 我进行了更改,并添加了更多列以进行读取,然后可以将这些值映射到我的自定义ListView
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.