0

我在片段类中有一个 Listview,每次添加新数据时都需要更新它。新数据未反映在列表中

我的片段类:

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.messagelist, null);
        listView = (ListView) view.findViewById(R.id.listOfMessages);

    messageList();
    return view;

}

私人无效消息列表(){

    msgRecordList.addAll((AppService.getInstance()).getMsgId());
    if (!(msgRecordList.equals(""))) {


        msgListAdapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1, addMessage());  //addMessage adds data to the list


         listView.setAdapter(msgListAdapter);
         ((BaseAdapter) msgListAdapter).notifyDataSetChanged();


        listView.setVisibility(View.VISIBLE);
    } else {
        System.out.println(" i am in else");
    }
    return listView;

}


protected List<String> addMessage() {

    List<String> msgList = new ArrayList<String>();
    int size = AppService.getInstance().getMsgId().size();
    System.out.println("sizeee:::" + size);
    for (int i = 0; i < size; i++) {
        msgList.add(AppService.getInstance().getMsgTimeStamp().get(i) + " : "
                + AppService.getInstance().getMsgDescription().get(i));
    }

    return msgList;
}
4

1 回答 1

0

使用此代码,您要在其中刷新数据。

myListAdapter.notifyDataSetChanged();

List<object> list= addMesage();
listview.setAdaptor("**", "***", list);

然后使用 notifydatasetChanged();

于 2012-08-17T09:22:29.720 回答