首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何摆脱SimpleAdapter

如何摆脱SimpleAdapter
EN

Stack Overflow用户
提问于 2014-02-12 00:46:09
回答 2查看 38关注 0票数 0

我有一个列表,里面有三个项目。color_idcolor_namecolor_count

代码语言:javascript
复制
<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>

我将数据从服务器加载到我的列表中。在这里,我将数据手动添加到列表中以进行演示:

代码语言:javascript
复制
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_idcolor_namecolor_count

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2014-02-12 00:55:49

您需要创建自定义的ArrayAdapterovveride getView()方法,该方法每次在列表视图中创建项时调用,然后在其中膨胀布局(其中包含文本视图)并附加其数据。

关于ListView和自定义教程有很好的教程

http://www.vogella.com/tutorials/AndroidListView/article.html

在任何对你来说都不明显的事情上,你可以自由地回馈我。

票数 0
EN

Stack Overflow用户

发布于 2014-02-12 03:36:22

您可以使用SimpleAdapter.This是我的演示。MainActivity.java:

代码语言:javascript
复制
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

代码语言:javascript
复制
<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

代码语言:javascript
复制
<?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"/>

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/21716396

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档