简体   繁体   English

RecyclerView 中不显示数据

[英]Data doesn't show in RecyclerView

I am trying to create a Nested RecyclerView program.我正在尝试创建一个嵌套的 RecyclerView 程序。 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.因此,我为列表父项和列表子项编写了 2 个RecyclerView ,并从该程序中创建了 2 个适配器,子适配器旨在作为数据子项,而适配器父项用于从数据子项中检索数据项和列表,这意味着adapter parent 有 2 个属性,即 data parent 和 list child,以及在 recycler parent。 The illustration for list like this: Nested RecyclerView像这样的列表插图: 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从图中的图中,从Child class中的数据是按照item的数量输入的,因为list的数据输入的布局是这样的: 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.从详细的 Activity(用于数据输入)中,我使用putExtragetStringExtra函数将输入数据相互发送。

DetailActivity.java 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) MainActivity.java (编辑)

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 .在这里,输入的数据不会出现在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 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.我对上面的项目做了一点总结,我仍然对如何检索数据然后使用循环显示它感到困惑,我仍然卡在for循环的 state 中。

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.请注意,您通过ArrayList()创建了一个ArrayList ,其初始容量为 10,但元素的大小/数量为 0,然后您尝试遍历空列表。 The for loop will never execute as ArrayList#size() returns 0. for 循环永远不会执行,因为ArrayList#size()返回 0。

You would want to change your for loops to instead do something like:您可能希望更改您的 for 循环,以改为执行以下操作:

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.请注意,我删除了对ArrayList#size()的调用,而是使用了一个常量值,例如 10 或您想要的任何值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM