我有一个列表,里面有三个项目。color_id、color_name和color_count
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView android:id="@+id/color_id"/>
<TextView android:id="@+id/color_name"/>
<TextView android:id="@+id/color_count"/>
</LinearLayout>我将数据从服务器加载到我的列表中。在这里,我将数据手动添加到列表中以进行演示:
ArrayList<HashMap<String, String>> colorsList = new ArrayList();
HashMap<String, String> map = new HashMap();
map.put("id", "1");
map.put("color_name", "red");
map.put("color_count", "10");
colorsList.add(map);
map = new HashMap();
map.put("id", "2");
map.put("color_name","yellow"):
map.put("color_count","15");
colorsList.add(map);
ListAdapter adapter = new SimpleAdapter(
MyActivity.this, colorsList,
R.layout.list_item_colors, new String[] {"id",
"color_name","color_count"}, new int[] {
R.id.color_id, R.id.color_name, R.id.color_count});
setListAdapter(adapter);问题
这一切对我来说都很好。然而,出于其他原因,我想与SimpleAdapter分开使用ArrayAdapter,然而,对于ArrayAdapter,我不知道如何设置我拥有的三项:color_id、color_name和color_count
发布于 2014-02-12 00:55:49
您需要创建自定义的ArrayAdapter和ovveride getView()方法,该方法每次在列表视图中创建项时调用,然后在其中膨胀布局(其中包含文本视图)并附加其数据。
关于ListView和自定义教程有很好的教程
http://www.vogella.com/tutorials/AndroidListView/article.html
在任何对你来说都不明显的事情上,你可以自由地回馈我。
发布于 2014-02-12 03:36:22
您可以使用SimpleAdapter.This是我的演示。MainActivity.java:
public class MainActivity extends Activity{
private ArrayList<HashMap<String, String>> colorsList;
private ListView list;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
list=(ListView)findViewById(R.id.listcolor);
colorsList = new ArrayList<HashMap<String, String>>();
HashMap<String, String> map = new HashMap<String, String>();
map.put("id", "1");
map.put("color_name", "red");
map.put("color_count", "10");
colorsList.add(map);
map = new HashMap<String, String>();
map.put("id", "2");
map.put("color_name","yellow");
map.put("color_count","15");
colorsList.add(map);
SimpleAdapter adapter=new SimpleAdapter(this,colorsList,R.layout.list_item_colors,
new String[] {"id","color_name","color_count"},
new int[] {R.id.color_id, R.id.color_name, R.id.color_count});
list.setAdapter(adapter);
}}
XMl: activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<ListView
android:id="@+id/listcolor"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
the list_item_colors.xml
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="@+id/color_id"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<TextView
android:id="@+id/color_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<TextView
android:id="@+id/color_count"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
https://stackoverflow.com/questions/21716396
复制相似问题