1

I got 2 different activities that I change between. But when I go to the second activity and press back the ListView isn't shown. I can't seem to find what's the problem.

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    currentLayout = R.layout.activity_main;
    db = new TodoDbHelper(this);
    todos = db.getAllTodos();
    nrOfTodos = todos.size();

    //For ListView
    mListView = (ListView) findViewById(R.id.gradientBackground);
    mAdapter = new TodoAdapter<String>(this, R.layout.text_view_item_default);
    mListView.setAdapter(mAdapter);

    //Calculate height of screen
    Display display = getWindowManager().getDefaultDisplay();
    Point size = new Point();
    display.getSize(size);
    int width = size.x;
    int height = size.y;
    listItemSize = height/10;
    SCREEN_WIDTH = width;        

    createTodoItems();

}

public void createTodoItems() {
    refresh();
    mAdapter.clear();
    for(int i = 0; i < nrOfTodos; i++) {
        String title = todos.get(i).getTitle();

        mAdapter.add(title);
        //System.out.println(title);
        //System.out.println(mAdapter);

    }
    mListView.setAdapter(mAdapter);         
    ImageButton b = (ImageButton) findViewById(R.id.addButton);
    b.bringToFront();
}

public void refresh() {
    todos = db.getAllTodos();
    nrOfTodos = todos.size();
}

@Override
public void onBackPressed() {
    System.out.println("" + this.findViewById(android.R.id.content).getRootView());
    System.out.println("" + currentLayout);
    if(R.layout.activity_add_todo == currentLayout) {
        setContentView(R.layout.activity_main);
        currentLayout = R.layout.activity_main;

        createTodoItems();
        Log.d("onBackPressed", "We're going back.");
    }

}

I do the same "createTodoItems()" both time but then second time it doesn't seem to matter.

Also it seems that mAdapter is the same after the second createTodoItems(). The values in mAdapter is still the same.

Same problem when adding a new item. It just wont render anything from the ListView.

Clanket
  • 1,259
  • 2
  • 11
  • 16
  • This isn't the right way to have 2 activities and press the back button to go back to the previous one...you need to be using intents. – Alex K Nov 26 '14 at 00:02
  • This is because onBackPressed() you are re-setting contentview and performing createTodoItems() without re-setting mListView and mAdapter again like you did onCreate() - I am assuming you are overriding onBackPressed() so as not to finish activity. – Wildroid Nov 26 '14 at 01:37
  • Yes that's correct. Now I don't even know what I'm doing anymore. – Clanket Nov 26 '14 at 02:19
  • @Clanket If you have the list in activity A and you go to activity B to update the list (which is in activity A), then all you have to do is override onRestart() or onResume() in activity A to refresh the list once activity B is closed. Or you can have a static refresh list method in Activity A and call it in onDestroy() or onStop() of Activity B. If you can post code of both activities, I can provide answer. -- I am not sure why you are setting contentview twice same activity? – Wildroid Nov 26 '14 at 05:08
  • @wildroid I'm actually trying to fix it with fragments but I've ran into some trouble. Is both screens supposed to be different fragments then? I tried fixing it like [link](http://developer.android.com/training/basics/fragments/fragment-ui.html) with an main activity that is just an empty FrameLayout and adding these with a transaction/commit but then I need to set the main screen and get a listView from the XML that is related to my maintodofragment but since my activity is just an empty frameLayout it returns null. – Clanket Nov 26 '14 at 10:31

1 Answers1

1

Try to add this function notifyDataSetChanged(), may it help:

public void createTodoItems() {
   refresh();
   mAdapter.clear();
   for(int i = 0; i < nrOfTodos; i++) {
      title = todos.get(i).getTitle();

      mAdapter.add(title);
      //System.out.println(title);
      //System.out.println(mAdapter);

   }
   mListView.setAdapter(mAdapter);  
   mAdapter.notifyDataSetChanged();   
   ImageButton b = (ImageButton) findViewById(R.id.addButton);
   b.bringToFront();
}
Fuong Lee
  • 175
  • 4