2013-04-29 36 views
0

我是Android新手。请帮帮我。我有两个字符串数组如下:Android - 字符串数组到ListView

String[] Array1 = {"ele1", "ele2", "ele3", "ele4", "ele5", "ele6"}; 
String[] Array2 = {"obj1", "obj2", "obj3", "obj4", "obj5", "obj6"}; 

我在我的布局如下列表视图:

<ListView 
android:layout_width="fill_parent" 
android:layout_height="wrap_content" 
android:id="@+id/lytlistView" 
android:layout_marginTop="4dp" 
android:layout_marginLeft="6dp" 
android:layout_marginRight="6dp" 
android:layout_below="@+id/lytListView"> 
</ListView> 

现在,我要为ListView控件添加两个字符串数组的值显示在下面:

------------------------- 
listHeader1 
listContent1 
------------------------- 
listHeader2 
listContent2 
------------------------- 
listHeader3 
listContent3 
------------------------- 

所以,listHeader[1,2,3,..]包含Array1[]元素和listContent[1,2,3,....]包含Array2[]元素

我该如何做到这一点?请帮帮我。

+0

您需要使用hashmap进行映射。 – Unknown 2013-04-29 05:50:52

+0

使用自定义列表视图http://techdroid.kbeanie.com/2009/07/custom-listview-for-android.html – rajeshwaran 2013-04-29 05:52:32

+1

尝试阅读[本博客文章](http ://bartinger.at/listview-with-sectionsseparators/)关于分段列表视图 – thepoosh 2013-04-29 06:04:17

回答

0

你可以将两个数组包装到一个HashMap对象中,然后使用SimpleAdapter。

+0

谢谢,我实施了同样的工作.... – user2326860 2013-04-29 08:44:14

0

按照以下说明声明字符串数组。

更改此:

String[] Array1 = [ele1, ele2, ele3, ele4, ele5, ele6]; 

String[] Array1 = {"ele1", "ele2", "ele3", "ele4", "ele5", "ele6"}; 

为此,您可以使用自定义列表视图,

检查HERE

+0

编辑我的问题..请现在检查... – user2326860 2013-04-29 05:53:58

+0

@ user2326860请参阅链接。 – 2013-04-29 05:56:28

+0

@ user2326860并让我知道是否有任何错误。 – 2013-04-29 06:11:17

0

为此,你必须使用BaseAdapter作为一个适配器的ListView。在您的基础适配器类中充胀具有两个文本视图的自定义视图。然后将该适配器设置为您的listView。

 public class YourAdapter extends BaseAdapter { 

Context mContext; 
ArrayList<ClubDetailContent> mArrayList; 

public ClubListAdapter(Context mContext, 
     ArrayList<ClubDetailContent> mArrayList) { 
    // TODO Auto-generated constructor stub 
    this.mContext = mContext; 
    this.mArrayList = mArrayList; 
} 

public int getCount() { 
    // TODO Auto-generated method stub 

    return mArrayList.size(); 
} 

public Object getItem(int position) { 
    // TODO Auto-generated method stub 
    return position; 
} 

public long getItemId(int position) { 
    // TODO Auto-generated method stub 
    return position; 
} 

public View getView(final int position, View convertView, ViewGroup parent) { 
    // TODO Auto-generated method stub 
    LayoutInflater layoutInflater = (LayoutInflater) mContext 
      .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
    View view = convertView; 
    view = layoutInflater.inflate(R.layout.listlayout, parent, false); 
    //Add your text view here 
      //set text to that textView according to your string 

    return view; 
    } 


} 

然后在主要活动中设置适配器。