Parsing user input into Recycler view

994 views Asked by At

I'm new to android and I'm trying to let the user input his data using EditText and display it using recycler View. is that possible?

here is the recycler view code I tested by inputting strings to it and it works fine

public class MedicineAdapter extends RecyclerView.Adapter {

List<Medicine> medicineItems;

public MedicineAdapter() {
    super();
    medicineItems = new ArrayList<Medicine>();
    Medicine item = new Medicine();
    item.setTitle("Panadol Extra");
    item.setNotes("Lorem ipsum dolor sit amet, consectetur adipiscing");
    item.setThumbnail(R.drawable.panadol_extra);
    item.setNextD(3);
    item.setDose("afternoon");

    medicineItems.add(item);
}

@Override
public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
    View v = LayoutInflater.from(viewGroup.getContext())
            .inflate(R.layout.medicine_view_card_item, viewGroup, false);
    ViewHolder viewHolder = new ViewHolder(v);
    return viewHolder;
}

@Override
public void onBindViewHolder(ViewHolder viewHolder, int i) {
    Medicine item = medicineItems.get(i);
    viewHolder.tvTitle.setText(item.getTitle());
    viewHolder.tvNotes.setText(item.getNotes());
    viewHolder.tvNextD.setText(Double.toString(item.getNextD()));
    viewHolder.tvDose.setText(item.getDose());
    viewHolder.imgThumbnail.setImageResource(item.getThumbnail());
}

@Override
public int getItemCount() {

    return medicineItems.size();
}

//    class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener{
class ViewHolder extends RecyclerView.ViewHolder {
    public ImageView imgThumbnail;
    public TextView tvTitle;
    public TextView tvNotes;
    public TextView tvDose;
    public TextView tvNextD;

    public ViewHolder(View itemView) {
        super(itemView);
        imgThumbnail = (ImageView) itemView.findViewById(R.id.mthumbnail);
        tvNotes = (TextView) itemView.findViewById(R.id.notes);
        tvTitle = (TextView) itemView.findViewById(R.id.title);
        tvNextD = (TextView) itemView.findViewById(R.id.nextDose);
        tvDose = (TextView) itemView.findViewById(R.id.dose);
    }


}

}

Now I'm trying to use EditText to set the medicine name, notes, and dosage from user input

public class AddMedicineActivity extends ActionBarActivity {

Toolbar toolbar;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_add_medicine);

    toolbar = (Toolbar) findViewById(R.id.tool_bar); // Attaching the layout to the toolbar object
    setSupportActionBar(toolbar);                   // Setting toolbar as the ActionBar with setSupportActionBar() call

    // Adding to the toolbar back button to be enable to return to home screen
    getSupportActionBar().setHomeButtonEnabled(true);
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);
}

public void onClick(View view){

    Intent i = new Intent(this, MedicineAdapter.class);

    final EditText nameInput = (EditText) findViewById(R.id.nameInput);
    final EditText notesInput = (EditText) findViewById(R.id.notesInput);
    final EditText doseInput = (EditText) findViewById(R.id.doseInput);

    String userName = nameInput.getText().toString();
    String userNotes = notesInput.getText().toString();
    String userDose = doseInput.getText().toString();

    i.putExtra("nameMassege",userName);
    i.putExtra("notesMassege",userNotes);
    i.putExtra("doseMassege",userDose);

    startActivity(i);
}

for some reasons when I try to use getintent().getExtras(); in the recycler view activity to use the intent it gives me error

am I doing something wrong?

0

There are 0 answers