Заполнение списка с помощью словаря и ListFragment

Как я могу заполнить список с помощью словаря из фрагмента для каждой строки и использовать собственный макет? В макете строки содержится два текстовых представления и одно изображение.

Мой макет со списком:

<?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);
    }

person julius_cesars    schedule 04.09.2014    source источник


Ответы (1)


Создайте пользовательский 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 и адаптерах можно прочитать здесь: http://developer.xamarin.com/guides/android/user_interface/working_with_listviews_and_adapters/

person Cheesebaron    schedule 04.09.2014