android 如何从编辑文本中获取数据在一个编辑器视图?

csbfibhn  于 2023-10-14  发布在  Android
关注(0)|答案(4)|浏览(132)

我有编辑文本的编辑器视图。每一行都有一个编辑文本。我在编辑文本中手动输入值,输入值后,我想在每一行中获得这些值。我想把这些值放到主活动中。在屏幕视图中可能有10到20行。我想得到这些编辑文本中的所有值。
这是我的适配器

public class SelectItemAdapter extends RecyclerView.Adapter<SelectItemAdapter.ItemHolder> {

    private List<String> itemsName, itemsQty, itemsPCode, itemPlant;
    private OnItemClickListener onItemClickListener;
    private LayoutInflater layoutInflater;

    public SelectItemAdapter(Context context, String[] mDataset) {
        layoutInflater = LayoutInflater.from(context);
        itemsName = new ArrayList<String>();
        itemsQty = new ArrayList<String>();
        itemsPCode = new ArrayList<String>();
        itemPlant = new ArrayList<String>();
        this.mDataset = mDataset;

    }

    @Override
    public SelectItemAdapter.ItemHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View itemView = layoutInflater.inflate(R.layout.custom_row_selected_item, parent, false);
        return new ItemHolder(itemView, this);
    }

    @Override
    public void onBindViewHolder(SelectItemAdapter.ItemHolder holder, final int position) {
        holder.setItemName(itemsName.get(position));
        holder.setItemQty(itemsQty.get(position));
        holder.setItemPCode(itemsPCode.get(position));
        holder.setItemPlant(itemPlant.get(position));

    }

    @Override
    public int getItemViewType(int position) {
        return position;
    }

    @Override
    public int getItemCount() {
        return itemsName.size();
    }

    public Object getItemName(int position) {
        return itemsName.get(position);
    }

    public Object getItemPCode(int position) {
        return itemsPCode.get(position);
    }

    public Object getItemPlant(int position) {
        return itemPlant.get(position);
    }

    public void setOnItemClickListener(OnItemClickListener listener) {
        onItemClickListener = listener;
    }

    public OnItemClickListener getOnItemClickListener() {
        return onItemClickListener;
    }

    public interface OnItemClickListener {
        public void onItemClick(ItemHolder item, int position);
    }

    public void add(int location, String iName, String iQty, String iPCode, String iPlant) {
        itemsName.add(location, iName);
        itemsQty.add(location, iQty);
        itemsPCode.add(location, iPCode);
        itemPlant.add(location, iPlant);
        notifyItemInserted(location);
    }

    public void remove(int location) {
        if (location >= itemsName.size())
            return;

        itemsName.remove(location);
        notifyItemRemoved(location);
    }

    public static class ItemHolder extends RecyclerView.ViewHolder implements View.OnClickListener {

        private SelectItemAdapter parent;
        TextView textItemName, txtPCode, txtAvailableQty, txtTempQty, txtPlant;
        Button bRemove;
        EditText numPicker;

        public ItemHolder(View itemView, SelectItemAdapter parent) {
            super(itemView);
            this.parent = parent;
            textItemName = (TextView) itemView.findViewById(R.id.txtProductName);
            txtAvailableQty = (TextView) itemView.findViewById(R.id.txtAvailbleQty);
            txtPCode = (TextView) itemView.findViewById(R.id.txtPCode);
            txtPlant = (TextView) itemView.findViewById(R.id.txtPlant);
            bRemove = (Button) itemView.findViewById(R.id.bRemove);
            numPicker = (EditText) itemView.findViewById(R.id.numberPicker);
            bRemove.setOnClickListener(this);
        }

        public void setItemName(CharSequence name) {
            textItemName.setText(name);
        }

        public void setItemQty(CharSequence name) {
            txtAvailableQty.setText(name);
        }

        public void setItemPCode(CharSequence name) {
            txtPCode.setText(name);
        }

        public void setItemPlant(CharSequence name) {
            txtPlant.setText(name);
        }

        public String getQtyNumber() {
            return numPicker.getText().toString();
        }

        public CharSequence getItemName() {
            return textItemName.getText();
        }

        public CharSequence getItemPCode() {
            return txtPCode.getText();
        }

        @Override
        public void onClick(View v) {
            final OnItemClickListener listener = parent.getOnItemClickListener();
            if (listener != null) {
                listener.onItemClick(this, getPosition());
            }
        }
    }

}

这就是我试图获得价值观的方式。但这并不奏效

private String getNumPicNumber(int i) {
        if (myRecyclerView.findViewHolderForLayoutPosition(i) instanceof SelectItemAdapter.ItemHolder) {
            SelectItemAdapter.ItemHolder childHolder = (SelectItemAdapter.ItemHolder) myRecyclerView.findViewHolderForLayoutPosition(i);
            numberPickerNumber = childHolder.getQtyNumber();
        }
        return numberPickerNumber;
    }
ao218c7q

ao218c7q1#

你不需要使用这么多的列表,只需要创建一个类,它将包含单个项目的所有数据,不需要按钮,只使用文本更改侦听器。
示例代码

public class RetItem
{
    public String _itemName;
    public String _itemQty;
    public String _itemPcode;
    public String _itemPlant;
}

public class SelectItemAdapter extends RecyclerView.Adapter<SelectItemAdapter.ItemHolder> {

    private List<RetItem> _retData;
     public SelectItemAdapter(Context context, String[] mDataset) {
        layoutInflater = LayoutInflater.from(context);
        _retData = new ArrayList<RetItem>(mDataset.length);
        this.mDataset = mDataset;
    }

      @Override
    public void onBindViewHolder(SelectItemAdapter.ItemHolder holder, final int position) {
        holder.setItemName(itemsName.get(position));
        holder.setItemName.addTextChangedListener(new TextWatcher() {

           public void afterTextChanged(Editable s) {}

           public void beforeTextChanged(CharSequence s, int start,
             int count, int after) {
           }

           public void onTextChanged(CharSequence s, int start, int before, int count) {
             _retData.get(position)._itemName = s.toString();
           }
          });

        holder.setItemQty(itemsQty.get(position));
        holder.setItemQty.addTextChangedListener(new TextWatcher() {

           public void afterTextChanged(Editable s) {}

           public void beforeTextChanged(CharSequence s, int start,
             int count, int after) {
           }

           public void onTextChanged(CharSequence s, int start, int before, int count) {
             _retData.get(position)._itemQty = s.toString();
           }
          });

        holder.setItemPCode(itemsPCode.get(position));
        holder.setItemPCode.addTextChangedListener(new TextWatcher() {

           public void afterTextChanged(Editable s) {}

           public void beforeTextChanged(CharSequence s, int start,
             int count, int after) {
           }

           public void onTextChanged(CharSequence s, int start, int before, int count) {
             _retData.get(position)._itemPcode = s.toString();
           }
          });       
        holder.setItemPlant(itemPlant.get(position));
        holder.setItemPlant.addTextChangedListener(new TextWatcher() {

           public void afterTextChanged(Editable s) {}

           public void beforeTextChanged(CharSequence s, int start,
             int count, int after) {
           }

           public void onTextChanged(CharSequence s, int start, int before, int count) {
             _retData.get(position)._itemPlant = s.toString();
           }
          });
    }

    public List<RetItem> retrieveData()
    {
        return _retData;
    }
}
ozxc1zmp

ozxc1zmp2#

要做到这一点,有两种方法:
1)在此按钮单击时在每一行的QuixlerView中添加保存按钮(onClick)

@Override
        public void onClick(View v) {
           String ans = holher.numPicker.getText().toString();
           // save ans to sharedpreferences or Database
        }

2)将onTextChangedText添加到EditText

holher.mumPicker.addTextChangedListener(new TextWatcher() {

   public void afterTextChanged(Editable s) {}

   public void beforeTextChanged(CharSequence s, int start,
     int count, int after) {
   }

   public void onTextChanged(CharSequence s, int start,
     int before, int count) {
      String ans = holher.numPicker.getText().toString();
           // save ans to sharedpreferences or Database
   }
  });

然后在MainActivity.java中从sharedpreferences或数据库中检索数据

uubf1zoe

uubf1zoe3#

我也有类似的问题,我的编辑器视图包含一个文本视图,两个编辑文本和一个删除按钮,用于从编辑器视图中删除项目。我从两个Edittexts中获取数据,从一个模型类中使用我的Activity中的一个按钮。一切都很好,我只是想知道我可以做什么样的优化。请给予详细的建议。
我的模型类:

public class OrderGS {
  String names, rty,qty;

    public OrderGS(String name, String rty, String qty) {
        this.names = name;
        this.rty = rty;
        this.qty = qty;
    }

    public String getName() {
        return names;
    }

    public void setName(String names) {
        this.names = names;
    }

    public String getRty() {
        return rty;
    }

    public void setRty(String rty) {
        this.rty = rty;
    }

    public String getQty() {
        return qty;
    }

    public void setQty(String qty) {
        this.qty = qty;
    }
}

上面的类包含三个String,一个是titile,两个包含edittext数据。
我的适配器代码:

class OOHolderI extends RecyclerView.ViewHolder{

        TextView title;
        EditText rty,qty;
        public OOHolderI(View itemView) {
            super(itemView);
            title = itemView.findViewById(R.id.text_order);
            rty = itemView.findViewById(R.id.editRTY);
            qty = itemView.findViewById(R.id.editQTY);
            itemView.findViewById(R.id.button_remove).setOnClickListener(v -> {
               arrayList.remove(getAdapterPosition());
               if(rtyArray.size()>0) {
                   rtyArray.remove(getAdapterPosition());
                   qtyArray.remove(getAdapterPosition());
               }
               notifyDataSetChanged();
           });
            rty.addTextChangedListener(new TextWatcher() {
                @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) {
                arrayList.get(getAdapterPosition()).setRty(s.toString());
                if(!rtyArray.contains(getAdapterPosition())){
                rtyArray.add(getAdapterPosition());}
                }
            });
            qty.addTextChangedListener(new TextWatcher() {
                @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) {
                    arrayList.get(getAdapterPosition()).setQty(s.toString());
                    if(!qtyArray.contains(getAdapterPosition())) {
                        qtyArray.add(getAdapterPosition());
                    }
                }

            });

        }

}

在适配器中,我使用两个数组列表rtyArrayqtyArray来保存模型类中Edittext数据的位置。

public ArrayList<Integer> getRTY() {
    return rtyArray;
}
public ArrayList<Integer> getQTY(){
    return qtyArray;
}

上面的两个函数返回Edittextdata的位置
我的活动:

findViewById(R.id.button_save).setOnClickListener(v -> {
            ArrayList<Integer> rtyArray = adapter.getRTY();
            ArrayList<Integer> qtyArray = adapter.getQTY();
            if(rtyArray.size()>0) {
               for(Integer i: rtyArray){
              //get data here like 
              arrayListOfModel.get(i).getRty(); 
              arrayListOfModel.get(qtyArray.get(i)).getQty();
               }
            }
        });
        adapter.notifyDataSetChanged();

在我的例子中,两个位置保持数组的大小总是相等的。

uttx8gqw

uttx8gqw4#

要在ViewClerView中获取单击的文本字段的文本,在ViewHolder中可以获取单击的文本字段的文本。

public class MyHolder extends RecyclerView.ViewHolder {
    private TextView myData;
    public MyHolder(View itemView) {
        super(itemView);
        myData = itemView.findViewById(R.id.txtTitle);

        itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(v.getContext(), "Clicked " + myData.getText(), Toast.LENGTH_LONG).show();
                
            }
        });
    }
}

相关问题