gpt4 book ai didi

java - 使 listActivity 每行包含 2 个项目

转载 作者:行者123 更新时间:2023-12-02 06:51:59 25 4
gpt4 key购买 nike

我在将 listView 设置为与列表中的每个项目使用两个 textView 时遇到问题。这是我的代码。我知道我尝试实现两个不同数组的方式存在根本性错误,但我一直无法弄清楚这一点。不确定 hashMaps 是否是正确的选择。

private String[] nums= { "One", "Two", "Three" };
private String[] names= { "HoneyComb", "JellyBean", "ICS" };


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
int[] ids = {android.R.id.text1, android.R.id.text2};
SimpleAdapter<String> adapter = new SimpleAdapter(this, names,
android.R.layout.simple_list_item_2, nums, ids);
setListAdapter(adapter);

}

如果可能的话,我真的很想坚持使用 SimpleAdapter。

最佳答案

提一下这个

private String[] nums= { "One", "Two", "Three" };
private String[] names= { "HoneyComb", "JellyBean", "ICS" };


List<HashMap<String,String>> aList = new ArrayList<HashMap<String,String>>();

for(int i=0;i<nums.lenght();i++){
HashMap<String, String> hm = new HashMap<String,String>();
hm.put("txt1", nums[i]);
hm.put("txt2", names[i]);
aList.add(hm);
}

// Keys used in Hashmap
String[] from = { "txt1","txt2" };

// Ids of views in listview_layout
int[] to = {R.id.txt1,R.id.txt2};

// Instantiating an adapter to store each items
// R.layout.listview_layout defines the layout of each item
SimpleAdapter adapter = new SimpleAdapter(getActivity().getBaseContext(), aList, R.layout.listview_layout, from, to);

setListAdapter(adapter);

}

listview_layout.xml 是

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




<TextView
android:id="@+id/txt1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="15dp" />

<TextView
android:id="@+id/txt2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="10dp" />



</LinearLayout>

关于java - 使 listActivity 每行包含 2 个项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17917246/

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