2016-10-25 5 views
0

私はカスタムリストビューを持っており、すべての行に対して1つの編集テキストと1つのボタンがあります。 私はそのボタンをクリックすると、編集テキストの価値を得たいだけです。カスタムリストビューに配置されたedittextから値を取得する方法

click here for activity image

画像のように、私たちは、カートに追加]ボタンをクリックしたとき量のためにあるのEditTextの値を取得したいです。私は以下のようなものだったアダプターやコードを使用することについては :

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; 
} 
} 
+0

の可能性のある重複した[アンドロイド - カスタムArrayAdapterとリストビューの各EditTexts値を取得します](http://stackoverflow.com/questions/23953066/android-get-each-edittexts-values-of-listview-with -custom-arrayadapter) – Rahul

答えて

0

などの使用ListsRecyclerviews、データを表示するために)、アダプタを使用して、リストに接続されているモデルですべてのあなたのEditTextの値を保持します。

たとえば、位置5にあるedittextを編集して、位置4(0が最初に表示されます)でビューに接続されたリストモデルですぐに変更された場合、この編集値を保存します。または、編集を終了するとモデルに保存できますが、とにかくモデルに保存します。あなたのデータをviewsに保存しないでください)例えば、recyclerviewはビューを再利用するため、スクロール時にデータが簡単に失われます)

1

別の方法として、ViewHolderパターンを使用します。 holder.btn_addto_CartのclickListenerでは、その編集テキストからデータを取得して処理を行うことができます。

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(); 
     } 
    }); 
} 
関連する問題