Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

SOLVED: How to add selected listview items into arraylist

curious:

I have shopping list items. first, I store into ArrayList of hashmap and then store using ArrayList position.Now I want to add Selected Listview Items into new ArrayList names contents. How can I do this?

The code snippet :

Shopping_list.java


public class Shopping_list extends AppCompatActivity {

ArrayList data;
RelativeLayout norecord;
private Dialog dialog ;
ListView lv;
Context ctx = this;
ArrayList> contents = new ArrayList>();
ArrayList>contents_show=new ArrayList>();
HashMap 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://ift.tt/2AaQ3GE";
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 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 {

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 JSONObject jsonobject = jarray.getJSONObject(i);

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

}

}
});
} }

please look at this code and help me to solve the problem.For example, i selected 2,4,7 etc rows using button then these row's hashmap data store into contents (ArrayList).



Posted in S.E.F
via StackOverflow & StackExchange Atomic Web Robots
This Question have been answered
HERE


This post first appeared on Stack Solved, please read the originial post: here

Share the post

SOLVED: How to add selected listview items into arraylist

×

Subscribe to Stack Solved

Get updates delivered right to your inbox!

Thank you for your subscription

×