如何在RecyclerView中的EditText中跟踪值

时间:2017-01-13 17:33:30

标签: android android-recyclerview

很抱歉,如果我没有为问题选择明确的标题。这是情况。我必须在下面的屏幕上实现,它通过网络获取产品名称。

enter image description here

这3个白色矩形有3个EditText,可由用户编辑。如果用户编辑的一个Item中的那3个EditText中的任何一个我想将该对象存储在不同的ArrayList中。(每个列表项由产品对象表示)。

例如,考虑用户编辑的“Alzel 400mg”值。当用户使用其他网络呼叫搜索产品以“Al”开头时,将采取相关数据,并在清除旧列表后应用于此列表视图。在此过程之后,我想显示“Alzel 400mg”产品,该产品的用户输入值较早。为此,我需要跟踪所有用户编辑的产品并将其与新产品进行比较,并对对象进行相关更新并应用于列表视图。

我希望你们明白我想要达到的目标。因为我有大约12000种产品,所以我没有采用所有产品。我一次只得到20个并将它们添加到ResylerView。

这是适用于listView的适配器。

return num

正如您所看到的,我已实现TextWatcher类以防止输入值分散表单列表滚动时的项目。我应该能够使用public class ProductListAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> { private LayoutInflater inflater; private ArrayList<Products> listData; private ArrayList<Products> editedData; private Context context; private ItemClickListener itemClickListener; private static final int ITEM = 0; private static final int LOADING = 1; private static final int QUANTITY = 5; private static final int FREE_QUANTITY = 10; private static final int DISCOUNT = 15; //private String[] quantitiy1Data; public ProductListAdapter(ArrayList<Products> listData, Context context) { this.listData = listData; this.context = context; editedData = new ArrayList<>(); inflater = LayoutInflater.from(this.context); } public void setItemClickListener(ItemClickListener itemClickListener) { this.itemClickListener = itemClickListener; } public ArrayList<Products> getListData() { return listData; } public ArrayList<Products> getEditedData() { return editedData; } public void updateData(ArrayList<Products> data) { this.listData = data; notifyDataSetChanged(); } @Override public int getItemViewType(int position) { return listData.get(position) == null ? LOADING : ITEM; } @Override public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { RecyclerView.ViewHolder viewHolder = null; switch (viewType) { case ITEM: viewHolder = getViewHolder(parent, inflater); break; case LOADING: View v2 = inflater.inflate(R.layout.list_footer, parent, false); viewHolder = new LoadingVH(v2); break; } return viewHolder; } @Override public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) { Products productObject = listData.get(position); switch (getItemViewType(position)) { case ITEM: String productName = productObject.getProductName(); String quantityValue = productObject.getQuantity(); String freeQuantityValue = productObject.getFreeQuantity(); String discountValue = productObject.getDiscount(); ContentViewHolder movieVH = (ContentViewHolder) holder; movieVH.productName.setText(productName); movieVH.quantityEditTextListener.updatePosition(movieVH.getLayoutPosition()); movieVH.quantity.setText(quantityValue); movieVH.freeQuantityEditTextListener.updatePosition(movieVH.getLayoutPosition()); movieVH.freeQuantity.setText(freeQuantityValue); movieVH.discountEditTextListener.updatePosition(movieVH.getLayoutPosition()); movieVH.discount.setText(discountValue); movieVH.quantity.setOnEditorActionListener(new TextView.OnEditorActionListener() { @Override public boolean onEditorAction(TextView v, int actionId, KeyEvent event) { return false; } }); break; case LOADING: //Do nothing break; } } @Override public int getItemCount() { return listData == null ? 0 : listData.size(); } @NonNull private RecyclerView.ViewHolder getViewHolder(ViewGroup parent, LayoutInflater inflater) { RecyclerView.ViewHolder viewHolder; View view = inflater.inflate(R.layout.custom_product_list_item, parent, false); viewHolder = new ContentViewHolder(view, new MyCustomEditTextListener() , new MyCustomEditTextListener(), new MyCustomEditTextListener()); return viewHolder; } /** * View holder for main container */ public class ContentViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener { private TextView productName; private EditText quantity; private EditText freeQuantity; private EditText discount; private MyCustomEditTextListener quantityEditTextListener; private MyCustomEditTextListener freeQuantityEditTextListener; private MyCustomEditTextListener discountEditTextListener; public ContentViewHolder(View itemView, MyCustomEditTextListener textListener , MyCustomEditTextListener textListener2, MyCustomEditTextListener textListener3) { super(itemView); itemView.setOnClickListener(this); productName = (TextView) itemView.findViewById(R.id.product_name_data); quantity = (EditText) itemView.findViewById(R.id.quantity_1_edit_text); freeQuantity = (EditText) itemView.findViewById(R.id.quantity_2_edit_text); discount = (EditText) itemView.findViewById(R.id.quantity_3_edit_text); quantityEditTextListener = textListener; quantityEditTextListener.setEditTextType(QUANTITY); freeQuantityEditTextListener = textListener2; freeQuantityEditTextListener.setEditTextType(FREE_QUANTITY); discountEditTextListener = textListener3; discountEditTextListener.setEditTextType(DISCOUNT); this.quantity.addTextChangedListener(quantityEditTextListener); this.freeQuantity.addTextChangedListener(freeQuantityEditTextListener); this.discount.addTextChangedListener(discountEditTextListener); } @Override public void onClick(View v) { if (itemClickListener != null) { //itemClickListener.onItemClick(v,this.getLayoutPosition()); } } } /** * View holder to display loading list item */ protected class LoadingVH extends RecyclerView.ViewHolder { public LoadingVH(View itemView) { super(itemView); } } /** * textWatcher for to keep track of changed data. */ private class MyCustomEditTextListener implements TextWatcher { private int position; private int type; public void updatePosition(int position) { this.position = position; } public void setEditTextType(int type) { this.type = type; } @Override public void beforeTextChanged(CharSequence charSequence, int i, int i2, int i3) { // no op } @Override public void onTextChanged(CharSequence charSequence, int i, int i2, int i3) { if (type == QUANTITY) { listData.get(position).setQuantity(charSequence.toString()); } else if (type == FREE_QUANTITY) { listData.get(position).setFreeQuantity(charSequence.toString()); } else if (type == DISCOUNT) { listData.get(position).setDiscount(charSequence.toString()); } } @Override public void afterTextChanged(Editable s) { } } public interface ItemClickListener { void onItemClick(View v, int position); } } 将更改对象添加到新的数组列表中,但它声明要在数组列表中复制值。还有一些如何在列表初始加载时触发该方法,这使得很难实现我想要的。请帮忙。

1 个答案:

答案 0 :(得分:0)

为防止重复,您可能需要使用HashMap / HashSet代替ArrayList来存储已编辑的项目。