Search code examples
androidlistviewandroid-cardviewcardview

Inflate list view in a cardview recyclerview


I have a recyclerview contains a list of cardview ( 1 textview, 1 button to add and list which has a visibilty gone) so once the user press on the add button it takes him to another activity to do some job and once he save he should add an item inside the selected card listview and am stuck i have no idea how to do it in a onactivityresult to inflate an item of a specific card.


Solution

  • You can use Bundle to send arguments to the activity and back. For example, When the user clicks on an add button in the item in the RecyclerView. your adapter knows the position. As a matter of fact, your onClick must be implemented in your adapter. Send that position to the activity as putExtra before you startActvity. And then before finish you can again do putExtra.

    //Your onClick handler in the Adapter class
    Intent intent = new Intent(this, Activity.class);
    intent.putExtra("position", position); //position in adapter
    startActivityForResult(intent, REQ_CODE);
    
    
    
    //in the next activity before finish()
    Intent intent = getIntent();
    itnent.putExtra("position", position);
    finish();
    
    //in your previous activity 
    @Override
        public void onActivityResult(int requestCode, int resultCode, Intent data) {
    
        String pos = data.getStringExtra("position");
        adapter.getItem(Integer.parseInt(pos));
    }