2015-04-02 51 views
0

我有ListView和3个字符串的自定义布局。 如何使用布局将项目添加到ListView如何使用自定义布局填充ListView,并使用字符串

R.id.Tag1 takes value of String1; 
R.id.Tag2 takes value of String2; 
R.id.Tag3 takes value of String3; 

而不是Item1 from ListView = the objects Tag1,Tag2,Tag3

我该怎么做?我知道如何处理Context,使用SimpleContextAdapter,但我不知道只有字符串。

+0

不知道你要问这里。您可以将您的代码发布到目前为止所取得的成就吗?您的自定义布局等 – 2015-04-02 06:50:48

+0

请尝试以下链接http://androidexample.com/How_To_Create_A_Custom_Listview_-_Android_Example/index.php?view=article_discription&aid=67&aaid=9 – 2015-04-02 06:51:56

+0

尝试任何基本的自定义列表视图示例,您将找到答案 例如:http ://www.androidhive.info/2014/07/android-custom-listview-with-image-and-text-using-volley/ – 2015-04-02 06:58:48

回答

0

让你定义适配器这样的,

这里是布局custom_items_list.You可以改变布局accordingly.I我只是张贴的布局,我现在有。

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
android:layout_width="match_parent" 
android:layout_height="wrap_content" 
android:background="#FFFFFF" 
android:orientation="vertical" > 

<RelativeLayout 
    android:layout_width="wrap_content" 
    android:layout_height="@dimen/headerHeight1" 
    android:gravity="center" 
    android:padding="5dp" > 

    <TextView 
     android:id="@+id/more_item" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_centerVertical="true" 
     android:layout_marginLeft="@dimen/viewSpace1" 
     android:textColor="#000000" 
     android:textSize="@dimen/titlebar_textSize" 
     tools:ignore="HardcodedText" /> 

    <TextView 
     android:textSize="@dimen/titlebar_textSize" 
     android:textColor="#000000" 
     android:id="@+id/itemPrice" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentRight="true" 
     android:layout_centerVertical="true" /> 

</RelativeLayout> 

<View 
    android:layout_height="0.5dip" 
    android:background="#000000" 
    android:layout_width="fill_parent" 
    android:layout_marginLeft="@dimen/viewSpace3" 
    android:layout_marginRight="@dimen/viewSpace3"/> 

</LinearLayout> 

在您的主要活动,

ArrayList<HashMap<String, String>> detailss = new ArrayList<HashMap<String, String>>(); 
ItemList=(ListView) findViewById(R.id.listViewItems); 

HashMap<String, String> map = new HashMap<String, String>(); 

    map.put("itemId", "1"); 
    map.put("itemName", "Name"); 
    map.put("itemPrice", "120"); 

    detailss.add(map); 

    adapter = new CustomItemsList(this, detailss); 
    ItemList.setAdapter(adapter); 
相关问题