从edittext输入动态地将文本添加到recyclerview

时间:2018-01-28 20:20:01

标签: android android-recyclerview

难以从编辑视图中不断获取值并将其显示在Recyclerview中,而不是另一个。下面的代码使用预设的字符串数组来显示更简单的文本。

Mainactivity

Context context;
RecyclerView recyclerView;
RelativeLayout relativeLayout;
RecyclerView.Adapter recyclerViewAdapter;
RecyclerView.LayoutManager recylerViewLayoutManager;
String[] subjects =
        {
                "First Text",
                "Second Text",
                "Third Text",
                "Fourth Text",
                "Fifth Text",
                "Sixth Text",
        };

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_ACTION_BAR);

    setContentView(R.layout.activity_main);

    context = getApplicationContext();

    relativeLayout = (RelativeLayout) findViewById(R.id.relativelayout1);

    recyclerView = (RecyclerView) findViewById(R.id.recyclerview1);

    recylerViewLayoutManager = new LinearLayoutManager(context);

    recyclerView.setLayoutManager(recylerViewLayoutManager);

    recyclerViewAdapter = new RecyclerViewAdapter(context, subjects);

    recyclerView.setAdapter(recyclerViewAdapter);
}

使用下面的代码尝试使用此代码,但它只更新textview,而不是继续将新文本发布在另一个之下

 public void SendButton(View view){
    // EditText
    EditText editText = (EditText) findViewById(R.id.editText);
    String editTextValue = editText.getText().toString();

    // TextView
    TextView textView = (TextView) findViewById(R.id.subject_textview);
    textView.setText(editTextValue);
    }

适配器用于预设阵列版本

   public RecyclerViewAdapter(Context context1,String[] SubjectValues1){

    SubjectValues = SubjectValues1;
    context = context1;
}

public static class ViewHolder extends RecyclerView.ViewHolder{

    public TextView textView;

    public ViewHolder(View v){

        super(v);

        textView = (TextView)v.findViewById(R.id.subject_textview);
    }
}

@Override
public RecyclerViewAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType){

    view1 = LayoutInflater.from(context).inflate(R.layout.recyclerview_items,parent,false);

    viewHolder1 = new ViewHolder(view1);

    return viewHolder1;
}

@Override
public void onBindViewHolder(ViewHolder holder, int position){

    holder.textView.setText(SubjectValues[position]);
}

@Override
public int getItemCount(){

    return SubjectValues.length;
}

试图让它工作一段时间,但没有运气,所以如果有人帮助我,我将不胜感激。

1 个答案:

答案 0 :(得分:0)

SubjectValues更改为列表,以便能够添加项目。并改变你的构造函数:

 public RecyclerViewAdapter(Context context1,String[] SubjectValues1){

    SubjectValues = new ArrayList<String>(Arrays.asList(SubjectValues1));
    context = context1;
}

然后在适配器中添加方法:

public void addItem(String subject) {
  SubjectValues.add(subject);
  mAdapter.notifyItemInserted(SubjectValues.size());
}

如果您想添加多个值,请使用:

public void addAllItem(List<String> subjects) {
  int startingPosition = SubjectValues.size();
  SubjectValues.addAll(subjects);
  mAdapter.notifyItemInserted(startingPosition, subjects.size());
}

然后像这样更改您的SendButon方法:

public void SendButton(View view){
    // EditText
    EditText editText = (EditText) findViewById(R.id.editText);
    String editTextValue = editText.getText().toString();

     recyclerViewAdapter.addItem(editTextValue )
}