Kaidul
Kaidul

Reputation: 15875

How to update ListFragment on Runtime

I am developing an app in which there are three pan layout with fragments and each fragments are ListFragment. I am communicating between them via interface successfully but I am not able to update the ListView with new items. Here is my code of the ListView which need to be updated:

public class SubChaptersListFragment extends SherlockListFragment {

    public interface OnSubChapterSelectListener {
        public void onSubChapterSelected(int position);
    }

        @Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    mAdapter = null;
}

    @Override
    public void onStart() {
        super.onStart();
        if (getFragmentManager().findFragmentById(
                R.id.sub_sub_category_fragment) != null) {
            getListView().setChoiceMode(ListView.CHOICE_MODE_SINGLE);
        }
    }

        // this function will be invoked from another fragment (succesffully invoked)
    public void updateList(int position) {
        Log.d("success", "" + position); // position is successfully passed
        int layout = Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB ? android.R.layout.simple_list_item_activated_1
                : android.R.layout.simple_list_item_1;
        ArrayList<String> items = new ArrayList<String>();
//      Chapter instance = CompetitiveProgramming.chapterList.get(position);
        for (int i = 0; i < CompetitiveProgramming.chapterList.get(position).subchapterList.size(); i++) {
            items.add(CompetitiveProgramming.chapterList.get(position).subchapterList.get(i).subChapterTitle);
        }
                // everything is okay till now
                setListAdapter(new ArrayAdapter<String>(getSherlockActivity(), layout, items)); // is not working in this line and crashed
    }
}

How to update the ListView frequently in runtime?

Update:

public class SubChaptersListFragment extends SherlockListFragment {
    OnSubChapterSelectListener mCallback; 
    public interface OnSubChapterSelectListener {
        public void onSubChapterSelected(int prev, int position);
    }
    ArrayAdapter<String> mAdapter;
    int mPosition;

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        ArrayList<String> items = new ArrayList<String>();
        int layout = Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB ? android.R.layout.simple_list_item_activated_1
                : android.R.layout.simple_list_item_1;
        Chapter instance = CompetitiveProgramming.chapterList.get(0);
        for (int i = 0; i < instance.subchapterList.size(); i++) {
            items.add(instance.subchapterList.get(i).subChapterTitle);
        }
        mAdapter = new ArrayAdapter<String>(getSherlockActivity(), layout, items);
        setListAdapter(mAdapter);
    }

    @Override
    public void onStart() {
        super.onStart();
        if (getFragmentManager().findFragmentById(
                R.id.sub_sub_category_fragment) != null) {
            getListView().setChoiceMode(ListView.CHOICE_MODE_SINGLE);
        }
    }

    public void updateList(int position) {
        mPosition = position;
        mAdapter.clear();
        ArrayList<String> items = new ArrayList<String>();
        for (int i = 0; i < CompetitiveProgramming.chapterList.get(position).subchapterList.size(); i++) {
            items.add(CompetitiveProgramming.chapterList.get(position).subchapterList.get(i).subChapterTitle);
        }
        mAdapter.notifyDataSetChanged();
    }

    @Override
    public void onListItemClick(ListView l, View v, int position, long id) {
        mCallback.onSubChapterSelected(mPosition, position);
        getListView().setItemChecked(position, true);
    }

}

Upvotes: 0

Views: 5431

Answers (2)

Allen Z.
Allen Z.

Reputation: 350

Try this:

mAdapter.clear();
for (int i = 0; i < CompetitiveProgramming.chapterList.get(position).subchapterList.size(); i++) {
        mAdapter.add(CompetitiveProgramming.chapterList.get(position).subchapterList.get(i).subChapterTitle);
}
mAdapter.notifyDataSetChanged();

Upvotes: 2

Sushil
Sushil

Reputation: 8488

After feeding data through adapter, you need to call mAdapter.notifyDataSetChanged(); It should update then..

Upvotes: 1

Related Questions