Search code examples
androidlistviewandroid-activitytabssimplecursoradapter

i have a Tab Activity(A.java) in my android application. when i go from Activity A to another(say B) and back to A i find nothing in tab


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



}

Solution

  • 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.