android listview数据不刷新数据来自服务器

时间:2016-05-22 05:52:11

标签: java android listview

我有一个布局,其中有edit_text按钮和list_view我在编辑文本中输入文本,当我点击按钮数据时会在list_view中显示问题,只有当我点击后退按钮并再次打开list_view时,才会显示该按钮数据显示 。我在list_view刷新时遇到问题我希望当我发送数据时list_view自动更新并向我显示这里的数据是list_view的代码。

protected void showList() {
    try {
        JSONObject jsonObj = new JSONObject(myJSON);
        peoples = jsonObj.getJSONArray(TAG_RESULTS);

        for (int i = 0; i < peoples.length(); i++) {
            JSONObject c = peoples.getJSONObject(i);
            //String sender_email = c.getString(TAG_SENDER_EMAIL);
           // String reciver_email = c.getString(TAG_RECIVER_EMAIL);
            String data = c.getString(TAG_DATA);
            //       String email = c.getString(TAG_EMAIL);
           //  e1.setText(reciver_email);

            HashMap<String, String> user_data = new HashMap<String, String>();

            //           persons.put(TAG_ID,id);
            user_data.put(TAG_DATA, data);
            //      users.put(TAG_EMAIL,email);

            personList.add(user_data);
        }

        ListAdapter adapter = new SimpleAdapter(
                DataSendActivity.this, personList, R.layout.layout_chat,
                new String[]{TAG_DATA},
                new int[]{R.id.data}

        );

        list.setAdapter(adapter);

    } catch (JSONException e) {
        e.printStackTrace();
    }

}

1 个答案:

答案 0 :(得分:0)

使用下面的代码:

protected void showList() {

    ListAdapter adapter = new SimpleAdapter(
            DataSendActivity.this, personList, R.layout.layout_chat,
            new String[]{TAG_DATA},
            new int[]{R.id.data}

    );

    list.setAdapter(adapter);

try {
    JSONObject jsonObj = new JSONObject(myJSON);
    peoples = jsonObj.getJSONArray(TAG_RESULTS);

    for (int i = 0; i < peoples.length(); i++) {
        JSONObject c = peoples.getJSONObject(i);
        //String sender_email = c.getString(TAG_SENDER_EMAIL);
       // String reciver_email = c.getString(TAG_RECIVER_EMAIL);
        String data = c.getString(TAG_DATA);
        //       String email = c.getString(TAG_EMAIL);
       //  e1.setText(reciver_email);

        HashMap<String, String> user_data = new HashMap<String, String>();

        //           persons.put(TAG_ID,id);
        user_data.put(TAG_DATA, data);
        //      users.put(TAG_EMAIL,email);

        personList.add(user_data);
    }

 adapter.notifyDataSetChanged();

} catch (JSONException e) {
    e.printStackTrace();
}

}