1 个列表视图中的 2 个列表 android
2 lists in 1 listview android
我正在尝试创建用于在一个列表视图中显示 2 个列表的适配器。列表应按图像划分。这是XML的想法。
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal" >
<TextView
android:id="@+id/Itemname"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20sp"
android:layout_gravity="left"
android:paddingTop="5dp"/>
<ImageView
android:id="@+id/icon"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_marginBottom="5dp"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:layout_marginTop="5dp"
android:layout_gravity="center"
android:src="@mipmap/ic_launcher" />
<TextView
android:id="@+id/Itemname2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20sp"
android:layout_gravity="right"
android:paddingTop="5dp"/>
我发现编写自定义适配器很复杂,因此欢迎任何帮助。
虽然我不太清楚你想做什么,但这是我的理解:
您有两个列表,您希望使用两个列表中的相应数据显示在 ListView 的单个项目中。
如果这确实是您想要做的,那么创建第三个数据模型,表示要在 ListView 的每个项目中显示的数据模型,并使用该模型的 ArrayList 来填充项目列表视图。
希望这对您有所帮助。祝你好运:)
您将不得不编写自己的适配器,只需从 ArrayAdapter 扩展 class 并填写覆盖的方法。
这只是一个简单的例子(不完整)
public class MyAdapter extends ArrayAdapter {
private List list_one;
private List list_two;
public MyAdapter(Context context, int resource, int textViewResourceId, List objects1, List objects2) {
super(context, resource, textViewResourceId, objects1);
this.list_one = objects1;
this.list_two = objects2;
}
@Override
public int getCount() {
//here you return the count of items your list will display
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = inflater.inflate(R.layout.xml_file_name, parent);
TextView item1 = (TextView) v.findViewById(R.id.Itemname);
TextView item2 = (TextView) v.findViewById(R.id.Itemname2);
ImageView imageView = (ImageView) v.findViewById(R.id.icon);
item1.setText(list_one.get(position));
item2.setText(list_two.get(position));
imageView.setImageResource(/*not really sure where you will be getting your image from*/);
return v;
}
}
我正在尝试创建用于在一个列表视图中显示 2 个列表的适配器。列表应按图像划分。这是XML的想法。
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal" >
<TextView
android:id="@+id/Itemname"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20sp"
android:layout_gravity="left"
android:paddingTop="5dp"/>
<ImageView
android:id="@+id/icon"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_marginBottom="5dp"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:layout_marginTop="5dp"
android:layout_gravity="center"
android:src="@mipmap/ic_launcher" />
<TextView
android:id="@+id/Itemname2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20sp"
android:layout_gravity="right"
android:paddingTop="5dp"/>
我发现编写自定义适配器很复杂,因此欢迎任何帮助。
虽然我不太清楚你想做什么,但这是我的理解: 您有两个列表,您希望使用两个列表中的相应数据显示在 ListView 的单个项目中。
如果这确实是您想要做的,那么创建第三个数据模型,表示要在 ListView 的每个项目中显示的数据模型,并使用该模型的 ArrayList 来填充项目列表视图。
希望这对您有所帮助。祝你好运:)
您将不得不编写自己的适配器,只需从 ArrayAdapter 扩展 class 并填写覆盖的方法。
这只是一个简单的例子(不完整)
public class MyAdapter extends ArrayAdapter {
private List list_one;
private List list_two;
public MyAdapter(Context context, int resource, int textViewResourceId, List objects1, List objects2) {
super(context, resource, textViewResourceId, objects1);
this.list_one = objects1;
this.list_two = objects2;
}
@Override
public int getCount() {
//here you return the count of items your list will display
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = inflater.inflate(R.layout.xml_file_name, parent);
TextView item1 = (TextView) v.findViewById(R.id.Itemname);
TextView item2 = (TextView) v.findViewById(R.id.Itemname2);
ImageView imageView = (ImageView) v.findViewById(R.id.icon);
item1.setText(list_one.get(position));
item2.setText(list_two.get(position));
imageView.setImageResource(/*not really sure where you will be getting your image from*/);
return v;
}
}