我有一个包含许多片段的主要活动,其中一个包含一个列表视图,带有适配器homeworkListAdapter
,这个适配器确实显示任何以homeworkItems
. 我的活动打开了另一个活动(HomeworkAddActivity
,它从用户那里获取一个字符串并将其放入一个数组homeworkItems
中,这一切都有效,因为我已经独立检查了每个步骤。但是,当我尝试使用这个新项目(使用.notifyDataSetChanged
)更新列表视图时没有更新,我已经尝试修复此问题 3-4 小时,请帮助。
代码
MainActivity
public static ArrayAdapter<String> homeworkListAdapter;
public static ArrayList<String> homeworkItems;
....
protected void onCreate(Bundle savedInstanceState) {
....
homeworkItems = new ArrayList<String>();
homeworkListAdapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, homeworkItems);
....
public static class HomeworkListViewFragment extends Fragment {
....
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(
R.layout.fragment_homework_listview, container, false);
homeworkListView = (ListView) rootView
.findViewById(R.id.homework_listview);
homeworkListView.setAdapter(homeworkListAdapter);
return rootView;
}
HomeworkAddActivity
public class HomeworkAddActivity extends ActionBarActivity {
....
public void buttonAddHomework(View v) {
NavUtils.navigateUpFromSameTask(this);
EditText editText = (EditText) findViewById(R.id.text_new_homework_name);
String homeworkItem = (String) editText.getText().toString();
if (!(TextUtils.isEmpty(homeworkItem))) {
//MainActivity.homeworkList.addItem(homeworkItem);
MainActivity.homeworkItems.add(homeworkItem);
MainActivity.homeworkListAdapter.notifyDataSetChanged();
}
}
}