Added images in the first item, when created another item, then showing the images of previous item instead of showing empty child recycler view


Inside a parent linear recycler view, I’ve child grid recycler view.

When I click on the image icon, it’ll go to gallery and I can select multiple images and show in the child grid recycler view.

I’ve created an item and added images from the gallery, When I am adding another item then the images are automatically added in that item from the previous item.

How to solve it?

The complete code is provided in the below link


You are creating only one list childModelClassList containing Uri Images. Which is read by every parentModelClassArrayList.

When you are adding item to childModelClassList this list gets updated and every parentModelClassArrayList is referenced to this list so the same item is shown on every grid.

The simplest way to fix it is having own childModelClassList for every parentModelClassArrayList

Something like so:

public class MainActivity extends AppCompatActivity implements OnItemClickListener{

    RecyclerView recyclerView;
    ArrayList<ParentModelClass> parentModelClassArrayList;
    ArrayList<ArrayList<Uri>> childModelClassList;
    ParentAdapter parentAdapter;
    ChildAdapter childAdapter;
    FloatingActionButton btnOpenDialog;

    private static final int Read_Permission = 101;
    private static int lastClickedPosition = 0;

    protected void onCreate(Bundle savedInstanceState) {


        recyclerView = findViewById(;

        parentModelClassArrayList = new ArrayList<>();
        childModelClassList = new ArrayList<>();

        btnOpenDialog = findViewById(;

        btnOpenDialog.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
                Dialog dialog = new Dialog(MainActivity.this);

                TextView edtTitle = dialog.findViewById(;
                Button okBtn = dialog.findViewById(;
                Button cancelBtn = dialog.findViewById(;

                // ok button
                okBtn.setOnClickListener(new View.OnClickListener() {
                    public void onClick(View view) {

                        String title = "";

                            title = edtTitle.getText().toString();

                            Toast.makeText(MainActivity.this, "Please Enter the title", Toast.LENGTH_SHORT).show();

                        ArrayList<Uri> uriListForParentModel = new ArrayList<>();

                        parentModelClassArrayList.add(new ParentModelClass(title,uriListForParentModel));
                // cancel button
                cancelBtn.setOnClickListener(new View.OnClickListener() {
                    public void onClick(View view) {



        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        parentAdapter = new ParentAdapter(this, parentModelClassArrayList, this::onItemClick);

        if(ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.READ_EXTERNAL_STORAGE)!= PackageManager.PERMISSION_GRANTED){
            ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.READ_EXTERNAL_STORAGE}, Read_Permission);

    public void onItemClick(int position) {
        //Toast.makeText(this, "Button " + position + " clicked", Toast.LENGTH_SHORT).show();
            Intent intent = new Intent();
            lastClickedPosition = position;
            if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2){
                intent.putExtra(Intent.EXTRA_ALLOW_MULTIPLE, true);
            startActivityForResult(Intent.createChooser(intent, "Select Picture"), 1);


    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        if(requestCode == 1 && resultCode == Activity.RESULT_OK){
            if(data.getClipData() != null){
                int x = data.getClipData().getItemCount();

                for(int i=0;i<x;i++){
            else if(data.getData() != null){
                String imageURL = data.getData().toString();


Answered By – Sabriael

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published