Wladislaw
Wladislaw

Reputation: 1250

Adding Data to Recyclerview by user

I am having troubles on adding data to my recyclerview by user. I don't want to have it hardcoded. I am having a fab, which opens alert dialog with 3 input fields, where user should type in exam_name, exam_desc and exam_author. These 3 fields should be written to 3 different strings, which are called by ItemsDate. This should create a new card with the strings user typed in. Probably i am doing something wrong, cause i can get it only hardcoded. If someone could tell me, what i am missing, or maybe something i had to add somewhere else, i would really appreciate it. I am beginner at coding on android. Thanks in advance!

Here is my MainActivity.java

final Context context = this;
protected String Name1 = String.valueOf(R.id.exam_name);
protected String Desc1 = String.valueOf(R.id.exam_desc);
protected String Author1 = String.valueOf(R.id.exam_author);
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
    RecyclerView recyclerView = (RecyclerView) findViewById(R.id.cardList);
    ItemData itemsData[] = { new ItemData(Name1, Desc1, Author1)
    };
    recyclerView.setLayoutManager(new LinearLayoutManager(this));
    recyclerView.setHasFixedSize(true);
    MyAdapter mAdapter = new MyAdapter(itemsData);
    recyclerView.setAdapter(mAdapter);
    recyclerView.setItemAnimator(new ScaleInAnimator());

    fab.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            LayoutInflater factory = LayoutInflater.from(context);

            final View textEntryView = factory.inflate(R.layout.text_entry, null);

            final EditText input1 = (EditText) textEntryView.findViewById(R.id.exam_namein);
            final EditText input2 = (EditText) textEntryView.findViewById(R.id.exam_descin);
            final EditText input3 = (EditText) textEntryView.findViewById(R.id.exam_authorin);
            input1.setText("", TextView.BufferType.EDITABLE);
            input2.setText("", TextView.BufferType.EDITABLE);
            input3.setText("", TextView.BufferType.EDITABLE);

            final AlertDialog.Builder alert = new AlertDialog.Builder(context);
            alert.setIcon(R.drawable.ic_input_blue_grey_500_24dp).setTitle("Create new exam").setView(textEntryView).setPositiveButton(R.string.save,
                    new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog,
                                            int whichButton) {

                            Name1 = input1.getText().toString();
                            Desc1 = input2.getText().toString();
                            Author1 = input3.getText().toString();

                            Snackbar snackbar = Snackbar.make(findViewById(android.R.id.content), "New Theme: "+Name1 +" created successful.", Snackbar.LENGTH_LONG);
                            snackbar.setActionTextColor(Color.RED);
                            View sbView = snackbar.getView();
                            TextView textView = (TextView) sbView.findViewById(android.support.design.R.id.snackbar_text);
                            textView.setTextColor(getResources().getColor(R.color.colorPrimary));
                            snackbar.show();
                        }
                    }).setNegativeButton(getString(R.string.cancel),
                    new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog,
                                            int whichButton) {
                            Snackbar snackbar = Snackbar.make(findViewById(android.R.id.content), "You canceled, theme was not created", Snackbar.LENGTH_SHORT);
                            // Changing action button text color
                            View sbView = snackbar.getView();
                            TextView textView = (TextView) sbView.findViewById(android.support.design.R.id.snackbar_text);
                            textView.setTextColor(getResources().getColor(R.color.md_red_500));
                            snackbar.show();

                        }
                    });
            alert.show();
        }
    });
...

Here is MyAdapter.java

public ItemData[] itemsData;
public MyAdapter(ItemData[] itemsData) {
    this.itemsData = itemsData;
}
@Override
public MyAdapter.ViewHolder onCreateViewHolder(ViewGroup viewGroup,
                                               int viewType) {
    View itemLayoutView = LayoutInflater.from(viewGroup.getContext())
            .inflate(R.layout.card_layout, viewGroup, false);
    ViewHolder viewHolder = new ViewHolder(itemLayoutView);
    return viewHolder;
}
@Override
public void onBindViewHolder(ViewHolder viewHolder, int position) {
    viewHolder.txtViewTitle.setText(itemsData[position].getTitle());
    viewHolder.txtViewTitle2.setText(itemsData[position].getDesc());
    viewHolder.txtViewTitle3.setText(itemsData[position].getAuthor());
}

@Override
public void onClick(View v) {

}
public static class ViewHolder extends RecyclerView.ViewHolder {

    public TextView txtViewTitle;
    public TextView txtViewTitle2;
    public TextView txtViewTitle3;

    public ViewHolder(View itemLayoutView) {
        super(itemLayoutView);
        txtViewTitle = (TextView) itemLayoutView.findViewById(R.id.exam_name);
        txtViewTitle2 = (TextView) itemLayoutView.findViewById(R.id.exam_desc);
        txtViewTitle3 = (TextView) itemLayoutView.findViewById(R.id.exam_author);
    }
}
@Override
public int getItemCount() {
    return itemsData.length;
}
}

Here is ItemData.java

public class ItemData {
private String examname1;
private String examdesc1;
private String examauthor1;

public ItemData(String examname1,String examdesc1, String examauthor1){
    this.examname1 = examname1;
    this.examdesc1 = examdesc1;
    this.examauthor1 = examauthor1;

}
public String getTitle() {
    return examname1;
}
public String getDesc() {
    return examdesc1;
}
public String getAuthor() {
    return examauthor1;
}
}

I think the layouts are not needed for my purpose. Thanks a lot!!!

Upvotes: 1

Views: 753

Answers (1)

yennsarah
yennsarah

Reputation: 5517

Since Arrays don't allow you to add data easily, I recommend changing itemsData to an ArrayList<ItemData>.

After you get your EditText data and save it into Strings, you need to add it your Dataset itemsData,

itemsData.add(new ItemData(Name1, Desc1, Author1));  

then tell your Adapter that your data has changed with

mAdapter.notifyDataSetChanged();

If you only added a item, call

mAdapter.notifyItemInserted(position);

Upvotes: 1

Related Questions