我使用类对象在列表视图中添加事件名称,开始和时间的生活事件,但仍然没有成功;我搜索了很多但找不到相关的答案。我是初学者,希望你能帮助我谢谢
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();
}
});
答案 0 :(得分:1)
我没有看到您将oneOf
的数据传递到oneOf
的位置/方式。当您将数据传递给适配器时,您需要获取它的引用,添加新项目并在ListView
上调用ArrayAdapter
。
答案 1 :(得分:0)
每当你对arraylist进行更改时,请调用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