HY, 我想在ListView的一个项目中添加一个图标和2行。首先是值,第二个是值2
String[] values = new String[] { "Android", "iPhone", "WindowsMobile",
"Blackberry", "WebOS", "Ubuntu", "Windows7", "Max OS X",
"Linux", "OS/2" };
String[] values2 = new String[] `enter code here`{ "Android2", "iPhone2", "WindowsMobile2",
"Blackberry2", "WebOS2", "Ubuntu2", "Windows72", "Max OS X2",
"Linux2", "OS/22" };
我的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" >
<ListView
android:id="@+id/list"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</ListView>
</LinearLayout>
每行的xml是:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<ImageView
android:id="@+id/icon"
android:layout_width="22px"
android:layout_height="22px"
android:layout_marginLeft="4px"
android:layout_marginRight="10px"
android:layout_marginTop="4px"
android:src="@drawable/ic_launcher" >
</ImageView>
<TextView
android:id="@+id/label"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@+id/label"
android:textSize="20px" />
<TextView
android:id="@+id/label2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@+id/label" />
</LinearLayout>
我的代码是:
public class listaNoua extends ListActivity {
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
String[] values = new String[] { "Android", "iPhone", "WindowsMobile",
"Blackberry", "WebOS", "Ubuntu", "Windows7", "Max OS X",
"Linux", "OS/2" };
String[] values2 = new String[] { "Android2", "iPhone2", "WindowsMobile2",
"Blackberry2", "WebOS2", "Ubuntu2", "Windows72", "Max OS X2",
"Linux2", "OS/22" };
ArrayAdapter<String> adapter = new ArrayAdapter<String> (this,R.layout.rowlayout, R.id.label2, values2);
setListAdapter(adapter);
}
如果我添加:
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,R.layout.rowlayout, R.id.label2, values2 , R.id.label, values);
代码不起作用
任何人都可以帮助我吗?
答案 0 :(得分:5)
您应该使用列表自定义适配器。你可以在这里找到一些好的例子
http://www.learn-android.com/2011/11/22/lots-of-lists-custom-adapter/
我认为你在行XML上忘记了LinearLayout的方向。问题是默认情况下线性布局具有“水平”方向。该行应如下所示,以显示列表中的所有3个项目。
<ImageView
android:id="@+id/icon"
android:layout_width="22dp"
android:layout_height="22dp"
android:layout_marginLeft="4dp"
android:layout_marginRight="10dp"
android:layout_marginTop="4dp"
android:src="@drawable/ic_launcher" >
</ImageView>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TextView
android:id="@+id/label"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@+id/label"
android:textSize="20sp" />
<TextView
android:id="@+id/label2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@+id/label" />
</LinearLayout>
答案 1 :(得分:5)
您可以使用SimpleAdapter。
List<Map<String, ?>> data = new ArrayList<Map<String, ?>>();
data.add(createRow("Android", "Android2"));
data.add(createRow("iPhone", "iPhone"));
String[] from = {"value1", "value2"};
int[] to = {R.id.label, R.id.label2};
SimpleAdapter adapter = new SimpleAdapter(this, data, R.layout.item_layout, from, to);
和创建行的功能
private Map<String, ?> createRow(String value1, String value2) {
Map<String, String> row = new HashMap<String, String>();
row.put("value1", value1);
row.put("value2", value2);
return row;
}
答案 2 :(得分:2)
Android已为此名为simple_list_item_2
的预定义XML布局附带,因此您无需编写自己的适配器或编写自己的布局文件。
您只需覆盖getView
并为已存在的android.R.id.text1
布局中已预定义的android.R.id.text2
和simple_list_item_2
TextView设置值。
ArrayAdapter<MyObject> adapter = new ArrayAdapter<MyObject>(this, android.R.layout.simple_list_item_2, android.R.id.text1, result) {
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = super.getView(position, convertView, parent);
TextView text1 = (TextView) view.findViewById(android.R.id.text1);
TextView text2 = (TextView) view.findViewById(android.R.id.text2);
text1.setText(result.get(position).OperatingSystem);
text2.setText(result.get(position).Platform);
return view;
}
};