【问题标题】:How to refresh data in listview in a fragment in android?如何在android的片段中刷新listview中的数据?
【发布时间】:2012-08-17 08:52:10
【问题描述】:

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

【问题讨论】:

    标签: android listview view fragment listadapter


    【解决方案1】:

    在您要刷新数据的地方使用此代码。

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

    然后使用 notifydatasetChanged();

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-04-09
      • 1970-01-01
      • 1970-01-01
      • 2020-07-31
      • 1970-01-01
      • 2016-03-04
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多