lundi 7 avril 2014

Java - ajouter les valeurs sur ArrayList dans Android - Stack Overflow


I have develop one application. Here i have to add the value on ArrayList. if i have to click Button means that value have to add on that ArrayList. I have to click another Button means that added list is displaying. How can i do? Please give me solution.


These are my values:


 product_id = getIntent().getStringExtra("id");
product_title = getIntent().getStringExtra("title");
product_image = getIntent().getStringExtra("image");
product_price = getIntent().getStringExtra("price");
product_desc = getIntent().getStringExtra("description");

arrayList = new ArrayList<String>();
arrayList.add(product_title);
arrayList.add(product_price);
arrayList.add(product_id);
arrayList.add(product_image);
arrayList.add(product_desc);

I have to add these values on ArrayList while clicking the Button:


  valueaddlist = (Button) findViewById(R.id.valueaddlist);
valueaddlist.setOnClickListener(new OnClickListener() {
public void onClick(View v){
Intent intent = new Intent(this,AddedListProducts.class);
intent.putExtra("WishListProducts", arrayList);
startActivity(intent);
}

In the AddedListProducts have to displaying all added products list.


How can i do ? please give me solution for these ?


EDIT:


This is my AddedListProducts class code:


wishlist_products = (ListView) findViewById(R.id.wishlist_products);
if(getIntent().getExtras() !=null){
WishListProducts = (ArrayList<String>) getIntent().getExtras().getSerializable("WishListProducts");
System.out.println(WishListProducts);
wishlistproductsAdapter = new WishListAdapter(this,WishListProducts);
wishlist_products.setAdapter(wishlistproductsAdapter);

}

In these arraylist am getting values.how can i set the value on adapter file and UI.


This is my adapter file code:


     public class WishListAdapter extends BaseAdapter{

WishListAdapter mListViewAdapter;
private Activity mActivity;
private ArrayList<String> mwishlistProducts;
public ImageLoader mImageLoader;
private static LayoutInflater inflater=null;
public WishListAdapter(Activity activity, ArrayList<String> products) {
mActivity = activity;
this.mwishlistProducts=products;
inflater = (LayoutInflater)mActivity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

}

class ViewHolder{
private ImageView productImageView;

private TextView productTitleView;
private TextView productPriceView;
private TextView productDescView;

public ViewHolder(ImageView productImageView, TextView productTitleView,TextView productPriceView,TextView productDescView) {
super();
this.productImageView = productImageView;

this.productTitleView = productTitleView;
this.productPriceView = productPriceView;
this.productDescView = productDescView;

}
} // ViewHolder-class

public int getCount() {
return mwishlistProducts.size();

}

public Object getItem(int position) {
return position;
}

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

public View getView(int position, View convertView, ViewGroup parent) {
View vi=convertView;
ViewHolder holder;

final String wishlistproductList = mwishlistProducts.get(position);

if( convertView == null )
{
convertView = inflater.inflate(R.layout.list_product, null);
ImageView productImage=(ImageView)convertView.findViewById(R.id.productimage);

TextView productTitle = (TextView)convertView.findViewById(R.id.producttitle);
TextView productPrice = (TextView)convertView.findViewById(R.id.productprice);
TextView productDesc = (TextView)convertView.findViewById(R.id.productdescription);

holder = new ViewHolder(productImage,productTitle,productPrice,productDesc);
convertView.setTag(holder);
}else{
holder = (ViewHolder) convertView.getTag();
}
convertView.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {

}
});

holder.productTitleView.setText();
holder.productPriceView.setText();
holder.productDescView.setText();
mImageLoader=new ImageLoader();
mImageLoader.DisplayImage();
return convertView;
}
}

In these holder file what i have to set ????


How can i set that arraylist value here.please help me yaar..


EDIT:


More products is displaying on one listview. Now i have to click one list item means its go to detail description page.here i have to click button means that product detail value is adding and have to display on AddedListProducts Page.


now i ll go to back and click another product means click button means that product detail also added and have to display on AddedListProducts page with that old added products...


i have to add products from that listview and go to next page and clicking button means have to display that all added products on AddedListProducts page.how can i do ???


Above code ly displaying last added product ly.I want to display all added products on that list.




After getting value from intent:


ArrayList<String> arrayList = new ArrayList<String>();

valueaddlist = (Button) findViewById(R.id.valueaddlist);
valueaddlist.setOnClickListener(new OnClickListener() {
public void onClick(View v){
arrayList.add(product_id);
arrayList.add(product_title);
arrayList.add(product_image);
arrayList.add(product_price);
arrayList.add(product_desc);

}

valuedisplaylist = (Button) findViewById(R.id.valuedisplaylist);
valuedisplaylist.setOnClickListener(new OnClickListener() {
public void onClick(View v){
Intent intent = new Intent(this,AddedListProducts.class);
intent.putStringArrayListExtra("arrayList", (ArrayList<String>) arrayList);
startActivity(intent);
}

May be this will help you.


In your second activity get the arraylist like :


 ArrayList<String> ar1=getIntent().getExtras().getStringArrayList("arrayList"); ArrayAdapter<String> arrayAdapter =      
new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, ar1);
lv.setAdapter(arrayAdapter);

Then have a look at this question to display arraylist: Populating a ListView using ArrayList?




ArrayList<Integer> arrayList = new ArrayList<>();
arrayList.add(5);


I have develop one application. Here i have to add the value on ArrayList. if i have to click Button means that value have to add on that ArrayList. I have to click another Button means that added list is displaying. How can i do? Please give me solution.


These are my values:


 product_id = getIntent().getStringExtra("id");
product_title = getIntent().getStringExtra("title");
product_image = getIntent().getStringExtra("image");
product_price = getIntent().getStringExtra("price");
product_desc = getIntent().getStringExtra("description");

arrayList = new ArrayList<String>();
arrayList.add(product_title);
arrayList.add(product_price);
arrayList.add(product_id);
arrayList.add(product_image);
arrayList.add(product_desc);

I have to add these values on ArrayList while clicking the Button:


  valueaddlist = (Button) findViewById(R.id.valueaddlist);
valueaddlist.setOnClickListener(new OnClickListener() {
public void onClick(View v){
Intent intent = new Intent(this,AddedListProducts.class);
intent.putExtra("WishListProducts", arrayList);
startActivity(intent);
}

In the AddedListProducts have to displaying all added products list.


How can i do ? please give me solution for these ?


EDIT:


This is my AddedListProducts class code:


wishlist_products = (ListView) findViewById(R.id.wishlist_products);
if(getIntent().getExtras() !=null){
WishListProducts = (ArrayList<String>) getIntent().getExtras().getSerializable("WishListProducts");
System.out.println(WishListProducts);
wishlistproductsAdapter = new WishListAdapter(this,WishListProducts);
wishlist_products.setAdapter(wishlistproductsAdapter);

}

In these arraylist am getting values.how can i set the value on adapter file and UI.


This is my adapter file code:


     public class WishListAdapter extends BaseAdapter{

WishListAdapter mListViewAdapter;
private Activity mActivity;
private ArrayList<String> mwishlistProducts;
public ImageLoader mImageLoader;
private static LayoutInflater inflater=null;
public WishListAdapter(Activity activity, ArrayList<String> products) {
mActivity = activity;
this.mwishlistProducts=products;
inflater = (LayoutInflater)mActivity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

}

class ViewHolder{
private ImageView productImageView;

private TextView productTitleView;
private TextView productPriceView;
private TextView productDescView;

public ViewHolder(ImageView productImageView, TextView productTitleView,TextView productPriceView,TextView productDescView) {
super();
this.productImageView = productImageView;

this.productTitleView = productTitleView;
this.productPriceView = productPriceView;
this.productDescView = productDescView;

}
} // ViewHolder-class

public int getCount() {
return mwishlistProducts.size();

}

public Object getItem(int position) {
return position;
}

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

public View getView(int position, View convertView, ViewGroup parent) {
View vi=convertView;
ViewHolder holder;

final String wishlistproductList = mwishlistProducts.get(position);

if( convertView == null )
{
convertView = inflater.inflate(R.layout.list_product, null);
ImageView productImage=(ImageView)convertView.findViewById(R.id.productimage);

TextView productTitle = (TextView)convertView.findViewById(R.id.producttitle);
TextView productPrice = (TextView)convertView.findViewById(R.id.productprice);
TextView productDesc = (TextView)convertView.findViewById(R.id.productdescription);

holder = new ViewHolder(productImage,productTitle,productPrice,productDesc);
convertView.setTag(holder);
}else{
holder = (ViewHolder) convertView.getTag();
}
convertView.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {

}
});

holder.productTitleView.setText();
holder.productPriceView.setText();
holder.productDescView.setText();
mImageLoader=new ImageLoader();
mImageLoader.DisplayImage();
return convertView;
}
}

In these holder file what i have to set ????


How can i set that arraylist value here.please help me yaar..


EDIT:


More products is displaying on one listview. Now i have to click one list item means its go to detail description page.here i have to click button means that product detail value is adding and have to display on AddedListProducts Page.


now i ll go to back and click another product means click button means that product detail also added and have to display on AddedListProducts page with that old added products...


i have to add products from that listview and go to next page and clicking button means have to display that all added products on AddedListProducts page.how can i do ???


Above code ly displaying last added product ly.I want to display all added products on that list.



After getting value from intent:


ArrayList<String> arrayList = new ArrayList<String>();

valueaddlist = (Button) findViewById(R.id.valueaddlist);
valueaddlist.setOnClickListener(new OnClickListener() {
public void onClick(View v){
arrayList.add(product_id);
arrayList.add(product_title);
arrayList.add(product_image);
arrayList.add(product_price);
arrayList.add(product_desc);

}

valuedisplaylist = (Button) findViewById(R.id.valuedisplaylist);
valuedisplaylist.setOnClickListener(new OnClickListener() {
public void onClick(View v){
Intent intent = new Intent(this,AddedListProducts.class);
intent.putStringArrayListExtra("arrayList", (ArrayList<String>) arrayList);
startActivity(intent);
}

May be this will help you.


In your second activity get the arraylist like :


 ArrayList<String> ar1=getIntent().getExtras().getStringArrayList("arrayList"); ArrayAdapter<String> arrayAdapter =      
new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, ar1);
lv.setAdapter(arrayAdapter);

Then have a look at this question to display arraylist: Populating a ListView using ArrayList?



ArrayList<Integer> arrayList = new ArrayList<>();
arrayList.add(5);

0 commentaires:

Enregistrer un commentaire