2013-06-24 29 views
0

我有一个ListView。我想动态地改变listview的内容。我用“adapter.notifyDataSetChanged();”改变内容。它清爽但不清除列表视图,而是将它附加到以前的结果。我想要的是应删除以前的内容,并显示新的结果。的ListView不明确设置新值

我不能明白的地方是我的错误,我所缺少的。

我会心存感激,如果有人可以帮我了这一点。

这里是我的代码

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(); 
    } 

} 

}

+0

你并不需要调用notifyDataSetChanged()方法,只是用新值重新设定适配器。 –

回答

0

得到了wha T为我失踪

只是要在load_list()函数的开头添加

mylist.clear(); 

感谢ü您的宝贵时间。

0

你应该让本地这一行: - 替换此: -

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

从这个: -

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