我的 Android 应用程序中有一个选项卡活动(A.java)。当我从活动 A 转到另一个活动(比如 B)并返回 A 时,我在选项卡中找不到任何内容

发布于 2024-11-29 13:46:04 字数 3226 浏览 1 评论 0原文

我在选项卡活动中有两个选项卡(比如 A)。
选项卡 1 显示了一些内容(此处无关紧要)。
选项卡 2 显示使用 SimpleCursorAdapter(工作完美)连接到 SQLite 数据库的 ListView。
现在我单击选项卡 2 的列表中的一个项目。这会将您带到另一个活动(例如 B)。
现在当我从活动 B 返回到活动 A(选项卡式活动)时。
我在那里没有看到 ListView。选项卡 2 为空。

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

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

    }}    

这是 TABBEd 活动的代码:-

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);



}

I have TWO tabs in Tab Activity(say A).
Tab 1 shows something(immaterial here).
Tab 2 shows a ListView connected to the SQLite Database with SimpleCursorAdapter(WORKS PERFECT).
Now i click on an item in LIST of Tab 2. This takes you to another activity(say B).
now when i go back from activity B to activity A(tabbed activity).
I dont see the ListView there. The Tab 2 is empty.

What to do?
Plz check the cursor adapter part of the code also because i receive a
"ERROR/Cursor(8736): Invalid statement in fillWindow()" IN logcat.

This is activity(java file) of TAB 2.

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);

    }}    

This is the code of the TABBEd ACTIVITY:-

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);



}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

七禾 2024-12-06 13:46:04

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

You call dbHelp.close(); after setting the adapter. But there should be open db connection for cursor adapter to work properly since it use cursor on every getView call. Move your closing db call to onDestroy. Or open it and set adapter in onStart and close then in onStop alternatively.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文