2016-01-05 71 views
1

enter image description here我希望如果用戶點擊+和 - 按鈕,然後對列表視圖中的特定列表項執行操作。在我的程序中,當我點擊第一個項目的按鈕時,也會增加和減少值,但是當另一個項目按鈕被點擊時,它會考慮先前項目的值以及對該值執行的增量和減量操作。我希望每個項目執行它們的分離。我不知道如何實現這一點。如何更新適配器中的列表視圖項目的點擊文本

這裏我的代碼:

public static class ViewHolder { 
     TextView tv_qty; 
    } 

    public class ProductAdapter extends ArrayAdapter<Product> { 
     ImageLoader imageLoader; 
     public ProductAdapter(Context context, int resource) { 
      super(context, resource); 
      imageLoader = new ImageLoader(context); 
     } 
     @Override 
     public View getView(final int position, View convertView, ViewGroup parent) { 
      Product product = getItem(position); 
     // Product product=ge 
      View view; 
      if (convertView == null) { 
       LayoutInflater layoutInflater = LayoutInflater.from(getContext()); 
       view = layoutInflater.inflate(R.layout.product_row, null); 
      } else { 
       view = convertView; 
      } 
      final ViewHolder viewHolder = new ViewHolder(); 
      tv_row_product_name = (TextView) view.findViewById(R.id.pname); 
      tv_row_product_rate = (TextView) view.findViewById(R.id.price); 
      tv_row_product_qty = (TextView) view.findViewById(R.id.productqty); 
      viewHolder. tv_qty = (TextView) view.findViewById(R.id.userqty); 
      tv_value = (TextView) findViewById(R.id.textView_value); 
      tv_totalprice = (TextView) findViewById(R.id.textview_totalprice); 
      ImageView imageView = (ImageView) view.findViewById(R.id.imageView); 
      Log.d(Config.tag, "url : " + "uploads/product/" + product.image1); 
      Picasso.with(ListViewProduct.this) 
        .load("http://www.sureshkirana.com/uploads/product/" + product.image1) 
        .into(imageView); 
      imgbtnp = (ImageButton) view.findViewById(R.id.imageButton2); 
      imgbtnm = (ImageButton) view.findViewById(R.id.imageButton); 
      imgbtnp.setOnClickListener(new View.OnClickListener() { 
       @Override 
       public void onClick(View v) { 
        // TODO Auto-generated method stub 
        count++; 
        viewHolder. tv_qty.setText(String.valueOf(count)); 
        notifyDataSetChanged(); 
       } 
      }); 
      imgbtnm.setOnClickListener(new View.OnClickListener() { 
       @Override 
       public void onClick(View v) { 
        // TODO Auto-generated method stub 
        if (count > 0) 
         count--; 
         viewHolder.tv_qty.setText(String.valueOf(count)); 
        notifyDataSetChanged(); 
       } 
      }); 
      view.setTag(viewHolder); 
      tv_row_product_name[enter image description here][1].setText(product.productTitle); 
      tv_row_product_rate.setText("Rs. " + product.productPrice + "/-"); 
      tv_row_product_qty.setText(product.quantity + "kg"); 
      tv_totalprice.setText("Rs." + product.product_amount); 
      return view; 
      } 
    } 
} 
+0

列表視圖項點擊或點擊按鈕??? –

+0

你應該看看這個:http://stackoverflow.com/questions/18119755/the-highligted-row-in-listview-doesnt-remain-highlighted-after-scrolling/18120781#18120781 – lgw150

+0

按鈕點擊先生 – pari

回答

0

無法使用單個變量用於這一目的。將計數設置爲標記到您的列表項目viewHolder.tv_qty.setTag(count); 並檢索值如viewHolder.tv_qty.getTag();

0

點擊+或 - 號在getView中獲取點擊項目的位置,並獲取該位置的產品對象,並用新值修改,然後再次將修改後的對象放在同一位置的同一列表中並調用notifyDatasetChanged ()。希望能幫助到你。

-1

使用Interface可以解決這個問題。您需要更新您的對象,然後使用notifyDataSetChanged()刷新您的列表適配器。

定製適配器

public interface QuantityClickListener { 
    void onIncrementClickListner(int position); 

    void onDecrementClickListner(int position); 
} 

/* 
* (non-Javadoc) 
* 
* @see android.widget.ArrayAdapter#getView(int, android.view.View, 
* android.view.ViewGroup) 
*/ 
@SuppressLint("InflateParams") 
@Override 
public View getView(final int position, View convertView, ViewGroup parent) { 
    ListViewWrapper wrapper = null; 
    LayoutInflater inflater = LayoutInflater.from(mContext); 
    if (null == convertView) { 
     convertView = inflater.inflate(R.layout.custom_list_item, null); 
     wrapper = new ListViewWrapper(convertView); 
     convertView.setTag(wrapper); 
    } else { 
     wrapper = (ListViewWrapper) convertView.getTag(); 
    } 
    // Schedule schedule = objects.get(position); 
    Products product = mProducts.get(position); 
    if (null != product) { 
     wrapper.getTxtQuantity().setText("" + product.quantity); 

     wrapper.getNegativeBtn().setOnClickListener(new OnClickListener() { 

      @Override 
      public void onClick(View v) { 
       quantityClickListener.onDecrementClickListner(position); 
      } 
     }); 

     wrapper.getPositiveBtn().setOnClickListener(new OnClickListener() { 

      @Override 
      public void onClick(View v) { 
       quantityClickListener.onIncrementClickListner(position); 
      } 
     }); 
    } 
    return convertView; 
} 

活動

/** 
* Initialize UI elements 
*/ 
private void initialize() { 
    listView = (ListView) findViewById(R.id.listview); 
    dummyData(); 
    adapters = new CustomAdapters(this, 0, products); 
    adapters.setQuantityClickListener(quantityClickListener); 
    listView.setAdapter(adapters); 
} 

private QuantityClickListener quantityClickListener = new QuantityClickListener() { 

    @Override 
    public void onIncrementClickListner(int position) { 
     if (null != products && products.size() > 0) { 
      Products product = products.get(position); 
      product.quantity++; 
      product.totalPrice = product.quantity * product.price; 
      adapters.notifyDataSetChanged(); 
     } 
    } 

    @Override 
    public void onDecrementClickListner(int position) { 
     if (null != products && products.size() > 0) { 
      Products product = products.get(position); 
      if (product.quantity > 0) { 
       product.quantity--; 
       product.totalPrice = product.quantity * product.price; 
       adapters.notifyDataSetChanged(); 
      } 
     } 
    } 
}; 
+0

請,添加關於你的解釋回答。只發布代碼並不是一個好的回答方式。 – HCarrasko

+0

這將幫助您解決您的問題。您可以從以下鏈接下載示例應用程序,[鏈接](https://www.dropbox.com/s/ff5g0corgisacz2/Sample.apk?dl=0) –

相關問題