【发布时间】:2021-04-04 08:33:33
【问题描述】:
我正在尝试创建一个嵌套的 RecyclerView 程序。因此,我为列表父项和列表子项编写了 2 RecyclerView 并从该程序中创建了 2 个适配器,子适配器旨在作为数据子项,而适配器父项用于从数据子项中检索数据项和列表,这意味着说明adapter parent有2个属性,即data parent和list child,在recycler parent中。列表说明如下:Nested RecyclerView
从图中的图中,Child类的数据是按照item的个数输入的,因为list的数据输入的布局是这样的:Input Data
从详细的 Activity(用于数据输入)中,我使用 putExtra 和 getStringExtra 函数将输入数据相互发送。
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 中。如果某个类中存在你认为看不懂的函数,可以查看我的github上的完整代码:my github project
我对上面的项目做了一点总结,我仍然对如何检索数据然后使用循环显示它感到困惑,我仍然停留在for 循环的状态。
【问题讨论】:
标签: java android arraylist android-recyclerview