Adam Fręśko
Adam Fręśko

Reputation: 1064

ListFragment - when adapter data change list positions are overridden and not added

I'm using SherlockListFragment(whichis the same as ListFragment) for displaying sms conversation list

Here is the code

   public static class TitlesFragment extends SherlockListFragment 
{
     static ConversationAdapter adapter;
     static List<String>    msgList;
     static Activity        activity;
     static ListView listView;

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

        DataGetters dataGetters = new DataGetters();
        activity = getActivity();
        msgList = dataGetters.getCONVERSATIONS(activity.getApplicationContext());

       adapter = new ConversationAdapter(activity, msgList);
       setListAdapter(adapter);

}

Code above prints all current sms conversations like this:

enter image description here

I'm refreshing adapter from code below by calling adapter.notifyDataSetChanged(); witch is called when new sms is received

public class ReceiverClass extends AsyncTask<String, Void, String> {

@Override
protected String doInBackground(String... params) {

    Thread.sleep(2000);

return null;
}

@Override
protected void onPostExecute(String result) {


      TitlesFragment.adapter.notifyDataSetChanged();

}

But fallowing code overides existing list view items insted of adding new list item position like this:

enter image description here

When i activity is recreated i get what i want but only then: example: enter image description here

Upvotes: 0

Views: 1316

Answers (2)

Mark Nguyen
Mark Nguyen

Reputation: 66

You are most likely incorrectly modifying the List that the ListAdapter is attached to.

Upvotes: 1

Adam Fręśko
Adam Fręśko

Reputation: 1064

So the problem was not me not modifiy adpater list with new position

Here is the code that neded to be added

  msgList= dataGetters.getCONVERSATIONS(activity.getApplicationContext());

  ConversationAdapter.msgList = msgList;

  TitlesFragment.adapter.notifyDataSetChanged();

Solved thanks to Mark Nguyen

Upvotes: 0

Related Questions