2015-04-22 75 views
0

我使用類對象在列表視圖中添加了具有事件名稱,開始和時間的生命事件,但仍未成功;我有很多搜索,但無法找到相關答案。我是一個初學者希望大家幫我謝謝通過類對象動態地在列表中插入項目

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

你有什麼錯準確..添加Logcat並縮小你的問題。 – Dhina

回答

1

我看不出哪裏/你是如何傳遞一個備份的ListViewArrayAdapter數據。當您將數據傳遞給適配器時,則需要獲取該數據的引用,添加新項目並在ArrayAdapter上調用notifyDataSetChanged()

+0

adapter = new ArrayAdapter (getActivity(),R.layout.list_item, R.id.meeting_name); listView.setAdapter(adapter); –

+0

'List '被傳入了哪裏? – Emmanuel

+0

先生,我在列表中的單個項目中有3個視圖,所以我很困惑如何通過適配器在他們的特定視圖中傳遞數據。 –

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

+0

我不認爲他將'List'全部傳遞給'Adapter' ... – Emmanuel

+0

你是對的。我甚至不知道會議陣列是什麼。您可以通過在第三個參數上添加列表來將列表傳遞給適配器; adapter = new ArrayAdapter (getActivity(),R.layout.list_item, meetingList); – Genevieve