gpt4 book ai didi

android - 使用 TreeMap 数据扩充 ListView(自定义适配器)

转载 作者:行者123 更新时间:2023-11-29 00:07:48 26 4
gpt4 key购买 nike

已解决:我根据@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;
}
}
}


ma​​in_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

最佳答案

做这样的事情:

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

关于android - 使用 TreeMap 数据扩充 ListView(自定义适配器),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32569709/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com