Darshan Dhoriya
Darshan Dhoriya

Reputation: 1014

how to get value from edittext which is placed into custom listview

i have a custom listview and for every row their is one Edit text and one Button. I just want to get value of edit text when we click on that button.

click here for activity image

as shown in image i want to get value of edittext which is for quantity when we click on add to cart Button. For that i used Adapter and code was like below:

public class ProductList_Adapter extends ArrayAdapter {

Context context;
int resource;
List<ProductList_ModelClass> productList_modelClasses = new ArrayList<>();

TextView tv_product_title;
TextView tv_product_price;
ImageView img_productImage;
RatingBar rb_custom_productlist;
EditText et_productQty;
Button btn_addto_Cart;
String product_Qty;

public ProductList_Adapter(Context context, int resource, List objects) {
    super(context, resource, objects);

    this.context = context;
    this.resource = resource;
    this.productList_modelClasses = objects;
}

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

@NonNull
@Override
public View getView(final int position, View convertView, ViewGroup parent) {

    if (convertView == null) {
        LayoutInflater layoutInflater = ((Activity) context).getLayoutInflater();
        convertView = layoutInflater.inflate(resource, parent, false);
    }

    img_productImage = (ImageView) convertView.findViewById(R.id.img_productImage);
    tv_product_title = (TextView) convertView.findViewById(R.id.tv_product_title);
    tv_product_price = (TextView) convertView.findViewById(R.id.tv_product_price);
    rb_custom_productlist = (RatingBar) convertView.findViewById(R.id.rb_custom_productlist);
    et_productQty = (EditText) convertView.findViewById(R.id.et_productQty);
    btn_addto_Cart = (Button) convertView.findViewById(R.id.btn_addto_Cart);


    Picasso.with(context)
            .load(productList_modelClasses.get(position).getProduct_image())
            .resize(250, 250)
            .placeholder(R.mipmap.icon_placeholder)   // optional
            .error(R.mipmap.icon_error)      // optional
            .into(img_productImage);

    tv_product_title.setText(productList_modelClasses.get(position).getProduct_name());
    rb_custom_productlist.setRating(productList_modelClasses.get(position).getProduct_rating());
    tv_product_price.setText("₹ " + productList_modelClasses.get(position).getProduct_price());


    product_Qty = et_productQty.getText().toString();

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

            Toast.makeText(context, product_Qty + , Toast.LENGTH_SHORT).show();
        }
    });

    convertView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Toast.makeText(context, productList_modelClasses.get(position).getProduct_name(), Toast.LENGTH_SHORT).show();
            Intent intent = new Intent(context, ProductActivity.class);
            intent.putExtra("product_id", productList_modelClasses.get(position).getProduct_id());
            context.startActivity(intent);


        }
    });

    return convertView;
}
}

Upvotes: 0

Views: 548

Answers (2)

Terry
Terry

Reputation: 669

Another practice is to use the ViewHolder pattern. In the clickListener of the holder.btn_addto_Cart, you can get data from that edittext and do the processing.

static class ViewHolder{
    TextView tv_product_title;
    TextView tv_product_price;
    ImageView img_productImage;
    RatingBar rb_custom_productlist;
    EditText et_productQty;
    Button btn_addto_Cart;
    String product_Qty;
}

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

    if (convertView == null) {
        convertView = LayoutInflater.from(getContext()).inflate(resouce, parent, false);

        holder = new ViewHolder();
        holder.img_productImage = (ImageView) findViewById(R.id.img_productImage);
        holder.tv_product_title = (TextView) findViewById(R.id.tv_product_title);
        holder.tv_product_price = (TextView) findViewById(R.id.tv_product_price);
        holder.rb_custom_productlist = (RatingBar) findViewById(R.id.rb_custom_productlist);
        holder.et_productQty = (EditText) findViewById(R.id.et_productQty);
        holder.btn_addto_Cart = (Button)findViewById(R.id.btn_addto_Cart);

        convertView.setTag(holder);
    } else {
        holder = (ViewHolder) convertView.getTag();
    }


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

            Toast.makeText(context, holder.et_productQty.getText() , Toast.LENGTH_SHORT).show();
        }
    });
}

Upvotes: 1

Stepan Maksymov
Stepan Maksymov

Reputation: 2606

Use Lists, Recyclerviews, etc. for displaying data ), hold all your edittext's values in models that are connected to your list with adapter.

for example if you edit some edittext in position 5 then save this edit value when it's changed immediately in list model connected to your view with adapter in position 4 (0 is first). Or you can save it to model when you end edit, but in anyway save to model. Don't keep your data in views it's bad practice ) For example recyclerview reuses views so your will simple loose your data when you scroll )

Upvotes: 0

Related Questions