我们如何在arraylist哈希映射中使用唯一值?

时间:2013-01-10 05:17:52

标签: android arraylist hashmap

我遇到了arraylist hashmap的问题。我想在arraylist hashmap中添加计数器值。我在不同的按钮上有很多计数器值。所以我想添加这些计数器值。但它在arraylist哈希映射中添加了double和duplicate值。

我的示例代码是:

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

提前致谢。

2 个答案:

答案 0 :(得分:0)

可能是因为你说:

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

答案 1 :(得分:0)

如何使用分页或加载更多选项

在android中添加masonary布局

示例代码是

public void oncreateProduct_List(int countm){

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

    }

}

并调用触控侦听器

Scroll_main.setOnTouchListener(new 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;
    }

});