2013-01-10 69 views
0

我有一個ArrayList hashmap的問題。我想在arraylist hashmap中添加計數器值。我在不同的按鈕上有很多計數器值。所以我想添加這些計數器值。但它在數組列表哈希映射中增加了雙倍和重複值。如何在arraylist哈希映射中使用唯一值?

我的示例代碼:提前

HashMap<String, String> map1 = new HashMap<String, String>(); 
String c = textcounter.getText().toString(); 
String d = txtdesc.getText().toString();    
st1=String.valueOf(button.getNextFocusForwardId()); 
st2=String.valueOf(button.getId()); 
System.out.println("----st1 value "+st1); 
System.out.println("---st2--value "+st2); 

String pval=String.valueOf(p); 


map1.put("PID", pval); 
map1.put("DESP", d); 
map1.put("PRICE", prc);    
map1.put("COUNTER_VAL", c); 

if(!myorder.contains(map1)) { 
    myorder.add(map1); 
} 
if(myorder.contains(map1)) 
{ 

    for(HashMap<String, String> hm : myorder) 
    { 
     hm.put("COUNTER_VAL", c); 
    } 
} 

System.out.println("Final value of My order"+ myorder); 

感謝。

+0

您已經添加'COUNTER_VAL'到'map1'。然後你在你的訂單中遍歷所有的地圖,併爲它們添加相同的'COUNTER_VAL'。這就是你想要做的事情嗎?看起來你根本不需要那樣做。 –

+0

其實我必須執行添加到購物車功能石工名單。在石匠一個Web服務調用來獲取數據字段product_id,pro_desc,價格和用戶可以從石匠列表中選擇多個項目和計數器將旋轉,每個產品都有其計數器和用戶將同時計數和價值將添加列表將生成,但我不要重複的值添加到列表和計數器也會移動。 –

+0

所以你有每個項目分開的櫃檯,而不是'全球'計數器? – codeMagic

回答

0

可能是因爲你說:

if(!myorder.contains(map1))     
    myorder.add(map1); 
} 
if(myorder.contains(map1)) // this is always true cos you just inserted it above... 
+0

好點子:錯過了那個。在這種情況下,我很困惑OP是怎麼回事;-) – John3136

+0

任何人都可以幫我解決我的問題 –

0

如何在Android中添加磚石佈局分頁或loadmore選項

示例代碼

公共無效oncreateProduct_List(INT COUNT分鐘){

String testurl = URL1 + "prod/"+countm; 

// String testurl = URL1 + "sandy_scandy/"+counter;   
    HttpClient client = new DefaultHttpClient(); 
    HttpConnectionParams.setConnectionTimeout(client.getParams(), 10000);  
    JsonParser jParser = new JsonParser(); 
    JSONObject json1 = jParser.getJSONfromUrl(testurl); 
    try { 
     JSONArray prod = json1.getJSONArray("prod"); 

     for (i = 0; i < prod.length(); i++) {     
      JSONObject prd = prod.getJSONObject(i); 
      p_id = prd.getString("p_id"); 
      cat_id = prd.getString("cat_id"); 
      barcode = prd.getString("barcode"); 
      p_name = prd.getString("p_name"); 
      price = prd.getString("price"); 
      desp = prd.getString("desp"); 
      image = prd.getString("image"); 
      status = prd.getString("status"); 
      timestamp = prd.getString("timestamp"); 
      cat_name = prd.getString("cat_name"); 

      try { 
       bitmap = BitmapFactory.decodeStream((InputStream) new URL(image).getContent()); 

      } catch (MalformedURLException e) { 

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

       e.printStackTrace(); 
      }    

      HashMap<String, String>map1= new HashMap<String, String>(); 
      map1.put("IMAGE", image); 

      pro_id = p_id;    
      create_layout(pro_id,bitmap); 

     } 

    } catch (JSONException e) { 
     Log.e("error", e.toString()); 
    } 

    //countm++; 


} 
private class LoadViewTask extends AsyncTask<Void, Integer, Void> { 
    // Before running code in the separate thread 
    @Override 
    protected void onPreExecute() { 

     progressDialog = new ProgressDialog(MyShopping_List.this); 



     progressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER); 





     progressDialog.setMessage("Please wait..."); 
     progressDialog.setCancelable(false); 
     progressDialog.setIndeterminate(false); 
     progressDialog.show(); 
    } 

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



     try { 

      synchronized (this) { 

       int counter = 0; 

       while (counter <= 10) { 

        this.wait(300); 

        counter++; 



       } 
      } 
     } catch (InterruptedException e) { 
      e.printStackTrace(); 
     } 
     return null; 
    } 
    @Override 
    protected void onProgressUpdate(Integer... values) { 

     progressDialog.setProgress(values[0]); 
    } 

    @Override 
    protected void onPostExecute(Void result) {   

      oncreateProduct_List(countm); 

      /*Scroll_main.setOnTouchListener(new OnTouchListener() { 


       public boolean onTouch(View v, MotionEvent event) {       

       if (event.getAction() == MotionEvent.ACTION_UP) { 
         Thread.getDefaultUncaughtExceptionHandler(); 
         Thread.yield(); 


          try { 
          Thread.sleep(100); 
         } catch (InterruptedException e) { 
          // TODO Auto-generated catch block 
          e.printStackTrace(); 
         } 
          new LoadViewTask().execute(); 
          oncreateProduct_List(countm);     
          countm++; 
         } 


        return false; 
       } 
    });   */ 



     progressDialog.dismiss(); 

    } 

} 

and call touch listener

Scroll_main.setOnTouchListener(新View.OnTouchListener(){

public boolean onTouch(View v, MotionEvent event) { 

     float yValue=event.getY(); 
     Display display = getWindowManager().getDefaultDisplay();   
      int height = display.getHeight(); 
      int scrollheight =Scroll_main.getHeight(); 
      System.out.println("Screen Height>"+height); 
     System.out.println("value of scroll main ="+scrollheight); 
     //int ss=height-181; 
     //height=(height*90)/100; 
     if((yValue)<scrollheight){ 
      Toast.makeText(getApplicationContext(), "Heloo"+height, Toast.LENGTH_SHORT).show(); 
     } /*elseif((Scroll_main.getHeight()-yValue)>=height){ 


       new LoadViewTask().execute(); 
       oncreateProduct_List(countm); 

       countm = countm--; 

       height=height--; 
     } */ 
     else{ 
      new LoadViewTask().execute(); 
      countm = countm ++; 
      oncreateProduct_List(countm);    
      scrollheight=scrollheight++; 

     } 



     return false; 
    } 

});