0

我对 arraylist 哈希图有疑问。我想在 arraylist 哈希图中添加计数器值。我在不同的按钮上有很多计数器值。所以我想添加这些计数器值。但它在 arraylist 哈希映射中添加了 double 和重复值。

我的示例代码是:

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

提前致谢。

4

2 回答 2

0

可能是因为你说:

if(!myorder.contains(map1))                 
    myorder.add(map1);
}
if(myorder.contains(map1)) // this is always true cos you just inserted it above...
于 2013-01-10T05:21:50.340 回答
0

如何使用分页或 loadmore 选项在 android 中添加砌体布局

示例代码是

公共无效 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;
    }

});
于 2013-01-23T04:47:20.373 回答