通过 class 对象在列表中动态插入项目
Inserting items in a list dynamically by class object
我正在使用 class 对象在列表视图中添加带有事件名称、开始和时间的生活事件,但仍然没有成功;我搜索了很多但找不到相关答案。我是初学者希望你能帮助我谢谢
public class MeetingFragment extends Fragment {
List<Meetings> meetingList; //here Meetings is a Class
Meetings newMeeting;
ListView listView;
String meetingTag;
String[] meetings;
ArrayAdapter<Meetings> adapter;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View rootView = inflater.inflate(R.layout.fragment_meeting, container,
false);
newMeeting = new Meetings();
listView = (ListView) rootView.findViewById(R.id.list_meetings);
meetings = new String[] {};
adapter = new ArrayAdapter<Meetings>(getActivity(), R.layout.list_item,
R.id.meeting_name);
listView.setAdapter(adapter);
});
//This is custom dialog
ok.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
String tagMeeting = et.getText().toString();
String startTimeStr = strtTime.getText().toString();
String endTimeStr = endTime.getText().toString();
newMeeting.setMeetingName(tagMeeting);
newMeeting.setStartTime(startTimeStr);
newMeeting.setEndTime(endTimeStr);
//Display error on below line
meetingList.add(newMeeting);
addMeeting.dismiss();
}
});
我没有看到 where/how 您正在将支持 ListView
的数据传递给 ArrayAdapter
。当您将数据传递给适配器时,您需要获取它的引用、添加新项目并在 ArrayAdapter
上调用 notifyDataSetChanged()
。
每当您对数组列表进行更改时,调用 notifydatasetchanged 它将为您处理
ok.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
String tagMeeting = et.getText().toString();
String startTimeStr = strtTime.getText().toString();
String endTimeStr = endTime.getText().toString();
newMeeting.setMeetingName(tagMeeting);
newMeeting.setStartTime(startTimeStr);
newMeeting.setEndTime(endTimeStr);
//Display error on below line
meetingList.add(newMeeting);
adapter.notifyDataSetChanged();
addMeeting.dismiss();
}
});
如果这不起作用,请显示 logcat
我正在使用 class 对象在列表视图中添加带有事件名称、开始和时间的生活事件,但仍然没有成功;我搜索了很多但找不到相关答案。我是初学者希望你能帮助我谢谢
public class MeetingFragment extends Fragment {
List<Meetings> meetingList; //here Meetings is a Class
Meetings newMeeting;
ListView listView;
String meetingTag;
String[] meetings;
ArrayAdapter<Meetings> adapter;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View rootView = inflater.inflate(R.layout.fragment_meeting, container,
false);
newMeeting = new Meetings();
listView = (ListView) rootView.findViewById(R.id.list_meetings);
meetings = new String[] {};
adapter = new ArrayAdapter<Meetings>(getActivity(), R.layout.list_item,
R.id.meeting_name);
listView.setAdapter(adapter);
});
//This is custom dialog
ok.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
String tagMeeting = et.getText().toString();
String startTimeStr = strtTime.getText().toString();
String endTimeStr = endTime.getText().toString();
newMeeting.setMeetingName(tagMeeting);
newMeeting.setStartTime(startTimeStr);
newMeeting.setEndTime(endTimeStr);
//Display error on below line
meetingList.add(newMeeting);
addMeeting.dismiss();
}
});
我没有看到 where/how 您正在将支持 ListView
的数据传递给 ArrayAdapter
。当您将数据传递给适配器时,您需要获取它的引用、添加新项目并在 ArrayAdapter
上调用 notifyDataSetChanged()
。
每当您对数组列表进行更改时,调用 notifydatasetchanged 它将为您处理
ok.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
String tagMeeting = et.getText().toString();
String startTimeStr = strtTime.getText().toString();
String endTimeStr = endTime.getText().toString();
newMeeting.setMeetingName(tagMeeting);
newMeeting.setStartTime(startTimeStr);
newMeeting.setEndTime(endTimeStr);
//Display error on below line
meetingList.add(newMeeting);
adapter.notifyDataSetChanged();
addMeeting.dismiss();
}
});
如果这不起作用,请显示 logcat