0

我有一个列表视图。我想动态更改列表视图的内容。我使用了“adapter.notifyDataSetChanged();” 更改内容。它正在刷新但不清除列表视图,而是将其附加到以前的结果中。我想要的是应该删除以前的内容并显示新的结果。

我无法理解我的错误在哪里以及我错过了什么。

如果有人能帮我解决这个问题,我将不胜感激。

这是我的代码

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    setContentView(R.layout.slider);


    t1 = (TextView) findViewById(R.id.noRec);
    // list = (ListView) findViewById(R.id.listView1);
    list = getListView();
    cb1 = (CheckBox) findViewById(R.id.checkBox1);
    cb2 = (CheckBox) findViewById(R.id.checkBox2);
    cb3 = (CheckBox) findViewById(R.id.checkBox3);
    cb4 = (CheckBox) findViewById(R.id.checkBox4);
    cb5 = (CheckBox) findViewById(R.id.checkBox5);
    cb6 = (CheckBox) findViewById(R.id.checkBox6);
    cb7 = (CheckBox) findViewById(R.id.checkBox7);
    cb8 = (CheckBox) findViewById(R.id.checkBox8);
    cb9 = (CheckBox) findViewById(R.id.checkBox9);
    cb10 = (CheckBox) findViewById(R.id.checkBox10);
    d = (Spinner) findViewById(R.id.drive);
    spn1 = (Spinner) findViewById(R.id.spinner1);
    ArrayAdapter<String> adap = new ArrayAdapter<String>(this,
            R.layout.spin_adptr, search);

    adap.setDropDownViewResource(R.layout.spin_drop);

    spn1.setAdapter(adap);
    spn1.setOnItemSelectedListener(this);
    int spinner_id = sid.getInt("SpnID", 0);
    // spn1.setSelection(spinner_id);

    ArrayAdapter spdrv = new ArrayAdapter(this, R.layout.spin_drop, drive);
    d.setAdapter(spdrv);

    t1.setVisibility(View.GONE);
    mylist = new ArrayList<HashMap<String, String>>();

    pos = new ArrayList<String>();

    Bundle bundle = getIntent().getExtras();
    stuff = bundle.getString("stuff");

    if (stuff.contains("null")) {

        t1.setVisibility(View.VISIBLE);
        t1.setText("No Records Found");
        Toast.makeText(getApplicationContext(), "No Records Found!",
                Toast.LENGTH_LONG).show();

    } else {

        load_list(stuff);

    }



}

private void startDownload() {

    new AppTask().execute(FILENAME);
}

public class AppTask extends AsyncTask<String, Integer, String> {

    @Override
    protected void onPreExecute() {
        super.onPreExecute();

    }

    protected void onPostExecute(String result) {
        super.onPostExecute(result);
        dialog.dismiss();
        String ref = result;
        Toast.makeText(getApplicationContext(), "Result= " + ref,
                Toast.LENGTH_LONG).show();

        Toast.makeText(getApplicationContext(), "POST EXECUTE FLG= " + flg,
                Toast.LENGTH_LONG).show();
        pos = new ArrayList<String>();

        load_list(ref);

        /*
         * Intent myIntent = new Intent(Slider.this, Slider.class);
         * 
         * Bundle bundle = new Bundle(); // Add your data to bundle
         * bundle.putString("stuff", ref); // Add the bundle to the intent
         * myIntent.putExtras(bundle); startActivity(myIntent);
         */

    }

    @Override
    protected void onProgressUpdate(Integer... values) {
        super.onProgressUpdate(values);

    }

    @Override
    protected String doInBackground(String... params) {
        if (flag == 1) {
            flag = 0;
            HttpClient httpclient = new DefaultHttpClient();
            HttpPost httppost = new HttpPost(
                    "http://1.php");
            try {
                List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(
                        5);
                nameValuePairs.add(new BasicNameValuePair("Qry", query));

                httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
                // httpclient.execute(httppost);
                HttpResponse response = httpclient.execute(httppost);

                HttpEntity entity = response.getEntity();
                is = EntityUtils.toString(entity);

            } catch (ClientProtocolException e) {
                // TODO Auto-generated catch block
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
            }
        }

        if (flag == 2) {
            flag = 0;
            flg++;
            HttpClient httpclient = new DefaultHttpClient();
            HttpPost httppost = new HttpPost(
                    "http://2.php");
            try {
                flg++;
                List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(
                        5);
                nameValuePairs.add(new BasicNameValuePair("reg_plc", city));

                httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
                httpclient.execute(httppost);
                HttpResponse response = httpclient.execute(httppost);

                HttpEntity entity = response.getEntity();
                is = EntityUtils.toString(entity);

            } catch (ClientProtocolException e) {
                // TODO Auto-generated catch block
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
            }
        }

        if (flag == 3) {
            flag = 0;
            flg++;
            HttpClient httpclient = new DefaultHttpClient();
            HttpPost httppost = new HttpPost(
                    "http://3.php");
            try {
                flg++;
                List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(
                        5);
                nameValuePairs.add(new BasicNameValuePair("reg_plc", city));

                httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
                httpclient.execute(httppost);
                HttpResponse response = httpclient.execute(httppost);

                HttpEntity entity = response.getEntity();
                is = EntityUtils.toString(entity);

            } catch (ClientProtocolException e) {
                // TODO Auto-generated catch block
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
            }
        }

        if (flag == 4) {
            if (make.equals("(Select)") || model.equals("(Select)")) {

                allcar();
            } else {

                selectcar();
            }

        }

        return is;
    }

    public void allcar() {

        HttpClient httpclient = new DefaultHttpClient();
        HttpPost httppost = new HttpPost(
                "http://4.php");
        try {
            List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(
                    3);
            nameValuePairs.add(new BasicNameValuePair("city", city));

            nameValuePairs.add(new BasicNameValuePair("make", make));
            nameValuePairs.add(new BasicNameValuePair("model", model));
            nameValuePairs.add(new BasicNameValuePair("range1", range1));
            nameValuePairs.add(new BasicNameValuePair("range2", range2));

            httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
            // httpclient.execute(httppost);

            HttpResponse response = httpclient.execute(httppost);
            HttpEntity entity = response.getEntity();
            is = EntityUtils.toString(entity);

        } catch (ClientProtocolException e) {
            // TODO Auto-generated catch block
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
        }

    }

    public void selectcar() {

        HttpClient httpclient = new DefaultHttpClient();
        HttpPost httppost = new HttpPost("http://5.php");
        try {
            List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(
                    5);
            nameValuePairs.add(new BasicNameValuePair("city", city));

            nameValuePairs.add(new BasicNameValuePair("make", make));
            nameValuePairs.add(new BasicNameValuePair("model", model));
            nameValuePairs.add(new BasicNameValuePair("range1", range1));
            nameValuePairs.add(new BasicNameValuePair("range2", range2));

            httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
            // httpclient.execute(httppost);

            HttpResponse response = httpclient.execute(httppost);
            HttpEntity entity = response.getEntity();
            is = EntityUtils.toString(entity);

        } catch (ClientProtocolException e) {
            // TODO Auto-generated catch block
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
        }

    }

}

@Override
public void onContentChanged() {
    super.onContentChanged();
    mCloseButton = (Button) findViewById(R.id.button_close);
    mOpenButton = (Button) findViewById(R.id.button_open);
    mDrawer = (MultiDirectionSlidingDrawer) findViewById(R.id.drawer);
}

@Override
public void onClick(View v) {
    // TODO Auto-generated method stub

}


public void load_list(String lt) {
    SimpleAdapter adapter = null;
    String[] frm = new String[] {};
    int[] too = new int[] {};
    SimpleAdapter adapter1 = new SimpleAdapter(this, mylist,
            R.layout.text_adaptr, frm, too);

    list = getListView();
    list.setAdapter(adapter1);

    // list.setAdapter(null);
    try {

        JSONArray jArray = new JSONArray(lt.toString());

        for (int i = 0; i < jArray.length(); i++) {

            map = new HashMap<String, String>();
            JSONObject jObject = jArray.getJSONObject(i);
            j_id = jObject.getString(ID);
            j_make = jObject.getString(MAKE);
            j_model = jObject.getString(MODEL);
            j_version = jObject.getString(VERSION);
            j_price = jObject.getString(PRICE);
            j_reg_plc = jObject.getString(PLACE_REG);
            // j_add = jObject.getString(ADDRESS);

            data = j_make + "";
            map.put("make", data);

            data = j_model + "";
            map.put("model", data);

            data = j_version + "";
            map.put("version", data);

            data = j_price + "";
            map.put("price", data);

            data = j_reg_plc + "";
            map.put("place", data);

            mylist.add(map);
            System.out.println("MAP=" + map);
            // Toast.makeText(getApplicationContext(), "MAP =" + map,
            // Toast.LENGTH_LONG).show();
            pos.add(j_id);

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

    {
        int fl = 0;
        fl++;
        // list = (ListView) findViewById(R.id.listView1);

        String[] from = new String[] { "make", "model", "version", "price",
                "place" };
        int[] to = new int[] { R.id.text1, R.id.text5, R.id.text3,
                R.id.text4, R.id.text2 };
        adapter = new SimpleAdapter(this, mylist, R.layout.text_adaptr,
                from, to);

        list.setAdapter(adapter);

        adapter.notifyDataSetChanged();
        list.setAdapter(adapter);
        // Toast.makeText(getApplicationContext(), "Fl= " + fl,
        // Toast.LENGTH_SHORT).show();
    }

    // ---------------------------------

    int[] colors = { 0, 0xff00ffff, 0 }; // red for the example
    list.setDivider(new GradientDrawable(Orientation.RIGHT_LEFT, colors));
    list.setDividerHeight(4);
    list.setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> parent, View view,
                int position, long id) {

            Boolean online = isOnline();

            if (online == false) {
                AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
                        context);

                // set title
                alertDialogBuilder.setTitle("Cellular Data is Turned Off!");

                // set dialog message
                alertDialogBuilder
                        .setMessage(
                                "Please turn on cellular data or use Wi-Fi to access data!")
                        .setCancelable(false)
                        .setNeutralButton("Settings",
                                new DialogInterface.OnClickListener() {
                                    public void onClick(
                                            DialogInterface dialog,
                                            int whichButton) {

                                        startActivity(new Intent(
                                                android.provider.Settings.ACTION_WIRELESS_SETTINGS));
                                    }
                                })
                        .setPositiveButton("Ok",
                                new DialogInterface.OnClickListener() {
                                    public void onClick(
                                            DialogInterface dialog, int id) {
                                        dialog.cancel();
                                    }
                                });

                // create alert dialog
                AlertDialog alertDialog = alertDialogBuilder.create();

                // show it
                alertDialog.show();
            } else {
                String data = pos.get(position) + "";

                // Display_Car dc=new Display_Car();
                // dc.get_cid(data);
                Intent myIntent = new Intent(Slider.this, Buy_View.class);
                Bundle bundle = new Bundle();
                // Add your data to bundle
                bundle.putString("stuff", data);
                // Add the bundle to the intent
                myIntent.putExtras(bundle);
                startActivity(myIntent);
            }
        }
    });
    // ---------------------------------
}

@Override
public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
        long arg3) {

    int id = spn1.getSelectedItemPosition();
    Editor edit = sid.edit();
    edit.putInt("SpnID", id);
    edit.commit();
    Toast.makeText(getApplicationContext(), "Shared pref=" + sid,
            Toast.LENGTH_LONG).show();
    if (id == 0) {

        flag = 4;
        // dialog = ProgressDialog.show(Slider.this, "Loading",
        // "Please Wait...");
        // startDownload();

        Toast.makeText(getApplicationContext(), "ALL", Toast.LENGTH_LONG)
                .show();
    } else if (id == 1) {

        flag = 2;
        flg++;
        Toast.makeText(getApplicationContext(),
                "INSIDE SPINNER FLG= " + flg, Toast.LENGTH_LONG).show();
        Toast.makeText(getApplicationContext(), "Dealers Only",
                Toast.LENGTH_LONG).show();
        dialog = ProgressDialog.show(Slider.this, "Loading",
                "Please Wait...");
        startDownload();

    } else {

        flag = 3;
        dialog = ProgressDialog.show(Slider.this, "Loading",
                "Please Wait...");
        startDownload();

        Toast.makeText(getApplicationContext(), "Individual Only",
                Toast.LENGTH_LONG).show();
    }

}

}

4

2 回答 2

0

您应该在本地制作这一行:--替换:-

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

由此:-

HashMap<String, String> map = new HashMap<String, String>();
于 2013-06-24T10:54:21.850 回答
0

知道我错过了什么

只需要添加

mylist.clear(); 

在 load_list() 函数的开头。

感谢您宝贵的时间。

于 2013-06-24T11:32:22.977 回答