我正在开发一个应用程序,其中有三个包含片段的平移布局,每个片段都是ListFragment
。我正在通过interface
成功与他们进行通信,但我无法使用新项目更新ListView
。以下是我需要更新的ListView
代码:
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
}
}
如何在运行时经常更新ListView
?
更新
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);
}
}
答案 0 :(得分:2)
试试这个:
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();
答案 1 :(得分:1)
通过适配器提供数据后,您需要调用 mAdapter.notifyDataSetChanged(); 它应该更新..