android listview刷新问题数据来自服务器

时间:2016-05-21 16:31:39

标签: android listview server

我有两个问题,一个是listview,它有来自服务器的数据。现在的问题是,这个listview不刷新数据我怎么能解决我的问题。我使用意图刷新我的数据数据刷新正确但在同一页面我有文本视图来自其他活动当我使用意图textview数据消失请告诉我怎么能解决它。 这是显示数据的列表视图。

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

        );

        list.setAdapter(adapter);

这是文本视图,其文本来自其他活动

Bundle extras = getIntent().getExtras();
    if (extras != null) {

        Remail = extras.getString("email");
        e1.setText(extras.getString("email"));

感谢

 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);
               // e2.setText(sender_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();
        }

    }

从服务器获取数据

public void getData() {
    class GetDataJSON extends AsyncTask<String, Void, String> {
        //String recID = ;
        //String userID = email;
        @Override
        protected String doInBackground(String... params) {
            DefaultHttpClient httpclient = new DefaultHttpClient(new BasicHttpParams());
            HttpPost httppost = new HttpPost("http://samplechatapp.gear.host/myphpfile.php");

            // Depends on your web service
            httppost.setHeader("Content-type", "application/json");

            InputStream inputStream = null;
            String result = null;
            try {
                HttpResponse response = httpclient.execute(httppost);
                HttpEntity entity = response.getEntity();

                inputStream = entity.getContent();
                // json is UTF-8 by default
                BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"), 8);
                StringBuilder sb = new StringBuilder();

                String line = null;
                while ((line = reader.readLine()) != null) {
                    sb.append(line + "\n");
                }
                result = sb.toString();
                Log.i("",result);
            } catch (Exception e) {
                // Oops
            } finally {
                try {
                    if (inputStream != null) inputStream.close();
                } catch (Exception squish) {
                }
            }
            return result;
        }

        @Override
        protected void onPostExecute(String result) {
            myJSON = result;
            showList();
        }
    }
    GetDataJSON g = new GetDataJSON();
    g.execute();
}

0 个答案:

没有答案