Reputation: 731
Hy, I want to add an icon and 2 rows in one item of ListView. First will be values and second will be values2
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" };
My xml for list is :
<?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>
My xml for each row is :
<?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>
And my code is :
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);
}
If i add :
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,R.layout.rowlayout, R.id.label2, values2 , R.id.label, values);
The code does not work
Can anybody help me ?
Upvotes: 3
Views: 15621
Reputation: 12846
Android already comes with a predefined XML layout for this called simple_list_item_2
so you don't have to write your own Adapter or write your own layout file.
You just need to override getView
and set values for the already pre-defined android.R.id.text1
and android.R.id.text2
TextViews in the already-existing simple_list_item_2
layout.
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;
}
};
Upvotes: 2
Reputation: 4081
You can use SimpleAdapter for this.
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);
and the function for creating a row
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;
}
Upvotes: 5
Reputation: 18746
You should use list custom adapter. You can find good some example here
http://www.learn-android.com/2011/11/22/lots-of-lists-custom-adapter/
I think you forget the orientation for LinearLayout at your the row XML. The issue is that linear layout by default has "horizontal" orientation. The row should be as below to show all 3 items in the list.
<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>
Upvotes: 6