gpt4 book ai didi

java - 如何在单击 ListView 按钮时从 ListView 中获取每个 edittext 值

转载 作者:行者123 更新时间:2023-11-30 10:46:40 26 4
gpt4 key购买 nike

我有 ListView 。我的自定义适配器中的每个列表都包含复选框、编辑文本和两个按钮。当我点击加号按钮时,我想增加相应位置的编辑文本值。我不知道如何在单击加号按钮时从 listView 获取特定位置的 edittext 值。

请任何人帮助我!

这是我的代码...

public class MenuListCustomAdapter extends BaseAdapter implements ListAdapter {
private ArrayList<String> list = new ArrayList<String>();
private ArrayList<String> Amtlist = new ArrayList<String>();
private Context context;
TextView listItemText;
TextView listAmtText;
EditText qty;
CheckBox checkBox;
ImageView plus;
ImageView minus;

public MenuListCustomAdapter(ArrayList<String> list, ArrayList<String> Amtlist, Context context) {
this.list = list;
this.Amtlist = Amtlist;
this.context = context;

}
@Override
public int getCount() {
return list.size();

}

@Override
public Object getItem(int position) {
return list.get(position);
}

@Override
public long getItemId(int position) {
return position;
}

@Override
public View getView(final int position, View convertView, ViewGroup parent) {
View view = convertView;



if (view == null) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inflater.inflate(R.layout.menu_custom_fragment, null);
}
listItemText = (TextView)view.findViewById(R.id.menu_list_item_string);
listAmtText = (TextView)view.findViewById(R.id.menu_amt_list_item);
qty = (EditText)view.findViewById(R.id.quantity);
checkBox = (CheckBox)view.findViewById(R.id.chckbox);
plus = (ImageView)view.findViewById(R.id.add);
minus = (ImageView)view.findViewById(R.id.minus);

qty.setText("1");
qty.setTag(position);
listItemText.setText(list.get(position));
listAmtText.setText(Amtlist.get(position));

plus.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {

String value = // How to get particular position edittext value
int count = Integer.parseInt(value)+1;
Log.d("value", String.valueOf(count));
qty.setText(Integer.toString(count));
}
});

return view;
}

My page here :

提前致谢!

最佳答案

尝试这样做以避免 ListView 重复,因为 ListView 不保证您添加到那里的项目的唯一性,Android 重用 UI 对象。

用列表的元素创建一个新类:

public class YourObject {
private String listElement;
private String amListElement;
private int count;

public YourObject(String listElement, String amListElement) {
this.listElement = listElement;
this.amListElement = amListElement;
this.count = 1;
}

public String getListElement() {
return listElement;
}

public void setListElement(String listElement) {
this.listElement = listElement;
}

public String getAmListElement() {
return amListElement;
}

public void setAmListElement(String amListElement) {
this.amListElement = amListElement;
}

public int getCount() {
return count;
}

public void setCount(int count) {
this.count = count;
}

将一个带有对象(模型)的 ArrayList 传递给适配器:

public MenuListCustomAdapter(ArrayList<YourObject> list, Context context) {
this.list = list;
this.context = context;
}
//....
qty.setText(list.get(position).getCount());
qty.setTag(position);
listItemText.setText(list.get(position).getListElement());
listAmtText.setText(Amtlist.get(position).getAmListElement());

plus.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {

int count = list.get(position).getCount()+1;
list.get(position).setCount(count);
Log.d("value", String.valueOf(count));
qty.setText(""+count);
notifyDataSetChanged();
}
});

获取特定位置的edittext值:

list.get(position).getCount();

如果你想得到edittext的值,可能会得到我一开始说的错误。

关于java - 如何在单击 ListView 按钮时从 ListView 中获取每个 edittext 值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36420695/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com