Data doesn't show in RecyclerView

Viewed 59

I am trying to create a Nested RecyclerView program. So, I programmed 2 RecyclerView for list parent and list child and from this program, I create 2 adapters, the child adapter is intended as data sub-items and adapter parent to retrieve data items and lists from data sub-items, meaning that the adapter parent has 2 attributes, namely data parent and list child, and in the recycler parent. The illustration for list like this : Nested RecyclerView

From the figure in the illustration, the data from the Child class is inputted according to the number of items, because the layout of the data input for the list is like this : Input Data

And from the detailed Activity (for data input) I use the putExtra and getStringExtra functions to send the input data to each other.

DetailActivity.java

public class DetailActivity extends AppCompatActivity implements View.OnClickListener {

    LinearLayout layoutRow;
    List<Childs> titleList = new ArrayList<>();
    String txname, txsubname;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_detail);

        layoutRow = findViewById(R.id.layout_list);
        EditText title = findViewById(R.id.edt_name);
        txname = title.getText().toString();
        findViewById(R.id.btn_add_item).setOnClickListener(this);
        findViewById(R.id.btn_submit).setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.btn_add_item:
                addView();
                break;
            case R.id.btn_submit:
                if(checkValidate()){
                    Intent intent = new Intent(this, MainActivity.class);
                    intent.putExtra("name",txname);
                    intent.putExtra("subname",txsubname);
                    startActivity(intent);
                }
        }
    }

    private boolean checkValidate(){
        titleList.clear();
        boolean result = true;

        for (int i = 0;i < layoutRow.getChildCount();i++){

            View listview = layoutRow.getChildAt(i);
            EditText items = listview.findViewById(R.id.edt_item);
            txsubname = items.getText().toString();

            Childs childs = new Childs();

            if (!items.getText().toString().isEmpty()){
                childs.setTitle(items.getText().toString());
            } else {
                result = false;
                break;
            }

            titleList.add(childs);
        }

        if (titleList.size() == 0){
            result = false;
            Toast.makeText(this, "Data Kosong", Toast.LENGTH_SHORT).show();
        } else if(!result){
            Toast.makeText(this, "Semua Data Kosong!", Toast.LENGTH_SHORT).show();
        }

        return result;
    }

    private void addView(){
        final View titleView = getLayoutInflater().inflate(R.layout.row_add_item,null,false);

        EditText txsubname = titleView.findViewById(R.id.edt_item);
        ImageButton btnClose = titleView.findViewById(R.id.bt_close_item);

        btnClose.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                removeView(titleView);
            }
        });

        layoutRow.addView(titleView);
    }

    private void removeView(View titleView) {
        layoutRow.removeView(titleView);
    }
}

MainActivity.java(edit)

private List<Parents> ItemData() {
            List<Parents> listParent = new ArrayList<>();
            for(int i = 0; i < listParent.size();i++){
                Parents parentsItem = new Parents(titles+i,SubitemData());
                listParent.add(parentsItem);
            }

            return listParent;
        }

        private List<Childs> SubitemData() {
            List<Childs> listTitle = new ArrayList<>();
            for (int i = 0; i < listTitle.size();i++){
                Childs childs = new Childs(subtitles+i);
                listTitle.add(childs);
            }

            return listTitle;
        }

Here, the data entered does not appear in the RecyclerView. If there is a function in a class that you think can not be understood, you can check the complete code on my github:my github project

I conclude a little for the project above, I am still confused about how to retrieve data and then display it using looping, I am still stuck in state of the for loop.

1 Answers

This won't work as in both your methods you do this:

private List<Parents> ItemData() {
    List<Parents> listParent = new ArrayList<>();
    for(int i = 0; i < listParent.size();i++){
        ...
    }

    return listParent;
}

private List<Childs> SubitemData() {
    List<Childs> listTitle = new ArrayList<>();
    for (int i = 0; i < listTitle.size();i++){
        ...
    }

    return listTitle;
}

Notice you create an ArrayList via ArrayList() which has an initial capacity of 10 but a size/number of elements of 0, then you try to Iterate over the empty list. The for loop will never execute as ArrayList#size() returns 0.

You would want to change your for loops to instead do something like:

private List<Parents> ItemData() {
    List<Parents> listParent = new ArrayList<>();
    for(int i = 0; i < 10; i++){
        ...
    }

    return listParent;
}

private List<Childs> SubitemData() {
    List<Childs> listTitle = new ArrayList<>();
    for (int i = 0; i < 10; i++){
        ...
    }

    return listTitle;
}

Notice I removed the calls to ArrayList#size() and instead used a constant value like 10 or whatever you want.

Related