0

我尝试从我创建的数据库中读取数据,并在自定义 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:“名称”和“金额”。但我仍然收到调试器错误。请帮忙。

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

Esoda [Android Application] DalvikVM[localhost:8603] Thread [<1> main] (Suspended (exception RuntimeException))
ActivityThread.performLaunchActivity(ActivityThread$ActivityRecord, Intent) 行:2663
ActivityThread.handleLaunchActivity(ActivityThread$ActivityRecord, Intent) 行: 2679 ActivityThread.access$2300(ActivityThread, ActivityThread$ActivityRecord, Intent) 行:125
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
ZygoteInit$MethodAndArgsCaller.run() line: 868
ZygoteInit.main(String[]) line: 626 NativeStart。 main(String[]) 行:不可用 [本机方法] 线程 [<6> Binder 线程 #2](运行)线程 [<5> Binder 线程 #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)
4

1 回答 1

0

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

于 2012-10-21T10:30:19.363 回答