代码之家  ›  专栏  ›  技术社区  ›  Umeshkumar D.

同一活动中EditText不清除值的动态Listview

  •  1
  • Umeshkumar D.  · 技术社区  · 6 年前

    我用过 ExpandebleListView 在我的代码中,行是在 添加 Button EditText .

    清除 按钮 ,很清楚 list 显示最后输入值在内部的新 编辑文本 .

    因此,如何在不更改活动的情况下清除所有内容和项 按钮 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());
        }
    }
    }
    
    2 回复  |  直到 6 年前
        1
  •  1
  •   Abdul Kawee    6 年前

    我认为你的代码中有一些逻辑错误,首先是为了清楚

    bean.clear();
    ItemBean objBean = new ItemBean();
    objBean.Qty = "";
    objBean.Rate = "";
    objBean.Total = "";
    bean.add(objBean);
    listviewAdapterForItem.notifyDataSetChanged();
    

    加上因为 是你的物品清单

      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 初始化为空的 String xyz = "" 然后

    editText.setText(xyz);
    

    希望这有帮助

        2
  •  1
  •   Sukhbir    6 年前

    你没有给你的 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);
            }
        }
    

    希望这会有帮助