从数据库读取数据并将它们绑定到自定义ListView

时间:2012-10-20 16:54:35

标签: android database listview

我尝试从我创建的数据库中读取数据,并在自定义ListView中连续显示一些数据。 我无法理解我的错误。

这是我的代码:

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

声明:Cursor cursor=databaseConnector.getAllEsoda(); 查询所有数据(列) 从我想要在我的自定义ListView 2中显示的数据:“name”和“amount”。 但我仍然遇到调试器错误。 请帮忙。

你的意思是来自调试器的消息?

  

Esoda [Android应用程序] DalvikVM [localhost:8603]线程[&lt; 1&gt;   main](Suspended(例外RuntimeException))
            ActivityThread.performLaunchActivity(ActivityThread $ ActivityRecord,   意图)行:2663
            ActivityThread.handleLaunchActivity(ActivityThread $ ActivityRecord,   意图)行:2679 ActivityThread.access $ 2300(ActivityThread,   ActivityThread $ ActivityRecord,Intent)line:125
            ActivityThread $ H.handleMessage(消息)行:2033
            ActivityThread $ H(Handler).dispatchMessage(Message)行:99             Looper.loop()行:123 ActivityThread.main(String [])行:   4627 Method.invokeNative(Object,Object [],Class,Class [],Class,   int,boolean)line:not available [native method]             Method.invoke(Object,Object ...)行:521
            ZygoteInit $ MethodAndArgsCaller.run()行:868
            ZygoteInit.main(String [])行:626 NativeStart.main(String [])   line:not available [native method] Thread [&lt; 6&gt; Binder Thread#2]   (运行)线程[&lt; 5&gt; Binder Thread#1](正在运行)

这是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)

1 个答案:

答案 0 :(得分:0)

好的解决了这个问题...... 当我调用getAllContacts()方法时,我没有读过alla列。 我更改了它并添加了更多列来读取,然后我可以将这些值映射到我的自定义ListView