Раздуйте ListView данными TreeMap (пользовательский адаптер)

решаемая: Я создал адаптер на основе предложения @JJV. Я знаю, что есть много возможностей для улучшения, но пока это работает. Я обновил эту упрощенную версию моей программы рабочим кодом; Надеюсь это будет полезно другим



MainActivity.java:

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

import java.util.Map;
import java.util.TreeMap;

public class MainActivity extends AppCompatActivity {

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

        Map<Integer, Object> m = new TreeMap<Integer, Object>();

        int key = 123;
        Item obj1 = new Item("abc", "xyz", 888);
        m.put(key, obj1);

        key = 456;
        Item obj2 = new Item("def", "zyx", 999);
        m.put(key, obj2);

        ListAdapter adapter = new TreeMapAdapter(this, (TreeMap<Integer, Object>) m);
        ListView itemListView = (ListView) findViewById(R.id.itemListView);
        itemListView.setAdapter(adapter);
    }

    public class Item {
        private String name;
        private String thing;
        private int number;

        Item(String name, String thing, int number) {
            this.name = name;
            this.thing = thing;
            this.number = number;
        }

        public String getName() {
            return this.name;
        }

        public String getThing() {
            return this.thing;
        }

        public int getNumber() {
            return this.number;
        }
    }
}


main_activity.xml:

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

    <ListView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/itemListView" />
</LinearLayout>


listview_row.xml:

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

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:id="@+id/name" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:id="@+id/thing" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:id="@+id/number" />
</LinearLayout>


TreeMapAdapter.java:

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.TreeMap;


public class TreeMapAdapter extends ArrayAdapter<String> {

    private Context context;
    private TreeMap<Integer, Object> treeMap;
    private Integer[] mapKeys;

    public TreeMapAdapter(Context context, TreeMap<Integer, Object> data) {
        super(context, R.layout.listview_row);

        this.context = context;
        this.treeMap = data;
        mapKeys = treeMap.keySet().toArray(new Integer[getCount()]);
    }

    public int getCount() {
        return treeMap.size();
    }

    public String getItem(int position) {
        return String.valueOf(treeMap.get(mapKeys[position]));
    }

    public long getItemId(int position) {
        return mapKeys[position];
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        LayoutInflater deviceInflater = LayoutInflater.from(getContext());
        View listViewRow = deviceInflater.inflate(R.layout.listview_row, parent, false);

        MainActivity.Item test = (MainActivity.Item) treeMap.get(mapKeys[position]);

        String nameString = test.getName() + ", ";
        String thingString = test.getThing() + ", ";
        String numberInt = String.valueOf(test.getNumber());

        TextView name = (TextView) listViewRow.findViewById(R.id.name);
        TextView thing = (TextView) listViewRow.findViewById(R.id.thing);
        TextView number = (TextView) listViewRow.findViewById(R.id.number);

        name.setText(nameString);
        thing.setText(thingString);
        number.setText(numberInt);

        return listViewRow;
    }

}


Результат: Я не могу встроить скриншот результата в этот пост, потому что у меня нет 10 репутации, но вы можете увидеть результат запуска кода здесь:http://i.stack.imgur.com/QoXX1.png

Ответы на вопрос(1)

Ваш ответ на вопрос