如何在android中的片段中刷新listview中的数据?

时间:2012-08-17 08:52:11

标签: android listview view fragment listadapter

我在片段类中有一个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;

}

private void messageList(){

    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;
}

1 个答案:

答案 0 :(得分:0)

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

myListAdapter.notifyDataSetChanged();

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

然后使用notifydatasetChanged();