【问题标题】:Android Plus & Minus buttons are not working correctly in Listview with BaseadapterAndroid 加号和减号按钮在带有 Baseadapter 的 Listview 中无法正常工作
【发布时间】:2016-10-18 06:37:04
【问题描述】:

我有一个带有自定义 Row.xml 的列表视图。

2 文本视图 2个按钮

按钮功能是加减数量。 App 开始运行时,每行默认数量为 1

点击加号时,数量+1

点击减号按钮时,数量-1

从屏幕截图 1 到屏幕截图 2 工作正常。我为第 1 行(data1)单击加号按钮 4 次。

但是,如果我点击 2nd Row(data2) Plus 按钮,数量没有加 1,而是直接跳到 6。这个错误也适用于减号

这意味着 Quantity 在每一行中不是唯一的。相反,它将两行 Quantity 累积在一起。

MainActivity

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);

MyCustomAdapter.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;
}
}

有人知道如何修复这个错误吗?

【问题讨论】:

  • 在按钮点击监听器上进行调试并检查分配给 int ab 的值
  • @DaminiMehra 我为 ab 放了 2 个日志,值与屏幕上显示的值相同。
  • 我认为您在创建数据列表时有问题。数组中只有一个字段[String],那么如何分别拥有不同项目的数量?您可以看看我在这里发布的答案:stackoverflow.com/questions/39926388/…,我认为这会有所帮助。

标签: android listview


【解决方案1】:

您应该将 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));
        }
    });

【讨论】:

  • 我被你的回答弄糊涂了
【解决方案2】:

在您的 clickListener 中使用来自相应文本视图的值 在您的减号 ClickListener 中

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);
}

【讨论】:

  • 如果我没有声明 Global,而是把它放在 getview 方法中,它会显示错误“将变量转换为最后一个元素数组”
  • 您的回答无效。点击时所有按钮都没有响应
【解决方案3】:

我自己解决了同样的问题。我使用对象来填充 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) {

}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-09-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多