首页 > 解决方案 > 用两个字符串数组填充 MultiColumn ListView

问题描述

我正在尝试编写一个代码,它在 ListBox 的第一列中显示产品列表,在第二列中显示它们的价格。TextView 上的每一个都由自定义行布局定义。

第一个字符串/列已正确填充,但是我还没有找到任何方法来填充第二列中的第二个字符串数组。

如何修改适配器以处理两组字符串?

这是我到目前为止所拥有的:

MainActivity.java

package com.example.serveira.productlist;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListAdapter;
import android.widget.ListView;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        String[] products = {"Product_1", "Product_2", "Product_3"};
        String[] prices = {"$ 3,00", "$ 5,00", "$ 3,50"};

        ListAdapter theAdapter = new ArrayAdapter<String>(this, R.layout.row_layout_2,
                R.id.textView1, products);    

        ListView product_list = (ListView) findViewById(R.id.product_list);

        product_list.setAdapter(theAdapter);

    }
}

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical"
    tools:context=".MainActivity">

    <ListView
        android:padding="2dp"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/product_list"></ListView>

</LinearLayout>

row_layout_2.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal"
    tools:context=".MainActivity">

    <TextView
        android:layout_weight="1"
        android:minWidth="50dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/textView1"
        android:textSize="17sp"
        android:padding="15dp"
        android:textStyle="italic"/>

    <TextView
        android:layout_weight="1"
        android:minWidth="50dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/textView2"
        android:textSize="17sp"
        android:padding="15dp"
        android:textStyle="italic"/>

 </LinearLayout>

标签: androidandroid-studio

解决方案


您需要为此创建一个单独的自定义列表适配器。

像这样创建一个新类:

public class CustomAdapter extends ArrayAdapter {

    private Context context;
    private String[] products;
    private String[] prices;

    public CustomAdapter(Context context, int resource, String[] products, String[] prices) {
        super(context, resource);
        this.context = context;
        this.products = products;
        this.prices = prices;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View view = LayoutInflater.from(context).inflate(R.layout.row_layout_2, parent, false);
        TextView column1 = view.findViewById(R.id.textView1);
        TextView column2 = view.findViewById(R.id.textView2);
        column1.setText(products[position]);
        column2.setText(prices[position]);
        return view;
    }

    @Override
    public int getCount() {
        return products.length;
    }
}

并且,在您的 MainActivity 中:

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        String[] products = {"Product_1", "Product_2", "Product_3"};
        String[] prices = {"$ 3,00", "$ 5,00", "$ 3,50"};
        CustomAdapter adapter = new CustomAdapter(this, -1, products, prices);
        ListView product_list = (ListView) findViewById(R.id.product_list);

        product_list.setAdapter(adapter);
    }
}

更聪明的方式

您可以创建一个对象 Product 而不是每列使用 2 个字符串数组

产品.java

public class Product {

    private String productName;
    private String price;

    public Product(String productName, String price) {
        this.productName = productName;
        this.price = price;
    }

// getter and setter stuff...
}

你可以创建一个ArrayList<Product>并将这个列表传递给你的CustomAdapter,而不是传递两个单独的数组。


推荐阅读