RecyclerView shows previous values entered in an EditText in new rows

后端 未结 2 787
执念已碎
执念已碎 2021-01-21 14:40

I\'m creating an android app, in which I\'m using recyclerView and the row of recyclerView is having editText.

This is my ReadingAdapter<

2条回答
  •  陌清茗
    陌清茗 (楼主)
    2021-01-21 15:39

    Your Activity Code:

    ListView listview = (ListView) findViewById(R.id.list_view);
    listview.setItemsCanFocus(true);
    Adapter adapter = new Adapter (YourActivity.this, YourArrayList);
    listview .setAdapter(adapter);
    

    Adapter class

    public class Adapter extends BaseAdapter {
    
    // Declare Variables \\
    Context mContext;
    LayoutInflater inflater;
    Activity act;
    String[] temp;
    
    
    public Adapter(Context context, ArrayList list) {
        mContext = context;
        inflater = LayoutInflater.from(mContext);
        act = (Activity) context;
        //-------Temp String Array-------\\
        temp = new String[this.count];
        for (int i = 0; i < this.count; i++) {
            temp[i] = list.get(i);
        }
        //---------------------------\\
    
    }
    
    public class ViewHolder {
        TextView optionTitle;
        EditText optionText;
        int ref;
    }
    
    @Override
    public int getCount() {
        return list.size;
    }
    
    @Override
    public Object getItem(int position) {
        return temp[position];
    }
    
    @Override
    public long getItemId(int position) {
        return position;
    }
    
    public View getView(final int position, View view, ViewGroup parent) {
        final ViewHolder holder;
        if (view == null) {
            holder = new ViewHolder();
            view = inflater.inflate(R.layout.lv_items_add_ques_options_mcq, null);
            holder.optionTitle = (TextView) view.findViewById(R.id.add_ques_opts_count_mcq_tv);
            holder.optionText = (EditText) view.findViewById(R.id.add_ques_opts_title_mcq_et);
            view.setTag(holder);
        } else {
            holder = (ViewHolder) view.getTag();
        }
        holder.ref = position;
    
        holder.optionTitle.setText(getCharForNumber(position) + ":");
    
        holder.optionText.setText(temp[position]);
        holder.optionText.addTextChangedListener(new TextWatcher() {
    
            @Override
            public void onTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {
            }
    
            @Override
            public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
                                          int arg3) {
            }
    
            @Override
            public void afterTextChanged(Editable arg0) {
                temp[holder.ref] = arg0.toString().trim();
            }
        });
    
        return view;
    }
    
    public void getList() {
        StaticValues.arrayListOptions = new ArrayList(Arrays.asList(temp));
        StaticValues.arrayListOptionsCount = new ArrayList();
        for (int i = 0; i < count; i++) {
            StaticValues.arrayListOptionsCount.add(String.valueOf(i+1));
            Log.e("err_al", StaticValues.arrayListOptions.get(i));
            Log.e("err_al", StaticValues.arrayListOptionsCount.get(i));
        }
    }
    
    private String getCharForNumber(int i) {
        char[] alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ".toCharArray();
        if (i > 25) {
            return null;
        }
        return Character.toString(alphabet[i]);
    }}
    

提交回复
热议问题