【问题标题】:2 lists in 1 listview android1 个列表视图中的 2 个列表
【发布时间】:2016-12-18 20:21:44
【问题描述】:

我正在尝试创建适配器以在一个列表视图中显示 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"/>

我发现编写自定义适配器很复杂,因此欢迎提供任何帮助。

【问题讨论】:

  • 为什么不简单地使用 2 个 ListViews 和一个 ImageView 之间?

标签: android listview arraylist android-arrayadapter


【解决方案1】:

您必须编写自己的适配器,只需从 ArrayAdapter 类扩展并填写覆盖的方法。

这只是一个简单的例子(不完整)

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】:

    虽然我不太清楚您要做什么,但我的理解是: 您有两个列表,并且您希望使用两个列表中的相应数据显示在 ListView 的单个项目中。

    如果这确实是您想要做的,则创建第三个数据模型,表示要在 ListView 的每个项目中显示的数据模型,并使用此模型的 ArrayList 填充 ListView 的项目.

    希望这会有所帮助。祝你好运:)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-07-30
      • 1970-01-01
      • 1970-01-01
      • 2015-05-11
      相关资源
      最近更新 更多