Recyclerview editText显示列表项位置,项目为“项目1”而不是“项目0”

时间:2018-05-02 13:32:08

标签: android android-recyclerview android-edittext recycler-adapter android-textwatcher

您好我有这个适配器类在recyclerview中显示editText它工作正常,但我想要做的是更改编辑文本显示的列表项位置,因为我使用数组它显示第一项为“0”, 所以如何从“1”开始计数,这样我就可以提示用户输入为“项目1”,而不是以“项目0”开头。 另外,为了保存我使用textwatcher的信息。

public class CustomAdapter extends RecyclerView.Adapter<CustomAdapter.MyViewHolder> 
{
    private LayoutInflater inflater;
    public static ArrayList<EditModel> editModelArrayList;


    public CustomAdapter(Context ctx, ArrayList<EditModel> editModelArrayList){

        inflater = LayoutInflater.from(ctx);
        this.editModelArrayList = editModelArrayList;
    }

    @Override
    public CustomAdapter.MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

        View view = inflater.inflate(R.layout.rv_item, parent, false);
        MyViewHolder holder = new MyViewHolder(view);

        return holder;
    }

    @Override
    public void onBindViewHolder(final CustomAdapter.MyViewHolder holder, final int position) {
        holder.editText.setText(" Item  : "+editModelArrayList.get(position).getEditTextValue());

    }

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

    class MyViewHolder extends RecyclerView.ViewHolder
    {
        protected EditText editText;

        public MyViewHolder(View itemView) {
            super(itemView);

            editText = (EditText) itemView.findViewById(R.id.editid);
            editText.addTextChangedListener(new TextWatcher() {
                @Override
                public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                }
                @Override
                public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                    editModelArrayList.get(getAdapterPosition()).setEditTextValue(editText.getText().toString());
                }
                @Override
                public void afterTextChanged(Editable editable) {
                    editModelArrayList.get(getAdapterPosition()).setEditTextValue(editText.getText().toString());
                }
            });

        }
    }
}

1 个答案:

答案 0 :(得分:0)

@Override
public void onBindViewHolder(final CustomAdapter.MyViewHolder holder, final 
int position) {
    holder.editText.setText(" Item  : "+ (Integer.parseInt(editModelArrayList.get(position))+1));

}