我有一个带有自定义Row.xml的列表视图。
2 Textviews 2个按钮
按钮功能是加号和减号。当应用程序在开始时运行时,每行默认数量为1 。
点击加号时,数量为+ 1
单击减号按钮时,数量为-1
从屏幕截图1到屏幕截图2工作正常。我为行1(数据1)单击加号按钮4次。
但是,如果我点击第二行(data2)加号按钮,数量不会加1,但会立即跳到6.此错误也适用于减号
这意味着数量在每一行中都不是唯一的。相反,它会将两个行累积在一起。
MainActivity
var n = new Date(),
jam = n.getHours(),
time = '';
switch (true) {
case (jam < 9): // this leads to if (true === (jam < 9)) { ...
time = "coffee";
break;
case (jam < 17):
time = "work";
break;
case (jam < 19):
time = "relaxing";
break;
default:
time = "sleep";
}
console.log("This Time for " + time);
console.log(jam);
MyCustomAdapter.java
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);
任何人都知道如何修复此错误?
答案 0 :(得分:0)
您应该将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));
}
});
答案 1 :(得分:0)
在 clickListener 中,使用相应文本视图中的值 在您的减去ClickListener
中if (Integer.parseInt(quantity.getText().toString())!=1){
quantity.setText(""+Integer.parseInt(quantity.getText().toString())+1);
}
在您的加ClickListener
中document.getElementById('main-li').addEventListener("click", function(e) {
if (e.target && e.target.nodeName === "LI") {
let elements = document.getElementById('main-li').children;
for (let i = 0; i < elements.length; ++i) {
elements[i].classList.remove("active-class");
}
e.target.classList.add("active-class");
}
}, true);
答案 2 :(得分:0)
我有同样的问题自己解决了。我使用对象来填充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) {
}