1

Having difficulty constantly getting values from the editview and displaying it in the recyclerview one below the other. The code below uses a preset array of string to display the text which is simpler.

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

Tried to this using the code below but it only updates the textview rather than keep posting new text one below the other

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

The adapter is for the preset array version

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

Been trying to get this to work for a while, but no luck, so would appreciate if someone helped me with this.

George
  • 25
  • 2
  • 9

1 Answers1

0

Change SubjectValues to List in order to be able to add items. And change your constructor like this:

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

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

Then add method in your Adapter:

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

If you want to add more then one value use this:

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

Then change your SendButon method like this:

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

     recyclerViewAdapter.addItem(editTextValue )
}
MeLean
  • 3,092
  • 6
  • 29
  • 43