0

我有一個ListView,我已經做了一個自定義適配器綁定數據到它,我已經做了一個asynctask活動獲取數據並顯示它進入listView,我有兩個不同的Url爲同一個asyctask,根據我使用它的條件,事情是,當我第二次listView不會刪除以前的值。ListView中的舊數據仍然存在,當我將新值綁定到ListView中的android

main.java

public class MyMessagesActivity extends Activity { 
    private ProgressDialog pDialog; 
    JSONArray msgArry; 
    private MessageAdapter msgContent; 
    ArrayList<HashMap<String, String>> msgList; 
    ListView lv; 
    JSONArray msgs = null; 
    String pro_id, pro_name, pro_img, pro_unit; 
    TextView tv_switch; 
    public boolean flag = false; 
    Header header; 
    Menu menu; 

    String url; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     requestWindowFeature(Window.FEATURE_NO_TITLE); 
     getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN); 
     setContentView(R.layout.activity_messgaes); 
     lv = (ListView) findViewById(R.id.list); 
     tv_switch = (TextView) findViewById(R.id.tv_switch); 
     header = (Header) findViewById(R.id.header_msg); 
     menu = (Menu) findViewById(R.id.menu_msg); 
     menu.setSelectedTab(3); 
     header.title.setText("Messages"); 
     msgList = new ArrayList<HashMap<String, String>>(); 
     // url = "?customer_id=" + Pref.getValue(MyMessagesActivity.this, 
     // Const.PREF_CUSTOMER_ID, "") + "&group_id=2"; 

     tv_switch.setOnClickListener(new OnClickListener() { 

      @Override 
      public void onClick(View v) { 

       if (flag) { 
        tv_switch.setText("Switch to supplier"); 
        new GetMessages().execute(); 
        flag = false; 
       } else { 
        tv_switch.setText("Switch to buyer"); 
        new GetMessages().execute(); 
        flag = true; 
       } 
      } 
     }); 
     // AsyncTAsk for Wholesale Product List...!!! 

     new GetMessages().execute(); 

     lv.setOnItemClickListener(new OnItemClickListener() { 

      @Override 
      public void onItemClick(AdapterView<?> parent, View view, int position, long id) { 
       // getting values from selected ListItem 

       // in = new Intent(getApplicationContext(), 
       // ProductDetailActivity.class); 
       /* 
       * pro_name = ((TextView) 
       * view.findViewById(R.id.product_label)).getText().toString(); 
       * 
       * // getting ProductId from the tag... 
       * 
       * pro_id = msgList.get(position).get(Const.TAG_PRODUCT_ID); 
       * pro_name = msgList.get(position).get(Const.TAG_PRODUCT_NAME); 
       * pro_img = msgList.get(position).get(Const.TAG_PRODUCT_IMG); 
       * System.out.println(
       * ":::::::::::::::;;THE INTENT FOR THE PRODUCUT DETIALS ACTIVITY=================" 
       * + pro_name); Toast.makeText(MyMessagesActivity.this, 
       * pro_name, Toast.LENGTH_SHORT).show(); 
       */ 
       // startActivity(in); 
      } 

     }); 
    } 

    private class GetMessages extends AsyncTask<Void, Void, Void> { 

     @Override 
     protected void onPreExecute() { 
      super.onPreExecute(); 
      pDialog = new ProgressDialog(MyMessagesActivity.this); 
      pDialog.setMessage("Please wait..."); 
      pDialog.setCancelable(false); 
      pDialog.show(); 

     } 

     @Override 
     protected Void doInBackground(Void... arg0) { 
      // Creating service handler class instance 
      BackendAPIService sh = new BackendAPIService(); 
      String query = Const.API_MESSAGES; 
      if (flag) { 
       url = "?customer_id=" + Pref.getValue(MyMessagesActivity.this, Const.PREF_CUSTOMER_ID, "") + "&group_id=1"; 
      } else { 
       url = "?customer_id=" + Pref.getValue(MyMessagesActivity.this, Const.PREF_CUSTOMER_ID, "") + "&group_id=2"; 
      } 

      url = url.replace(" ", "%20"); 
      url = query + url; 
      System.out.println(":::::::::::::My MESSGES URL::::::::::::::" + url); 

      // Making a request to url and getting response 
      String jsonStr = sh.makeServiceCall(url, BackendAPIService.GET); 

      Log.d("Response: ", "> " + jsonStr); 
      try { 
       if (jsonStr != null) { 
        msgArry = new JSONArray(jsonStr); 
        if (msgArry != null && msgArry.length() != 0) { 
         // looping through All Contacts 

         System.out.println(":::::::::::FLAG IN SUB:::::::::::" + msgArry.length()); 
         for (int i = 0; i < msgArry.length(); i++) { 
          JSONObject c = msgArry.getJSONObject(i); 

          String custID = c.getString(Const.TAG_CUSTOMER_ID); 
          String custName = c.getString(Const.TAG_CUSTOMER_NAME); 
          String proID = c.getString(Const.TAG_PRODUCT_ID); 
          String email = c.getString(Const.TAG_CUSTOMER_EMAIL); 
          String photo = Const.API_HOST + "/" + c.getString(Const.TAG_PHOTO); 
          String subject = c.getString(Const.TAG_SUBJECT); 
          String msg_read = c.getString(Const.TAG_MESSAGE_READ); 
          HashMap<String, String> message = new HashMap<String, String>(); 

          message.put(Const.TAG_CAT_ID, custID); 
          message.put(Const.TAG_CUSTOMER_NAME, custName); 
          message.put(Const.TAG_PRODUCT_ID, proID); 
          message.put(Const.TAG_CUSTOMER_EMAIL, email); 
          message.put(Const.TAG_PHOTO, photo); 
          message.put(Const.TAG_SUBJECT, subject); 
          message.put(Const.TAG_MESSAGE_READ, msg_read); 

          msgList.add(message); 

         } 
        } else { 
         runOnUiThread(new Runnable() { 

          @Override 
          public void run() { 
           Utils.showCustomeAlertValidation(MyMessagesActivity.this, "No messgaes found", "yehki", "Ok"); 

          } 
         }); 

        } 

       } else { 
        Log.e("ServiceHandler", "Couldn't get any data from the url"); 
       } 
      } catch (Exception e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } 
      return null; 
     } 

     @Override 
     protected void onPostExecute(Void result) { 
      super.onPostExecute(result); 
      // Dismiss the progress dialog 
      if (pDialog.isShowing()) 
       pDialog.dismiss(); 

      msgContent = new MessageAdapter(MyMessagesActivity.this, msgList); 
      msgContent.notifyDataSetChanged(); 
      lv.setAdapter(msgContent); 

     } 
    } 
} 

請大家幫幫我吧,謝謝前夕,一個

回答

1

嘗試從您的HashMap arraylist中刪除舊記錄,如下所示,以刪除arraylist中的所有數據。

數據綁定到ListView只是清除數組列表如下之後:

@Override 
    protected void onPostExecute(Void result) { 
     super.onPostExecute(result); 
     // Dismiss the progress dialog 
     if (pDialog.isShowing()) 
      pDialog.dismiss(); 

     msgContent = new MessageAdapter(MyMessagesActivity.this, msgList); 
     msgContent.notifyDataSetChanged(); 
     lv.setAdapter(msgContent); 

     msgList.clear(); 

    } 
+0

當暫停狀態下的活動會清除數據,並且當任何對話框到達前臺時它可能處於暫停狀態,對話框是否不屬於Asynctask。所以總是必須在'onResume()'中設置適配器 –

0

只是明確列表

添加mgList.clear();在保護無效onPreExecute()..

0

把這幾行的OnCreate()方法本身,

msgContent = new MessageAdapter(MyMessagesActivity.this, msgList); 
lv.setAdapter(msgContent); 

使用此行onPostExecute()的AsynTask類,

msgContent.notifyDataSetChanged(); 

如果這不起作用,請嘗試在msglist變量之前添加靜態關鍵字。

0

在你doInBackground()下面添加行開始for循環之前:

msgList.clear(); 
0

你需要調用msgList.clear();前到msgList ArrayList中添加數據。之後,在onPostExecute()方法只檢查而設置適配器的條件到列表視圖,

 try { 
      if (msgList!= null 
        && msgList.size() > 0) { 

       msgContent = new MessageAdapter(MyMessagesActivity.this, msgList); 
       lv.setAdapter(msgContent); 
       msgContent.notifyDataSetChanged(); 
      } else { 
       Toast.makeText(YourActivityName.this, 
         "No Data connection", Toast.LENGTH_SHORT).show(); 
      } 
     } catch (Exception e) { 
      e.printStackTrace(); 
     } 
0

清除msgList不是爲我工作。並使用 msgList = new ArrayList>();

in doInBackground(),然後再將新內容添加到列表中。只需要一個信息,當您將適配器的新實例設置爲列表視圖時,無需調用notifyDataSetChanged()(如果調用notifyDataSetChanged,則沒有問題)。

 msgContent = new MessageAdapter(MyMessagesActivity.this, msgList); 
     lv.setAdapter(msgContent); 
相關問題