2014-02-12 251 views
0

我有一个列表视图中有三个项目。 color_idcolor_namecolor_count如何摆脱SimpleAdapter

<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_idcolor_namecolor_count

+0

你想用自定义ListView和ArrayAdapter来保存textviews? –

+0

是的。我有ListView中有多个textviews,我不想使用SimpleAdapter。 – birdy

回答

0

您需要创建您的自定义ArrayAdapterovveridegetView()方法,这种方法,每次在列表视图中创建项目,并在其中,你会虚增您的布局(包含textviews),并附上其数据

有好的教程大约ListView和自定义一个叫

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

,并随时喂我回来在什么事上并不明显你

0

您可以使用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"/> 

的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"/>