2014-04-11 16 views
0

我很新鮮android。我使用JSON WebService創建了android ListView。我想要舊的ListView數據清除,並再次單擊getdata按鈕時重新設置新數據。幫助我,提前致謝。舊的listview數據將被清除並重新設置新的數據當getdata按鈕被再次點擊android

setContentView(R.layout.activity_main); 
    CargoTracklist = new ArrayList<HashMap<String, String>>(); 

    Btngetdata = (Button)findViewById(R.id.button1); 
    Btngetdata.setOnClickListener(new View.OnClickListener() {   
     @Override 
     public void onClick(View view) {    
      new JSONParse().execute();    
     } 
    });     
}  
private class JSONParse extends AsyncTask<String, String, JSONObject> { 
    private ProgressDialog pDialog; 
    @Override 
    protected void onPreExecute() { 
     super.onPreExecute();   
     Status = (TextView)findViewById(R.id.textView2); 
     Id= (TextView)findViewById(R.id.textView1); 
     DateTime = (TextView)findViewById(R.id.textView3); 
     JobNo =(EditText)findViewById(R.id.editText1); 
     pDialog = new ProgressDialog(MainActivity.this); 
     pDialog.setMessage("Getting Data ..."); 
     pDialog.setIndeterminate(false); 
     pDialog.setCancelable(true); 
     pDialog.show(); 

}  
@Override 
    protected JSONObject doInBackground(String... args) { 
        JSONParser jParser = new JSONParser(); 
       ArrayList<BasicNameValuePair> params = new ArrayList<BasicNameValuePair>(); 
     params.clear();   
     params.add(new BasicNameValuePair("JobNo",JobNo.getText().toString())); 

       // Getting JSON from URL 
       JSONObject json = jParser.getJSONFromUrl(url,params); 
       return json;        
    }     
    @Override 
    protected void onPostExecute(JSONObject json) { 
     pDialog.dismiss(); 
     try { 
      if (JobNo.getText().toString().equals("")) 
       { 
        Toast.makeText(MainActivity.this, "You did not enter a JobNo", Toast.LENGTH_SHORT).show(); 

       } 
       else     {        
      // Getting JSON Array from URL 
       Cargo = json.getJSONArray(TAG_CargoTrack); 
       for(int i = 0; i < Cargo.length(); i++){ 
       JSONObject c = Cargo.getJSONObject(i); 

       // Storing JSON item in a Variable 
       String Status = c.getString(TAG_Status); 
       String Id = c.getString(TAG_Id); 
       String DateTime = c.getString(TAG_DateTime);                      HashMap<String, String> map = new HashMap<String, String>(); 

       map.put(TAG_Status, Status); 
       map.put(TAG_Id, Id); 
       map.put(TAG_DateTime, DateTime); 

       CargoTracklist.add(map); 
       list=(ListView)findViewById(R.id.listView1); 
                        ListAdapter adapter = new SimpleAdapter(MainActivity.this, CargoTracklist, 
         R.layout.listview, 
         new String[] { TAG_Status,TAG_Id, TAG_DateTime }, new int[] { 
           R.id.textView2,R.id.textView1, R.id.textView3}); 

       list.setAdapter(adapter); 
       list.setOnItemClickListener(new AdapterView.OnItemClickListener() { 

        @Override 
        public void onItemClick(AdapterView<?> parent, View view, 
              int position, long id) { 
         Toast.makeText(MainActivity.this, "You Clicked at "+CargoTracklist.get(+position).get("Id"), Toast.LENGTH_SHORT).show(); 

        } 
       }); 
+0

郵編你已經嘗試 –

+0

這是代碼正在使用的主要活動清除舊數據。我不知道在哪裏插入listview.clear行。 – user3454183

+0

我發佈的代碼.help我.. – user3454183

回答

1

每當你想更新你的列表中

if(CargoTracklist != null){ 
CargoTracklist.clear(); 
// Add new Data to CargoTracklist 
CargoTracklist.add("new data"); 
if(adapter != null){ 
adapter.notifyDataChaned(); 
} 
} 
+0

實際上我正在使用SpecialAdapter..notifyDataChaned這是爲基礎適配器..它顯示錯誤,{無法使靜態引用從類型BaseAdapter的非靜態方法notifyDataSetChanged()} – user3454183

+0

將ListAdapter更改爲SimpleAdapter – Yuvaraja

+0

對不起,使您麻煩。我已經改變了。它不接受... – user3454183

相關問題