使用Dictionary和ListFragment填充Listview

时间:2014-09-04 16:53:36

标签: android listview xamarin xamarin.android android-listfragment

如何使用每个行的片段中的字典填充列表视图并使用自定义布局?在行的布局中,包含两个textview和一个Imageview。

我的列表视图布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:orientation="vertical"
  android:layout_width="match_parent"
  android:layout_height="match_parent">
 <ListView
    android:id="@android:id/list"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:scrollbars="vertical"
    android:background="#ff04aefa" />
</LinearLayout>

布局行:

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

    <ImageView
        android:src="@android:drawable/ic_menu_gallery"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/imageView1" />

<LinearLayout
    android:id="@+id/singleMessageContainer"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">
    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_margin="5dip"
        android:paddingLeft="10dip"
        android:text="Texview 1"
        android:textColor="@android:color/background_light" />

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_margin="5dip"
        android:paddingLeft="10dip"
        android:textColor="@android:color/background_light" />
    </LinearLayout>
</LinearLayout>

我的ListFragment.cs:

Dictionary <string,string> _Dic = new Dictionary<string,string>();

    string[] _ValuesOfDic = { };

    public override View OnCreateView (LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
    {
        _Dic = MyProject.UtilDic.ReturnDic ();

        _ValuesOfDic = new string[_Dic.Count];

        int cont = 0;

        foreach (var item in _Dic) {

            _ValuesOfDic[cont] = item.Value;

            cont++;
        }

        View view = inflater.Inflate(Resource.Layout.LayoutListOfValues, null); //Layout with listView

        ListView listview = (ListView)view.FindViewById (Android.Resource.Id.List);

        listview.Adapter = new ArrayAdapter <string> (this.Activity, 
                                                      Android.Resource.Layout.SimpleListItem1, 
                                                      _ValuesOfDic); //Here I want to use the layout for the rows of listview 

        return view;
    }

    public override void OnListItemClick (ListView l, View v, int position, long id)
    {
        string value = l.Adapter.GetItem (position).ToString ();
        string key = _Dic.FirstOrDefault (x => x.Value.Contains (value)).Key;

        Console.WriteLine ("Value: " + value);
        Console.WriteLine ("Key: " + key);
    }

1 个答案:

答案 0 :(得分:0)

创建一个自定义Adapter,其继承自BaseAdapter,可以带您的Dictionary并相应地出现。 ArrayAdapter只接受列表或数组。

类似于:

public class MyDictionaryAdapter : BaseAdapter
{

    private Dictionary<TX,TY> _dict;

    public MyDictionaryAdapter(Dictionary<TX, TY> dict)
    {
        _dict = dict;
    }

    public override View GetView (int position, View convertView, ViewGroup parent)
    {
        // populate your listview items here
    }
}

您可以在此处阅读有关ListView和Adapters的更多信息:http://developer.xamarin.com/guides/android/user_interface/working_with_listviews_and_adapters/