在使用Baseadapter的Listview中,Android Plus和Minus按钮无法正常工作

盛年不重来,一日难再晨,及时当勉励,岁月不待人。这篇文章主要讲述在使用Baseadapter的Listview中,Android Plus和Minus按钮无法正常工作相关的知识,希望能为你提供帮助。
我有一个带有自定义Row.xml的listview。
2 Textviews 2按钮
按钮功能是加号和减号数量。当应用程序在开始运行时,每行默认数量为1。
点击加号时,数量+ 1
单击减号按钮时,数量为-1

在使用Baseadapter的Listview中,Android Plus和Minus按钮无法正常工作

文章图片

从屏幕截图1到屏幕截图2工作正常。我为行1(data1)单击加号按钮4次。
【在使用Baseadapter的Listview中,Android Plus和Minus按钮无法正常工作】但是,如果我点击第二行(data2)加号按钮,数量不会加1,但会立即跳到6.这个错误也适用于减号
这意味着数量在每一行中都不是唯一的。相反,它会将两个行累积在一起。
主要信息
protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); final ArrayList arrayList=new ArrayList< String> (); arrayList.add("data1"); arrayList.add("data2"); ListView listView = (ListView) findViewById(R.id.listview); final MyCustomAdapter myCustomAdapter=new MyCustomAdapter(this,arrayList); listView.setAdapter(myCustomAdapter);

my custom adapter.java
public class MyCustomAdapter extends BaseAdapter {int ab=1; int p=1; Context ctx; LayoutInflater inflater=null; ArrayList arrayList; public MyCustomAdapter(Context ctx,ArrayList arrayList){ this.ctx=ctx; this.arrayList=arrayList; }@Override public int getCount() { return arrayList.size(); }@Override public Object getItem(int i) { return arrayList.get(i); }@Override public long getItemId(int i) { return i; }@Override public View getView(int i, View view, ViewGroup viewGroup) {View row=view; if(row==null){ inflater=(LayoutInflater)ctx.getSystemService(Context.LAYOUT_INFLATER_SERVICE); row=inflater.inflate(R.layout.row,null); }TextView product_name=(TextView)row.findViewById(R.id.text); product_name.setText(arrayList.get(i).toString()); final TextView quantity=(TextView)row.findViewById(R.id.quantity); Button minus=(Button) row.findViewById(R.id.minus); Button plus=(Button) row.findViewById(R.id.plus); minus.setOnClickListener(new View.OnClickListener(){ public void onClick(View v){ if(ab!=1){// if quantity =1, cannot minus anymore ab=ab-1; } p=ab; quantity.setText(Integer.toString(ab)); } }); plus.setOnClickListener(new View.OnClickListener(){ public void onClick(View v){ ab=ab+1; p=ab; quantity.setText(Integer.toString(ab)); } }); return row; } }

任何人都知道如何解决这个错误?
答案您应该将quantity.getText()强制转换为int,而不是使用类变量ab。 所以创建变量
int mQuantity = Integer.valueOf (quantity.geText().toString ())

然后转换此变量添加一个并重置Textview值。
还要确保Textview数量中有值,并且您不会得到解析NumberException
在你的情况下减去例子:
... Button plus=(Button) row.findViewById(R.id.plus); int mQuantity = Integer.valueOf(quantity.getText().toString()); minus.setOnClickListener(new View.OnClickListener(){ public void onClick(View v){ if(mQuantity!=1){// if quantity =1, cannot minus anymore mQuantity += 1; } p=mQuantity; quantity.setText(Integer.toString(mQuantity)); } });

另一答案在您的单击侦听器中使用来自相应textview的值在您的减号ONClickListener中
if (Integer.parseInt(quantity.getText().toString())!=1){ quantity.setText(""+Integer.parseInt(quantity.getText().toString())-1); }

在您的加ClickListener中
if (Integer.parseInt(quantity.getText().toString())!=1){ quantity.setText(""+Integer.parseInt(quantity.getText().toString())+1); }

另一答案我有同样的问题自己解决了。我使用对象来填充ExpandableListView的子项,其中包含像您的场景一样的加号和减号按钮。我发现您需要更新OnClick侦听器内的对象,否则它们将根据全局变量值递增。这是代码:
public class MainListAdapter extends BaseExpandableListAdapter implements View.OnClickListener { private Context context; private List< Category> expandableListTitle; private HashMap< Category, List< Item> > expandableListDetail; private double quant; public MainListAdapter(Context context, List< Category> expandableListTitle, HashMap< Category, List< Item> > expandableListDetail) { this.context = context; this.expandableListTitle = expandableListTitle; this.expandableListDetail = expandableListDetail; }@Override public Object getChild(int listPosition, int expandedListPosition) { return this.expandableListDetail.get (this.expandableListTitle.get(listPosition)) .get(expandedListPosition); }@Override public long getChildId(int listPosition, int expandedListPosition) { return expandedListPosition; }@Override public View getChildView(int listPosition, final int expandedListPosition, boolean isLastChild, View convertView, ViewGroup parent) {final Item expandedListitem = (Item) getChild(listPosition, expandedListPosition); Log.d("item", expandedListitem.getQuantity()+expandedListitem.getItemName()); if (convertView == null) { LayoutInflater layoutInflater = (LayoutInflater) this.context .getSystemService(Context.LAYOUT_INFLATER_SERVICE); convertView = layoutInflater.inflate(R.layout.list_item, null); }String nam=expandedListitem.getItemName(); Button plus = (Button)convertView.findViewById(R.id.plus); Button minus = (Button)convertView.findViewById(R.id.minus); final TextView quantity = (TextView) convertView .findViewById(R.id.quantity); //First get the quantity from object quant=expandedListitem.getQuantity(); quantity.setText(String.valueOf(quant)); plus.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { //also each time button is clicked get and set values to objects quant=expandedListitem.getQuantity(); quant+=1; Log.d("clicked",String.valueOf(quant)); expandedListitem.setQuantity(quant); quantity.setText(String.valueOf(quant)); } }); minus.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { quant=expandedListitem.getQuantity(); quant-=1; expandedListitem.setQuantity(quant); quantity.setText(String.valueOf(quant)); } }); TextView name =(TextView) convertView.findViewById(R.id.list_text); name.setText(nam); return convertView; }@Override public int getChildrenCount(int listPosition) { return this.expandableListDetail.get(this.expandableListTitle.get(listPosition)) .size(); }@Override public Object getGroup(int listPosition) { return this.expandableListTitle.get(listPosition); }@Override public int getGroupCount() { return this.expandableListTitle.size(); }@Override public long getGroupId(int listPosition) { return listPosition; }@Override public View getGroupView(int listPosition, boolean isExpanded, View convertView, ViewGroup parent) {Category category = (Category) getGroup(listPosition); String name=category.getCategoryName(); if (convertView == null) { LayoutInflater layoutInflater = (LayoutInflater) this.context. getSystemService(Context.LAYOUT_INFLATER_SERVICE); convertView = layoutInflater.inflate(R.layout.category, null); } TextView listTitleTextView = (TextView) convertView .findViewById(R.id.cat_text); listTitleTextView.setTypeface(null, Typeface.BOLD); listTitleTextView.setText(name); return convertView; }@Override public boolean hasStableIds() { return false; }@Override public boolean isChildSelectable(int listPosition, int expandedListPosition) { return true; }@Override public void onClick(View view) {}


    推荐阅读