0

我在选项卡活动中有两个选项卡(比如 A)。
Tab 1 显示了一些东西(这里无关紧要)。
选项卡 2 显示了一个使用 SimpleCursorAdapter(WORKS PERFECT) 连接到 SQLite 数据库的 ListView。
现在我单击选项卡 2 的列表中的一个项目。这会将您带到另一个活动(例如 B)。
现在,当我从活动 B 回到活动 A(选项卡式活动)时。
我在那里看不到 ListView。选项卡 2 为空。

该怎么办?请检查代码的光标适配器部分,因为我在 logcat 中收到
“错误/光标(8736):fillWindow()中的无效语句”

这是 TAB 2 的活动(java 文件)。

public class ViewAll extends Activity implements OnItemClickListener {
TextView selection ;
Cursor cursor;
ListView lv1;

@Override
    public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);

        setContentView(R.layout.bday_list);
       DatabaseHelp dbHelp = new DatabaseHelp(getApplicationContext());
       Log.d("holla", "after database open!");
       lv1= (ListView)findViewById(R.id.List_of_bday);


       dbHelp.open();
        cursor =dbHelp.fetchAllContacts();
         Log.d("DOR DOR",cursor.getCount()+"");
        startManagingCursor(cursor);

                    // the desired columns to be bound
                    String[] columns = new String[] {DatabaseHelp.KEY_NAME, DatabaseHelp.KEY_DATE };
                    // the XML defined views which the data will be bound to
                    int[] to = new int[] { R.id.tv_name, R.id.tv_date };

                   // create the adapter using the cursor pointing to the desired data as well as the layout information
                    SimpleCursorAdapter mAdapter = new SimpleCursorAdapter(this, R.layout.list_each_row, cursor, columns, to);
                    // set this adapter as your ListActivity's adapter
                    lv1.setAdapter(mAdapter);

                   dbHelp.close();
lv1.setOnItemClickListener(this);

               }


    @Override
    public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
            long arg3) {
        // TODO Auto-generated method stub
        Toast.makeText(this, "clicked "+arg2, Toast.LENGTH_SHORT).show();
        Intent i=new Intent(this,ViewOne.class);
        startActivity(i);

    }}    

这是标签活动的代码:-

public class Today extends TabActivity {
    @Override
  public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
     setContentView(R.layout.main);
    Resources res=getResources();
    TabHost tabHost = getTabHost();  // The activity TabHost
    TabHost.TabSpec spec;  // Resusable TabSpec for each tab
    Intent intent;  // Reusable Intent for each tab

    // Create an Intent to launch an Activity for the tab (to be reused)
    intent = new Intent().setClass(this, TodayFinal.class);


    // Initialize a TabSpec for each tab and add it to the TabHost
    spec = tabHost.newTabSpec("today").setIndicator("View Today's Birthday",res.getDrawable(R.drawable.icon)).setContent(intent);

    tabHost.addTab(spec);

    // Do the same for the other tabs
    intent = new Intent().setClass(this, ViewAll.class);
//THIS TAKES YOU TO THE ABOVE MENTIONED ACTIVITY(TAB 2)

      spec = tabHost.newTabSpec("all").setIndicator("View All",
                      res.getDrawable(R.drawable.icon))
                  .setContent(intent);


    tabHost.addTab(spec);



}
4

1 回答 1

1

dbHelp.close();在设置适配器后调用。但是游标适配器应该有打开的数据库连接才能正常工作,因为它在每次getView调用时都使用游标。将您的关闭数据库调用移动到onDestroy. 或者打开它并设置适配器,onStart然后再关闭onStop

于 2011-08-16T14:21:18.443 回答