我将用这些组件创建一个动态CardView,如文本视图(项目名称(、微调器、活动选择和编辑文本。
我已经为数组中的每个列表设置/获取项目名称,但对于Spinner项目我遇到了错误。
请参阅以下内容以供参考。非常感谢。
1.FormActivity.java
private void buildRecyclerView() {
mRecyclerView = findViewById(R.id.rv_item); //RecyclerView
mRecyclerView.setHasFixedSize(true);
mLayoutManager = new LinearLayoutManager(this);
mAdapter = new ItemAdapter(mItemList, this);
mRecyclerView.setLayoutManager(mLayoutManager);
mRecyclerView.setAdapter(mAdapter);
}
mItemList我要通过的ItemAdapter.class
{
"item_id": 1,
"location_id": 1,
"item_name": "Credit Back Door",
"value": [
"A",
"B",
"C"
]
}
},
{
"item_id": 2,
"location_id": 1,
"item_name": "2F Admin Bridgeway",
"value": [
"D",
"E",
"F"
]
}
}
2.form_item.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.cardview.widget.CardView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/itemCard"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_rowWeight="1"
android:layout_columnWeight="1"
android:layout_gravity="fill"
android:layout_margin="8dp"
app:cardCornerRadius="20dp"
app:cardElevation="20dp">
<androidx.constraintlayout.widget.ConstraintLayout
android:id="@+id/relativeLayout2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="8dp"
android:padding="10dp"
tools:layout_editor_absoluteX="4dp"
tools:layout_editor_absoluteY="4dp">
<TextView
android:id="@+id/txt_item"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:text="1. Item"
android:textAllCaps="false"
android:textColor="#327034"
android:textStyle="bold"
app:layout_constraintTop_toTopOf="parent"
tools:layout_editor_absoluteX="10dp" />
<Spinner
android:id="@+id/dd_activity"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="34dp"
android:animateLayoutChanges="false"
app:layout_constraintTop_toTopOf="parent"
tools:layout_editor_absoluteX="9dp" />
<EditText
android:id="@+id/txt_remarks"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="68dp"
android:hint="Remarks"
app:layout_constraintTop_toTopOf="parent"
tools:layout_editor_absoluteX="9dp" />
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.cardview.widget.CardView>
2.2动态CardView UI请单击此处查看图片
3.ItemAdapter.java
public class ItemAdapter extends RecyclerView.Adapter<ItemAdapter.ItemViewHolder> {
public ArrayList<ItemParams> mItemList;
public Context Context;
public ItemAdapter(ArrayList<ItemParams> itemList, Context context) {
mItemList = itemList;
Context = context;
}
public static class ItemViewHolder extends RecyclerView.ViewHolder {
public TextView txt_item;
public Spinner dd_activity;
public EditText txt_remarks;
public CardView itemCard;
public ItemViewHolder(View itemView) {
super(itemView);
itemCard = itemView.findViewById(R.id.itemCard);
txt_item = itemView.findViewById(R.id.txt_item);
dd_activity = itemView.findViewById(R.id.dd_activity);
txt_remarks = itemView.findViewById(R.id.txt_remarks);
}
}
@NonNull
@Override
public ItemAdapter.ItemViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.form_item, parent, false);
ItemAdapter.ItemViewHolder cvh = new ItemAdapter.ItemViewHolder(v);
//SPINNER ITEM SAMPLE BUT IT WILL BE A DYNAMIC FROM ONE OF THE LIST
List<String> list = new ArrayList<String>();
list.add("list 1");
list.add("list 2");
list.add("list 3");
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(parent.getContext(),R.layout.form_item, list);
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
cvh.dd_activity.setAdapter(dataAdapter);
return cvh;
}
@Override
public void onBindViewHolder(@NonNull ItemAdapter.ItemViewHolder holder, int position) {
ItemParams mFormItem = mItemList.get(position);
holder.txt_item.setText(mFormItem.getItem_name());
}
@Override
public int getItemCount() {
return mItemList.size();
}
}
错误:
2021-07-01 13:28:34.645 7337-7337/com.unilab.healthcheck E/RecyclerView: No adapter attached; skipping layout
2021-07-01 13:28:35.050 7337-7337/com.unilab.healthcheck E/ArrayAdapter: You must supply a resource ID for a TextView
2021-07-01 13:28:35.053 7337-7337/com.unilab.healthcheck E/AndroidRuntime: FATAL EXCEPTION: main Process: com.unilab.healthcheck, PID: 7337 java.lang.IllegalStateException: ArrayAdapter requires the resource ID to be a TextView
由于您不必在适配器中初始化数组,因此您可以在活动或片段中列出您所拥有的列表,你可以尝试这个片段(参考来自你的代码(
->首先,您必须将此代码添加到活动或片段中
//SPINNER ITEM SAMPLE
List<String> list = new ArrayList<String>();
list.add("list 1");
list.add("list 2");
list.add("list 3");
recycleview.setAdapter(new ItemAdapter(getActivity(),list));
->然后
package com.brnd.pos.adapters;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import java.util.List;
public class ItemAdapter extends RecyclerView.Adapter<ItemAdapter.ItemViewHolder> {
Context context;
List<String> list;
public ItemAdapter(Context context, List<String> list) {
this.context = context;
this.list = list;
}
@NonNull
@Override
public ItemAdapter.ItemViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.form_item, parent, false);
return new ItemAdapter.ItemViewHolder(v);
}
@Override
public void onBindViewHolder(@NonNull ItemAdapter.ItemViewHolder holder, int position) {
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(parent.getContext(), R.layout.form_item, R.id.dd_activity, list);
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
cvh.dd_activity.setAdapter(dataAdapter);
ItemParams mFormItem = mItemList.get(position);
holder.txt_item.setText(mFormItem.getItem_name());
}
@Override
public int getItemCount() {
return list.size();
}
//below viewholder is just for your reference you have to find view id's
public static class ItemViewHolder extends RecyclerView.ViewHolder {
public ItemViewHolder(@NonNull View itemView) {
super(itemView);
}
}
}
FormActivity.java
private void buildRecyclerView() {
mRecyclerView = findViewById(R.id.rv_item); //RecyclerView
mRecyclerView.setHasFixedSize(true);
mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
mRecyclerView.setAdapter(new ItemAdapter(mItemList,getApplicationContext()));
}
ItemAdapter.java
public class ItemAdapter extends RecyclerView.Adapter<ItemAdapter.ItemViewHolder> {
public ArrayList<ItemParams> mItemList;
public Context context;
public ItemAdapter(ArrayList<ItemParams> itemList, Context context) {
this.mItemList = itemList;
this.context = context;
}
public static class ItemViewHolder extends RecyclerView.ViewHolder {
public TextView txt_item;
public Spinner dd_activity;
public EditText txt_remarks;
public CardView itemCard;
public ItemViewHolder(View itemView) {
super(itemView);
itemCard = itemView.findViewById(R.id.itemCard);
txt_item = itemView.findViewById(R.id.txt_item);
dd_activity = itemView.findViewById(R.id.dd_activity);
txt_remarks = itemView.findViewById(R.id.txt_remarks);
}
}
@NonNull
@Override
public ItemViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.form_item, parent, false);
return new ItemViewHolder(v);
}
@Override
public void onBindViewHolder(@NonNull ItemViewHolder holder, int position) {
//SPINNER ITEM SAMPLE BUT IT WILL BE A DYNAMIC FROM ONE OF THE LIST
List<String> list = new ArrayList<String>();
list.add("list 1");
list.add("list 2");
list.add("list 3");
holder.dd_activity.setAdapter(
new ArrayAdapter(context, android.R.layout.simple_spinner_dropdown_item, list));
holder.txt_item.setText(mItemList.get(position).getItem_name());
}
@Override
public int getItemCount() {
return mItemList.size();
}
}
但我相信错误在xml:中
android.R.layout.simple_spinner_dropdown_item
如果错误仍然出现,请尝试切换到类似以下的XML:
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/item_dropdown_textView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:padding="15dp"
android:text="@string/texte"
android:textAlignment="center"
android:textSize="18sp"></TextView>