在Recyclerview中获取值编辑文本

时间:2016-05-07 03:34:44

标签: android json android-edittext android-recyclerview

我有一个recyclerview。我手动将项目(行)添加到recyclerview。每行都有文本视图和编辑文本,我需要输入一个值。将项目添加到recyclerview后,我需要使用文本视图名称(如购物车)获取输入的值。我知道如何获取文本视图值。问题是获取编辑文本值。我只能获得前五个edittext视图'值。对于其他人,它给了我第五个位置编辑文本值。我使用Json格式将数据发送到服务器。我正在构建一个json并向其添加项目。所以我需要添加编辑文本值。 滚动时没有价值交换问题。我唯一需要的是获取所有编辑文字值。

示例

Quantity : 1
Quantity : 2
Quantity : 3
Quantity : 4
Quantity : 5
Quantity : 5
Quantity : 5
Quantity : 5
Quantity : 5
Quantity : 5

主要课程

private class SendItemAsync extends AsyncTask<Void, Void, Void> {
        @Override
        protected void onPreExecute() {
        }

        @Override
        protected Void doInBackground(Void... arg0) {

            //Create JSON string start
            json_string = "{\"sending_items\":[";

            for (int i = 0; i < myRecyclerViewAdapter.getItemCount(); i++) {

                //Repeat and loop this until all objects are added (and add try+catch)
                try {
                    JSONObject obj_new = new JSONObject();
                    obj_new.put("order_no", orderIdForItemTable);
                    obj_new.put("items", myRecyclerViewAdapter.getItemName(i).toString());
                    obj_new.put("items_no", myRecyclerViewAdapter.getItemPCode(i).toString());
                    obj_new.put("plant", myRecyclerViewAdapter.getItemPlant(i).toString());
                    obj_new.put("quantity", getNumPicNumber(i));
                    json_string = json_string + obj_new.toString() + ",";

                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            //Close JSON string
            json_string = json_string.substring(0, json_string.length() - 1);
            json_string += "]}";

            HttpParams httpParams = new BasicHttpParams();
            HttpConnectionParams.setConnectionTimeout(httpParams, 3500);
            HttpConnectionParams.setSoTimeout(httpParams, 1000);
            HttpClient client = new DefaultHttpClient(httpParams);

            String url = AppConfig.URL_ITEMS_SEND;

            HttpPost request = new HttpPost(url);
            try {
                request.setEntity(new ByteArrayEntity(json_string.getBytes("UTF8")));
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }
            request.setHeader("json", json_string);
            request.setHeader("Accept", "application/json");
            request.setHeader("Content-Type", "application/json");
            Log.i("", "excuting request");
            HttpResponse response = null;
            try {
                response = client.execute(request);
                Log.d("HTTP Response", response.getStatusLine().toString());
                try {
                    String responseBody = EntityUtils.toString(response.getEntity());
                    Log.d("Server Response", responseBody);
                } catch (IOException e) {
                    e.printStackTrace();
                }
            } catch (IOException e) {
                e.printStackTrace();

            }
            return null;
        }

        @Override
        protected void onPostExecute(Void aVoid) {
            finish();
            Bundle basket = new Bundle();
            basket.putString("dealerName", dealerName);
            basket.putString("orderNo", newOrderId);
            basket.putString("jsonString", json_string);
            Intent intent = new Intent(SelectItem.this, ItemCart.class);
            intent.putExtras(basket);
            startActivity(intent);
            finish();
            //Toast.makeText(getApplicationContext(), json_string, Toast.LENGTH_LONG).show();
        }
    }

    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;
    }

我的适配器类

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

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

    public SelectItemAdapter(Context context) {
        layoutInflater = LayoutInflater.from(context);
        itemsName = new ArrayList<String>();
        itemsQty = new ArrayList<String>();
        itemsPCode = new ArrayList<String>();
        itemPlant = new ArrayList<String>();
    }

    @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, 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());
            }
        }
    }

}
到目前为止还不好。我被困在这里。

1 个答案:

答案 0 :(得分:0)

这是我认为你应该做的;

在RecyclerView Adapter类中声明全局数组变量数量; 在构造函数中实例化它,并使用您传递给adpter的项目的大小。

然后在你的onbind观点;将textchangelistener添加到数字选择器中,并在文本更改后获取该位置的文本并将其存储在数量全局变量中。

创建一个getter helper方法,您将在适配器实例上调用该方法以返回所有quanties 这些数量将传递给AsyncTask进行处理

SelectItemAdapter selectItemAdapter =  new SelectItemAdapter();
int quantities [] = selectItemAdapter.getQuantities();

在创建adpater实例的类中,调用getter方法获取数量

static_cast

然后可以将数量传递给您的AsyncTask类