2015-04-02 50 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); 
相關問題