如何将选定的listview项添加到arraylist中

时间:2017-11-06 08:04:46

标签: android listview arraylist android-asynctask hashmap

我有购物清单项目。首先,我存储到hashmap的ArrayList中,然后使用ArrayList position存储。现在我想将选定的listview项添加到新的ArrayList名称内容中。我怎么能这样做?

代码段:

Shopping_list.java

public class Shopping_list extends AppCompatActivity {

ArrayList<String> data;
RelativeLayout norecord;
private Dialog dialog ;
ListView lv;
Context ctx = this;
ArrayList<HashMap<String, String>> contents = new ArrayList<HashMap<String, String>>();
ArrayList<HashMap<String,String>>contents_show=new ArrayList<HashMap<String,String>>();
HashMap<String, String> map;
ListViewAdapter adapter;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_shopping_list);

   Button checkout=(Button)findViewById(R.id.checkout);

    lv=(ListView)findViewById(R.id.list_search);


    norecord=(RelativeLayout)findViewById(R.id.norecord);

  fetchShoppingData();


    checkout.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            if(contents.size()==0)
            {
                Toast.makeText(Shopping_list.this, "Please add some item in Cart", Toast.LENGTH_SHORT).show();
            }

            else
            {
                startActivity(new Intent(Shopping_list.this,Shopping_cart.class).putExtra("data",contents));

            }


        }
    });

}


class ListViewAdapter extends BaseAdapter {


    Context mContext;
    LayoutInflater inflater;


    ListViewAdapter(){
        inflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

    public class ViewHolder {
        ImageView icon;
        TextView title,price;
        EditText quantity;
        Button add,remove;

    }

    @Override
    public int getCount() {
        return contents_show.size();
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    public View getView(final int position, View view, ViewGroup parent) {
        imgUrl="http://www.domainname.com/images/Product/medium/";
        final ViewHolder holder;

        holder = new ViewHolder();


        view=inflater.inflate(R.layout.shopping_list_item,null);


        holder.icon=(ImageView)view.findViewById(R.id.image_shooping);
        holder.title=(TextView)view.findViewById(R.id.text_shopping);
        holder.price=(TextView)view.findViewById(R.id.price_shopping);
        holder.quantity=(EditText)view.findViewById(R.id.item_cart);
        holder.add=(Button)view.findViewById(R.id.add_cart);
        holder.remove=(Button)view.findViewById(R.id.remove_item);

        view.setId(position);


        HashMap<String, String> map = new HashMap<>(contents_show.get(position));

        holder.title.setText(map.get("name"));

        holder.price.setText("Price : "+map.get("Amount")+ " Rs.");


        Picasso.with(ctx).load(imgUrl+map.get("image"))
                .placeholder(R.drawable.logo)
                .error(R.drawable.errorlogo)
                .into(holder.image);


       holder.add.setOnClickListener(new View.OnClickListener() {
            @Override

          public void onClick(View v)
          { 

                     for(int i=0;i<contents.size;i++)
                      {

                      if(  condition ??   )
                        {
          contents.add(contents_show.get(position));
                        }


                 }    
            }  });

        return view;
    }

}


private void fetchShoppingData(){

    GetResponce as = new GetResponce(Shopping_list.this);
    try {
        String  res = as.execute().get().toString();

        JSONObject jObj = null;
        jObj = new JSONObject(res);
        JSONArray jsonarray = jObj.getJSONArray("listproduct");

        jObj = jsonarray.getJSONObject(0);
        if (jObj.getString("ResponseCode").contains("1")) {
            norecord.setVisibility(View.GONE);


              String response=jObj.getString("ResponseStatus");
               JSONArray jarray=new JSONArray(response);
              for (int i = 0; i < jarray.length(); i++) {
                JSONObject jsonobject = jarray.getJSONObject(i);

                HashMap<String, String> map = new HashMap<>();

                                map.put("name", datalist.get(position).getName());
                                map.put("Amount", datalist.get(position).getAmt());
                                map.put("image", imgUrl + datalist.get(position).getImg());
                                map.put("tax",datalist.get(position).getTax());
                                map.put("discount",datalist.get(position).getDiscount());
                                map.put("productId",datalist.get(position).getProductId());
                                map.put("bv",datalist.get(position).getBv());
                                map.put("pv",datalist.get(position).getPv());


                          contents_show.add(map);


            }

             setList();


        }else {
                    norecord.setVisibility(View.GONE);
                }
            }

    } catch (InterruptedException e) {
        e.printStackTrace();
    } catch (ExecutionException e) {
        e.printStackTrace();
    } catch (JSONException e) {
        e.printStackTrace();
    }


}



public void setList() {
    runOnUiThread(new Runnable() {
        public void run() {
            lv.setAdapter(new ListViewAdapter());
            if (contents_show.size() == 0) {
                norecord.setVisibility(View.VISIBLE);


            } else {
                norecord.setVisibility(View.GONE);

            }

        }
    });
}   }

请查看此代码并帮助我解决问题。例如,我使用按钮选择2,4,7等行,然后将这些行的hashmap数据存储到内容(ArrayList)。

1 个答案:

答案 0 :(得分:1)

这就是你的方式

sudo pip install futures