Android 如何在适配器中单击listview项时更新文本
我希望,若用户单击+和-按钮,那个么对listview中的特定列表项执行操作。在我的程序中,当我点击第一个项目的按钮,然后价值增值和减量,但当另一个项目按钮点击时间,它考虑以前的项目价值和减少和减量行动上执行该值。我希望每个项目执行其分立。我不知道如何实现这一点 这是我的代码:Android 如何在适配器中单击listview项时更新文本,android,Android,我希望,若用户单击+和-按钮,那个么对listview中的特定列表项执行操作。在我的程序中,当我点击第一个项目的按钮,然后价值增值和减量,但当另一个项目按钮点击时间,它考虑以前的项目价值和减少和减量行动上执行该值。我希望每个项目执行其分立。我不知道如何实现这一点 这是我的代码: public static class ViewHolder { TextView tv_qty; } public class ProductAdapter extends Array
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;
}
}
}
公共静态类ViewHolder{
TextView tv_数量;
}
公共类ProductAdapter扩展了ArrayAdapter{
图像加载器;
公共产品适配器(上下文,int资源){
超级(上下文、资源);
imageLoader=新的imageLoader(上下文);
}
@凌驾
公共视图getView(最终整数位置、视图转换视图、视图组父视图){
产品=获取项目(位置);
//产品=通用电气
视图;
if(convertView==null){
LayoutInflater LayoutInflater=LayoutInflater.from(getContext());
视图=布局平坦。充气(R.layout.product_行,空);
}否则{
视图=转换视图;
}
最终ViewHolder ViewHolder=新的ViewHolder();
tv_row_product_name=(TextView)view.findViewById(R.id.pname);
tv_row_product_rate=(TextView)view.findviewbyd(R.id.price);
tv\行\产品\数量=(TextView)视图.findViewById(R.id.productqty);
viewHolder.tv_qty=(TextView)view.findViewById(R.id.userqty);
tv_值=(TextView)findViewById(R.id.TextView_值);
tv_totalprice=(TextView)findViewById(R.id.TextView_totalprice);
ImageView=(ImageView)view.findViewById(R.id.ImageView);
Log.d(Config.tag,“url:”+“uploads/product/”+product.image1);
毕加索.with(ListViewProduct.this)
.加载(“http://www.sureshkirana.com/uploads/product/“+product.image1)
.进入(图像视图);
imgbtnp=(ImageButton)view.findViewById(R.id.imageButton2);
imgbtnm=(ImageButton)view.findViewById(R.id.ImageButton);
imgbtnp.setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图v){
//TODO自动生成的方法存根
计数++;
viewHolder.tv_数量setText(String.valueOf(count));
notifyDataSetChanged();
}
});
imgbtnm.setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图v){
//TODO自动生成的方法存根
如果(计数>0)
计数--;
viewHolder.tv_数量setText(String.valueOf(count));
notifyDataSetChanged();
}
});
view.setTag(viewHolder);
tv\u行\u产品\u名称[在此处输入图像描述][1].setText(product.productTitle);
tv_row_product_rate.setText(“Rs.”+product.productPrice+“/”);
电视行产品数量设置文本(产品数量+千克);
tv_totalprice.setText(“Rs.”+产品.product_金额);
返回视图;
}
}
}
您不能为此目的使用单个变量。将count as标记设置为列表项viewHolder.tv\u qty.setTag(计数)代码>
并检索值,如viewHolder.tv_qty.getTag()代码> 单击+或-符号后,获取已单击项在getView中的位置,并在该位置获取产品对象,使用新值进行修改,然后再次将修改后的对象放在同一位置的同一列表中,并调用notifyDatasetChanged()。希望有帮助。使用界面可以解决此问题。您需要更新对象,然后使用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();
}
}
}
};
listview项目点击或按钮点击???你应该看看这个:请添加一个关于你答案的解释。只发布代码不是一个很好的回答方式。这将帮助您解决问题。您可以从以下链接下载示例应用程序,