使用TreeMap数据扩展ListView(自定义适配器)

时间:2015-09-14 16:38:29

标签: android listview adapter 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 个答案:

答案 0 :(得分:2)

做这样的事情:

public class TreeMapAdapter extends ArrayAdapter<String> {
    private Context context
    private TreeMap<Integer, Object> treeMap;
    private int mapKeys[];
    public TreeMapAdapter(Context context,TreeMap<Integer, Object> treeMap)
        this.context=context;
        this.treeMao=treeMap;
        mapKeys=treemap.keySet().toArray();
    }

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

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

    public long getItemId(int position) {
        return mapKeys[position];
    }
    //your getView method....
}