我在适配器中有一个edittext来设置项目的数量。每个项目的初始数量设置为0.现在我希望在编辑文本字段完成编辑后,输入将被设置为该项目的数量。我试过onclicklistener但它没有用。我发布了我的适配器和项目类。让我知道如何做到这一点。
我的适配器:
package com.res.easyorder;
import android.content.Context;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.inputmethod.EditorInfo;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import java.util.ArrayList;
import java.util.zip.Inflater;
public class MyAdapter extends ArrayAdapter<item> {
int q_ty = 0;
ArrayList<item> foodlist = new ArrayList<>();
public MyAdapter(Context context, int textViewResourceId, ArrayList<item> objects){
super(context, textViewResourceId, objects);
foodlist = objects;
}
@Override
public int getCount(){
return super.getCount();
}
@Override
public View getView(final int position, View convertView, ViewGroup parent){
View v = convertView;
LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = inflater.inflate(R.layout.list_view_layout, null);
ImageView foodpic = (ImageView) v.findViewById(R.id.foodpic);
TextView foodname = (TextView) v.findViewById(R.id.foodname);
TextView foodprice = (TextView) v.findViewById(R.id.foodprice);
Button plus = (Button) v.findViewById(R.id.plus);
final EditText quantity = (EditText) v.findViewById(R.id.quantity);
Button minus = (Button) v.findViewById(R.id.minus);
plus.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
item item = getItem(position);
q_ty = Integer.parseInt(item.getQuantity());
q_ty = q_ty + 1;
item.setQuantity(String.valueOf(q_ty));
quantity.setText(item.getQuantity());
}
});
minus.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
item item = getItem(position);
q_ty = Integer.parseInt(item.getQuantity());
if (q_ty > 0) {
q_ty = q_ty - 1;
item.setQuantity(String.valueOf(q_ty));
quantity.setText(item.getQuantity());
}
}
});
foodname.setText(foodlist.get(position).getFoodName());
foodpic.setImageResource(foodlist.get(position).getFoodImage());
foodprice.setText("BDT: " +foodlist.get(position).getFoodPrice());
quantity.setText(foodlist.get(position).getQuantity());
return v;
}
}
项目类:
package com.res.easyorder;
/**
* Created by User on 02-Apr-18.
*/
public class item {
String foodName, quantity;
int foodImage, foodPrice;
public item(String foodName, int foodImage, int foodPrice, String quantity)
{
this.foodName = foodName;
this.foodImage = foodImage;
this.foodPrice = foodPrice;
this.quantity = quantity;
}
public void setQuantity(String quantity)
{
this.quantity = quantity;
}
public String getFoodName()
{
return foodName;
}
public int getFoodImage()
{
return foodImage;
}
public int getFoodPrice()
{
return foodPrice;
}
public String getQuantity()
{
return quantity;
}
}
答案 0 :(得分:0)
editText.setOnFocusChangeListener(new OnFocusChangeListener() {
@Override
public void onFocusChange(View v, boolean hasFocus) {
if(!hasFocus){
//this if condition is true when edittext lost focus...
// do your changes in data and flag here
editText.setText("10");
//then you can use notifyDataSetChanged or notifyItemChanged
}
}
});