我在代码中使用了 ExpandebleListView
,在 ADD Button
点击时动态生成了行。每个新行包含两个 EditText
。
现在,当我单击清除Button
时,它会清除list
个项目,但是当我单击“添加新项目”时,它的会显示具有最新输入值的新项目在EditText
内部。
那么,如何清除所有内容和项目而不更改Button
Click
上的活动?
这是我的代码,用于清除数据
itemBean.clear();
ItemBean objBean = new ItemBean();
objBean.Qty = "";
objBean.Rate = "";
objBean.Total = "";
itemBean.add(objBean);
listviewAdapterForItem.notifyDataSetChanged();
这是我的添加数据代码
ItemBean objBean = new ItemBean();
objBean.Qty = "";
objBean.Rate = "";
objBean.Total = "";
itemBean.add(objBean);
listviewAdapterForItem.notifyDataSetChanged();
这是我的适配器代码
public class ListviewAdapterForItem extends BaseAdapter {
Context context;
ArrayList<ItemBean> bean;
public ListviewAdapterForItem(Context context, ArrayList<ItemBean> itemBeans) {
this.context = context;
if(this.bean != null){
this.bean.clear();
}
this.bean = itemBeans;
}
@Override
public int getCount() {
return bean.size();
}
@Override
public Object getItem(int position) {
return bean.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
final ViewHolder viewHolder;
if (convertView == null){
LayoutInflater layoutInflater = (LayoutInflater)context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
convertView = layoutInflater.inflate(R.layout.list_row_items,null);
viewHolder = new ViewHolder();
viewHolder.editQty = convertView.findViewById(R.id.editQty);
viewHolder.editRate = convertView.findViewById(R.id.editRate);
viewHolder.txtRs = convertView.findViewById(R.id.txtRs);
convertView.setTag(viewHolder);
}else {
viewHolder = (ViewHolder)convertView.getTag();
}
ItemBean objBean = (ItemBean) getItem(position);
if(objBean != null) {
viewHolder.editQty.setText(objBean.getQty());
viewHolder.editRate.setText(objBean.getRate());
viewHolder.txtRs.setText(objBean.getTotal());
}
}
}
答案 0 :(得分:1)
我认为您的代码中存在一些逻辑错误,首先是为了清楚
bean.clear();
ItemBean objBean = new ItemBean();
objBean.Qty = "";
objBean.Rate = "";
objBean.Total = "";
bean.add(objBean);
listviewAdapterForItem.notifyDataSetChanged();
要添加,因为 bean 是您的商品列表
ItemBean objBean = new ItemBean();
objBean.Qty = "abc"; // some dummy values to test
objBean.Rate = "xyz";
objBean.Total = "abc";
bean.add(objBean);
listviewAdapterForItem.notifyDataSetChanged();
如果要清除所有项目,则应这样做
bean.clear(); // it will clear all your list, what you are doing is clearing or removing one item
在您的getView()
函数中,获取编辑文本的引用并在那里清除它
viewHolder.editQty= (EditText) convertView.findViewById(R.id.exitText);// change editext accordingly
editText.getText().clear();
或者其他解决方案可以声明一些类型为String
的xyz变量,像String xyz = ""
一样将其初始化为空,然后
editText.setText(xyz);
希望这会有所帮助
答案 1 :(得分:1)
您没有为Editext
设置任何值,您应该在getView()
上执行以下操作
@Override
public View getView(int position, View convertView, ViewGroup parent) {
final ViewHolder viewHolder;
if (convertView == null){
LayoutInflater layoutInflater = (LayoutInflater)context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
convertView = layoutInflater.inflate(R.layout.list_row_items,null);
viewHolder = new ViewHolder();
viewHolder.editQty = convertView.findViewById(R.id.editQty);
viewHolder.editRate = convertView.findViewById(R.id.editRate);
viewHolder.txtRs = convertView.findViewById(R.id.txtRs);
convertView.setTag(viewHolder);
}else {
viewHolder = (ViewHolder)convertView.getTag();
}
String Qty =bean.get(position).Qty;
viewHolder.editQty.setText(Qty);
viewHolder.editQty.addTextChangedListener(new onEditTextchange(holder.editQty, position));
}
public class onEditTextchange implements TextWatcher {
EditText getvalue;
int pos;
public onEditTextchange(EditText getvalue, int position) {
this.getvalue = getvalue;
this.pos = position;
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
@Override
public void afterTextChanged(Editable s) {
ItemBean objBean = new ItemBean();
objBean.Qty = getvalue.getText().toString();
objBean.Rate = items.get(pos).question;
objBean.Total = items.get(pos).question_id;
bean.set(pos, objBean);
}
}
希望这会有所帮助