我正在开发一个应用程序,其中有三个带有片段的平移布局,每个片段都是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);
}
}