簡體   English   中英

RecyclerView 中不顯示數據

[英]Data doesn't show in RecyclerView

我正在嘗試創建一個嵌套的 RecyclerView 程序。 因此,我為列表父項和列表子項編寫了 2 個RecyclerView ,並從該程序中創建了 2 個適配器,子適配器旨在作為數據子項,而適配器父項用於從數據子項中檢索數據項和列表,這意味着adapter parent 有 2 個屬性,即 data parent 和 list child,以及在 recycler parent。 像這樣的列表插圖: Nested RecyclerView

從圖中的圖中,從Child class中的數據是按照item的數量輸入的,因為list的數據輸入的布局是這樣的: Input Data

從詳細的 Activity(用於數據輸入)中,我使用putExtragetStringExtra函數將輸入數據相互發送。

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 (編輯)

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;
        }

在這里,輸入的數據不會出現在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

我對上面的項目做了一點總結,我仍然對如何檢索數據然后使用循環顯示它感到困惑,我仍然卡在for循環的 state 中。

這在您執行此操作的兩種方法中都不起作用:

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;
}

請注意,您通過ArrayList()創建了一個ArrayList ,其初始容量為 10,但元素的大小/數量為 0,然后您嘗試遍歷空列表。 for 循環永遠不會執行,因為ArrayList#size()返回 0。

您可能希望更改您的 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;
}

請注意,我刪除了對ArrayList#size()的調用,而是使用了一個常量值,例如 10 或您想要的任何值。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM