我试图创建一个自定义适配器来在列表视图中显示来自ArrayList的信息,但它似乎拒绝了一个ArrayList,而只是要求一个数组。这是我错误的代码:
的ArrayList
public ArrayList<Ing> IngList = new ArrayList<>();
public ArrayList<Ing> get(){
return this.IngList;
}
对象类
public class Ing {
int init;
double value;
String name;
String unit;
String quantity;
public String getName() { return name; }
public void setName(String name) { this.name = name;}
public double getValue(){ return value; }
public void setValue(double value){ this.value = value; }
public int getInit(){ return init; }
public void setInit(int init){ this.init = init; }
public String getUnit(){ return unit; }
public void setUnit(String unit){ this.unit = unit; }
public String getQuantity(){ return quantity; }
public void setQuantity(String quantity){ this.quantity = quantity; }
Ing(String name, double value, int init, String unit, String quantity) {
this.name = name;
this.value = value;
this.init = init;
this.unit = unit;
this.quantity = quantity;
}
}
ListAdapter
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;
import java.util.ArrayList;
public class ListAdapter extends ArrayAdapter<Ing> {
private Context context;
private Ing[] values = null;
public static Kitchen kitchen = new Kitchen();
public ListAdapter(Context context, int textViewResourceId, Ing[] values) {
super(context, textViewResourceId, values);
this.context = context;
this.values = values;
}
public int getCount(){
return values.length;
}
public Ing getItem(int position){
return values[position];
}
public long getItemId(int position){
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = LayoutInflater.from(getContext()).inflate(R.layout.row_layout, parent, false);
}
TextView name = (TextView) convertView.findViewById(R.id.name);
TextView value = (TextView) convertView.findViewById(R.id.value);
TextView unit = (TextView) convertView.findViewById(R.id.unit);
name.setText(kitchen.IngList.get(position).getName());
value.setText(kitchen.IngList.get(position).getQuantity());
unit.setText(kitchen.IngList.get(position).getUnit());
return convertView;
}
}
分配适配器
final ListAdapter adapter = new ListAdapter(getContext(),
R.layout.row_layout, IngList);
//error is in line above
final ListView listView = (ListView) root.findViewById(R.id.list);
listView.setAdapter(adapter);
行布局
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="10dp"
android:paddingTop="10dp"
android:paddingRight="10dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/name"
android:text="Name"
android:textAppearance="?android:attr/textAppearanceLarge"
android:layout_alignParentTop="true"
android:ellipsize="end"
android:maxLines="1"
android:layout_marginBottom="2dp"
android:layout_marginLeft="10dp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/value"
android:textAppearance="?android:attr/textAppearanceLarge"
android:layout_alignParentTop="true"
android:maxLines="1"
android:layout_toEndOf="@+id/name"
android:layout_toRightOf="@+id/name"
android:text="Value"
android:layout_marginLeft="50dp"
android:layout_marginBottom="2dp"
android:ellipsize="end"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/unit"
android:textAppearance="?android:attr/textAppearanceLarge"
android:layout_alignParentTop="true"
android:maxLines="1"
android:layout_toEndOf="@+id/value"
android:layout_toRightOf="@+id/value"
android:text="Unit"
android:layout_marginLeft="5dp"
android:layout_marginBottom="2dp"
android:ellipsize="end"/>
很抱歉,如果我发布了超过我需要的内容。我还提前道歉,因为在组织代码方面有点愚蠢。万分感谢能找到错误的人。
答案 0 :(得分:0)
您的修改后的ListAdapter
public class ListAdapter extends ArrayAdapter<Ing> {
private Context context;
private ArrayList<Ing> values = new ArrayList<>();
public static Kitchen kitchen = new Kitchen();
public ListAdapter(Context context, int textViewResourceId, ArrayList<Ing> values) {
super(context, textViewResourceId, values);
this.context = context;
this.values = values;
}
public int getCount(){
return values.size();
}
public Ing getItem(int position){
return values.get(position);
}
public long getItemId(int position){
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = LayoutInflater.from(getContext()).inflate(R.layout.row_layout, parent, false);
}
TextView name = (TextView) convertView.findViewById(R.id.name);
TextView value = (TextView) convertView.findViewById(R.id.value);
TextView unit = (TextView) convertView.findViewById(R.id.unit);
name.setText(kitchen.IngList.get(position).getName());
value.setText(kitchen.IngList.get(position).getQuantity());
unit.setText(kitchen.IngList.get(position).getUnit());
return convertView;
}
}
我也不明白为什么你使用kitchen.IngList
获取ArrayList来填充你的列表。您可以使用values
代替kitchen.IngList
答案 1 :(得分:0)
就像@ mark-keen和@ mike-m所说的那样,你的适配器构造函数期待Array
,但你给它ArrayList
。
使用以下命令创建适配器:
public ArrayList<Ing> IngList = new ArrayList<>();
...
final ListAdapter adapter = new ListAdapter(getContext(), R.layout.row_layout, IngList);
但是你的Adapter构造函数只接受Array:
public class ListAdapter extends ArrayAdapter<Ing> {
...
private Ing[] values = null;
public ListAdapter(Context context, int textViewResourceId, Ing[] values) {
...
}
}
因此,您需要更改构造函数以接受ArrayList作为其参数。像这样:
public class ListAdapter extends ArrayAdapter<Ing> {
private Context context;
// don't need to create object for values
// because it depends on the parameter value.
private ArrayList<Ing> values;
...
public ListAdapter(Context context, int textViewResourceId, ArrayList<Ing> values) {
super(context, textViewResourceId, values);
this.context = context;
this.values = values;
}
...
}