Заполнение MultiColumn ListView двумя строковыми массивами - PullRequest
0 голосов
/ 12 декабря 2018

Я пытаюсь написать код, который отображает список продуктов в первом столбце 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>

1 Ответ

0 голосов
/ 12 декабря 2018

Для этого вам нужно создать отдельный настраиваемый адаптер списка.

Создайте новый класс, подобный этому:

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;
    }
}

И в своей основной деятельности:

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);
    }
}

SMARTER WAY

Вместо того, чтобы использовать 2 строковых массива на столбец, вы можете создать объект Product

Product.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, вместо того, чтобы передавать два отдельных массива.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...