<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ImageView
android:id="@+id/image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:maxHeight="5dp"/>
<TextView
android:id="@+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@id/image"/>
<TextView
android:id="@+id/price"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/text"
android:layout_marginLeft="20dp"
android:text="¥"/>
</RelativeLayout>
package com.example.simpleadapater;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import android.app.Activity;
import android.app.ActionBar;
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.os.Build;
public class MainActivity extends Activity {
String[]data={"apple","banana","grape","peach","pear","strawberry","mango","watermelon","orange"};
private List<Map<String, Object>> alList;
private ListView listView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
listView=(ListView) findViewById(R.id.listView);
alList=new ArrayList<Map<String,Object>>();
SimpleAdapter adapter=new SimpleAdapter(this, GetData(), R.layout.item, new String[]{"pic","te","pri"}, new int[]{R.id.image,R.id.text,R.id.price});
listView.setAdapter(adapter);
}
public List<Map<String, Object>> GetData()
{
Map<String, Object>map0=new HashMap<String, Object>();
map0.put("pic", R.drawable.ic_apple);
map0.put("te", data[0]);
map0.put("pri", "¥2.5");
alList.add(map0);
Map<String, Object>map1=new HashMap<String, Object>();
map1.put("pic", R.drawable.ic_banana);
map1.put("te", data[1]);
map1.put("pri", "¥3.5");
alList.add(map1);
Map<String, Object>map2=new HashMap<String, Object>();
map2.put("pic", R.drawable.ic_grape);
map2.put("te", data[2]);
map2.put("pri", "¥5.5");
alList.add(map2);
Map<String, Object>map3=new HashMap<String, Object>();
map3.put("pic", R.drawable.ic_peach);
map3.put("te", data[3]);
map3.put("pri", "¥22");
alList.add(map3);
Map<String, Object>map4=new HashMap<String, Object>();
map4.put("pic", R.drawable.ic_pear);
map4.put("te", data[4]);
map4.put("pri", "¥4.5");
alList.add(map4);
Map<String, Object>map5=new HashMap<String, Object>();
map5.put("pic", R.drawable.ic_strawberry);
map5.put("te", data[5]);
map5.put("pri", "¥6.5");
alList.add(map5);
Map<String, Object>map6=new HashMap<String, Object>();
map6.put("pic", R.drawable.ic_mango);
map6.put("te", data[6]);
map6.put("pri", "¥7.5");
alList.add(map6);
Map<String, Object>map7=new HashMap<String, Object>();
map7.put("pic", R.drawable.ic_watermelon);
map7.put("te", data[7]);
map7.put("pri", "¥9.5");
alList.add(map7);
Map<String, Object>map8=new HashMap<String, Object>();
map8.put("pic", R.drawable.ic_orange);
map8.put("te", data[8]);
map8.put("pri", "¥4.5");
alList.add(map8);
return alList;
}
}
本文介绍了一种在Android应用中使用SimpleAdapter来显示带有图片、文本及价格的列表项的方法。通过实例展示了如何创建和配置SimpleAdapter,并为每个列表项提供自定义布局。
1518

被折叠的 条评论
为什么被折叠?



