有没有办法清除在onItemSelectedListener下从php接收的listview上显示的数据?

时间:2015-07-02 16:54:10

标签: php android listview

我正在做一个Android应用程序,显示数据(从php接收)白天(我有一个微调器)。

现在当我选择第二天时,第一天的数据会保留,无论如何要在显示第二天数据之前清除它吗?

示例:

=When I click first day=

[Spinner] - day 1

[listview] day 1 data



=When I click second day= 

[Spinner] - day 2 

[listview] day 1 data  <--- need to delete this

[listview] day 2 data

activity.java

//code for spinner inside onCreate()
Spinner spinner = (Spinner) findViewById(R.id.day_spinner);
    ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
            this, R.array.day_spinner_items, android.R.layout.simple_spinner_item);
    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    spinner.setAdapter(adapter);

    spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {

            switch (position) {
                case 0:
                    day="1";
                    getData();
                    break;
                case 1:
                    day="2";
                    getData();
                    break;
            }
        }



//display data
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 code = c.getString(TAG_CODE);
            String subject = c.getString(TAG_SUBJECT);
            String day = c.getString(TAG_DAY);
            String start = c.getString(TAG_START);
            String end = c.getString(TAG_END);
            String location = c.getString(TAG_LOCATION);

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

            persons.put(TAG_CODE, code);
            persons.put(TAG_SUBJECT, subject);
            persons.put(TAG_DAY, day);
            persons.put(TAG_START, start);
            persons.put(TAG_END, end);
            persons.put(TAG_LOCATION, location);

            personList.add(persons);
        }

        ListAdapter adapter = new SimpleAdapter(
                TimeActivity.this, personList, R.layout.time_listitem,
                new String[]{TAG_CODE, TAG_SUBJECT, TAG_DAY, TAG_START, TAG_END, TAG_LOCATION},
                new int[]{R.id.code, R.id.subject, R.id.day, R.id.start, R.id.end, R.id.location}
        );

        list.setAdapter(adapter);
    } catch (JSONException e) {
        e.printStackTrace();
    }

}

//get data from php
public void getData() {
    class GetDataJSON extends AsyncTask<String, Void, String> {

        @Override
        protected String doInBackground(String... params) {


            // Depends on your web service
            //httppost.setHeader("Content-type", "application/json");
            List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
            nameValuePairs.add(new BasicNameValuePair("day", day));

            InputStream inputStream = null;
            String result = null;
            try {
                HttpClient httpclient = new DefaultHttpClient();
                HttpPost httppost = new HttpPost("http://192.168.1.12/android/fetchtime.php");
                httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
                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();
            } catch (ClientProtocolException e) {
                e.printStackTrace();
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
            return result;
        }

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

1 个答案:

答案 0 :(得分:1)

更新您的信息数组,然后调用适配器的notifyDataSetChanged以刷新信息。

抱歉我的英语不好。