Android : Getting User input and Display in RecyclerView

Viewed 949

How to get the user input and display into a Recycler View

In My Previous Activity i get user input of Item Name and Also user Select the Images in the Gridview and want to pass those Images and Item Name into a Listview

Passing the User Input from Activity 1

 done.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {


                String itemname = listname.getText().toString();
                if (!TextUtils.isEmpty(listname.getText().toString())) {

                    startActivity(new Intent(getContext(), CheckHome.class).putExtra("data", itemname).putExtra("image", imageRes));
                    dismiss();
                } else {
                    Toast.makeText(getContext(), "List Name not Empty ", Toast.LENGTH_SHORT).show();
                }

            }

        });

Want to Display the User Input and the selected images into a ListView in recycler View

public class CheckHome extends AppCompatActivity {

    ArrayList listimages;
    ArrayList listname;



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

        // get the reference of RecyclerView
        RecyclerView recyclerView = (RecyclerView) findViewById(R.id.recyclerView);
        // set a LinearLayoutManager with default vertical orientation
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getApplicationContext());
        recyclerView.setLayoutManager(linearLayoutManager);
        // call the constructor of CustomAdapter to send the reference and data to Adapter
        CustomAdapter customAdapter = new CustomAdapter(this, listname,listimages);
        recyclerView.setAdapter(customAdapter); // set the Adapter to RecyclerView
    }

    private class CustomAdapter extends RecyclerView.Adapter {


        ArrayList listimages;
        ArrayList listname;
        Context context;

        public CustomAdapter(Context context,ArrayList listimages, ArrayList listname ) {
            this.listimages = listimages;
            this.listname = listname;
            this.context = context;
        }

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

            // infalte the item Layout
            View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.rowlayout, parent, false);

            MyViewHolder vh = new MyViewHolder(v); // pass the view to View Holder
            return vh;

        }

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

                   //what to Display Here 
        }

        @Override
        public int getItemCount() {
            return 0;
        }
    }

    private class MyViewHolder extends RecyclerView.ViewHolder {
        // init the item view's
        TextView name;
        ImageView image;
        TextView listcount;

        public MyViewHolder(View itemView) {
            super(itemView);
            // get the reference of item view's
            name = (TextView) itemView.findViewById(R.id.list_name);
            image = (ImageView) itemView.findViewById(R.id.image_list);
            listcount=itemView.findViewById(R.id.list_count);


        }
    }
}
2 Answers

In your Activity OnCreate() method you can access the data passed through Intent like this:

Bundle bundle = getIntent().getExtras();
if (bundle != null) {
String itemName= bundle.getString("data");
}

Here I'm getting String but you can pass String array as well. And you can pass this value to your RecyclerAdapter class:

 CustomAdapter customAdapter = new CustomAdapter(this, listname,listimages);

But don't pass huge data through intent.

Also getItemCount() should not be 0. You have to pass some size to it. like, modelList.size()

Related