我正在制作一个餐厅应用程序,其中包含食品项目列表,用户可在其中添加或减去食品项目。我的问题是,每当点击添加按钮时,它会增加该食物项值,但是在向下滚动列表或向上滚动之后。它的值变为初始状态。我已经在Stackoverflow和许多其他网站上搜索过有关此问题的所有问题,但没有任何帮助。 我试过ViewHolder,setfocuschangelistener等等 我知道问题是回收由android完成的东西请任何人都可以告诉我该怎么做才能解决我的问题
这是我的自定义适配器类:
public class CustomAdapter extends ArrayAdapter<String> {
String costing;
ImageView add;
int totalCost;
loggedIn loggedIn;
public CustomAdapter(Context context, String[] foods) {
super(context,R.layout.custom_row ,foods);
}
static class ViewHolder{
TextView foodItem;
TextView noOfFoodItem;
ImageView minus;
ImageView add;
TextView cost;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
final ViewHolder holder;
LayoutInflater layoutInflater = LayoutInflater.from(getContext());
if(convertView==null) {
convertView = layoutInflater.inflate(R.layout.custom_row, parent, false);
holder = new ViewHolder();
holder.foodItem = (TextView) convertView.findViewById(R.id.foodItem);
holder.cost = (TextView) convertView.findViewById(R.id.cost);
holder.minus = (ImageView) convertView.findViewById(R.id.minus);
holder.noOfFoodItem = (TextView) convertView.findViewById(R.id.noOfFoodItem);
holder.add = (ImageView) convertView.findViewById(R.id.add);
convertView.setTag(holder);
}else{
holder = (ViewHolder)convertView.getTag();
}
try{
String[] foodItems = {"Burger ","Pizza ","Cold Drinks ",
"Coffee ","Burger ","Pizza ",
"Cold Drinks ","Coffee ","Burger ",
"Pizza ","Cold Drinks ","Coffee ","Burger ",
"Pizza ",
"Cold Drinks ","Coffee "};
holder.foodItem.setText(foodItems[position]);
String[] costs = {"Rs.70","Rs.80","Rs.70","Rs.80","Rs.70","Rs.80","Rs.70","Rs.80","Rs.70","Rs.80","Rs.70","Rs.80"};
holder.cost.setText(costs[position]);
holder.minus.setImageResource(R.drawable.minus);
holder.noOfFoodItem.setText("0");
holder.add.setImageResource(R.drawable.add);
}catch (Exception e){
e.printStackTrace();
}
holder.add.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//Getting items selected of any food item
if(holder.noOfFoodItem.getText().toString().equals("9")){
Toast.makeText(getContext(),"Maximum limit reached!",Toast.LENGTH_SHORT).show();
holder.noOfFoodItem.setText("9");
}else
holder.noOfFoodItem.setText(String.valueOf(Integer.parseInt(holder.noOfFoodItem.getText().toString())+1));
}
});
holder.minus.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(holder.noOfFoodItem.getText().toString().equals("0")){
Toast.makeText(getContext(),"Do you need some medical attention?",Toast.LENGTH_SHORT).show();
holder.noOfFoodItem.setText("0");
}else{
holder.noOfFoodItem.setText(String.valueOf(Integer.parseInt(holder.noOfFoodItem.getText().toString())-1));
}
}
});
return convertView;
}
}
答案 0 :(得分:0)
哟需要保持列表中每个项目的价值不被回收。您可以使用HashMap并将noOfFoodCount放在hasmap中,其中position为position,food count为value。
答案 1 :(得分:0)
您只是在当前文本中添加或减去该值。您必须创建一个用于保存这些计数的arraylist。
创建像ArrayList<Integer> count=new ArrayList<Integer>;
这样的arraylist
然后在添加时,
holder.add.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//Getting items selected of any food item
if(holder.noOfFoodItem.getText().toString().equals("9")){
Toast.makeText(getContext(),"Maximum limit reached!",Toast.LENGTH_SHORT).show();
holder.noOfFoodItem.setText("9");
count.add(9);
}else {
holder.noOfFoodItem.setText(String.valueOf(Integer.parseInt(holder.noOfFoodItem.getText().toString())+1));
count.add(Integer.parseInt(holder.noOfFoodItem.getText().toString())+1);
}
}
});
如果减少计数,
holder.minus.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(holder.noOfFoodItem.getText().toString().equals("0")){
Toast.makeText(getContext(),"Do you need some medical attention?",Toast.LENGTH_SHORT).show();
holder.noOfFoodItem.setText("0");
count.add(0);
}else{
holder.noOfFoodItem.setText(String.valueOf(Integer.parseInt(holder.noOfFoodItem.getText().toString())-1));
count.add(Integer.parseInt(holder.noOfFoodItem.getText().toString())-1);
}
}
});
希望你理解。
答案 2 :(得分:0)
添加全局变量
List<String> foodItems;
将构造函数更改为
public CustomAdapter(Context context, String[] foods) {
super(context,R.layout.custom_row ,foods);
foodItems=Arrays.asList(foods);
}
并修改onClickListerner上的添加按钮阻止
else{
foodItems.add("add your item name here");
holder.noOfFoodItem.setText(foodItems.size());
}
如果您需要任何清晰度,请告诉我