Xamarin SherlockListFragment

时间:2013-11-13 18:40:23

标签: c# android .net actionbarsherlock xamarin

在我的应用程序中创建一个List。我正在使用SherlockListFragment。 问题是,我没有得到任何错误,我的应用程序只是给我一个PopUp与以下消息:“抱歉不幸的是,App_Name已经停止。” 我得到了我需要的所有数据,并且应用程序停止返回rootView

谢谢你的帮助!

守则如下: Groupsfragment

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Android.App;
using Android.Content;
using Android.OS;
using Android.Runtime;
using Android.Util;
using Android.Views;
using Android.Widget;
using Xamarin.ActionbarSherlockBinding.App;

namespace Community_Task_Manager
{
    public class GroupsFragment : SherlockListFragment
    {
        List<TwoLineItem> list_items = new List<TwoLineItem> ();

        public override View OnCreateView(LayoutInflater p_layout, ViewGroup p_view, Bundle p_bundle)
        {
            // Set view
            var rootView = p_layout.Inflate(Resource.Layout.ListFragment, p_view, false);


            // Get Data from Server
            string jsonToSend;
            DataBaseSendObject dbs = new DataBaseSendObject ();
            dbs.CommandId = 4;
            dbs.AddParameter ("@UserId", Localpreference.GetUserId (Application.Context), 2);
            jsonToSend = dbs.JsonSerialization ();
            object objRecieved;
            objRecieved = DatabaseConnection.SendToServer(dbs);

            // Bringing Items into the right Format
            list_items = BM_JsonSerializer.GetListFromJson<TwoLineItem> (objRecieved.ToString ());
            TwoLineItem createNewGroupe = new TwoLineItem ();
            createNewGroupe.Id = 0;
            createNewGroupe.FirstLine = "Create new Group";
            createNewGroupe.SecondLine = "";


            this.ListAdapter = new TwoLineItemAdapter(Activity, list_items);
            //SetListShown (true);
            // Adding data to the View with an Adapter
            //ListView lst = rootView.FindViewById<ListView> (Resource.Id.list);
            //lst.Adapter = new TwoLineItemAdapter(this.Activity, list_itemsss);
            //lst.ItemClick += OnListItemClick;  // to be defined

            return rootView;


        }
}
}

ListFragment.axml

<?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent">
        <ListView
            android:minWidth="25px"
            android:minHeight="25px"
            android:layout_width="fill_parent"
            android:layout_height="match_parent"
            android:id="@+id/list" />
    </LinearLayout>

TwoLineItem.axml

<?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:padding="8dp">
        <LinearLayout
            android:id="@+id/TwoLineItemLayout"
            android:orientation="vertical"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:paddingLeft="10dip">
            <TextView
                android:id="@+id/FirstLine"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textColor="#FFFFFF"
                android:textSize="20dip"
                android:textStyle="italic" />
            <TextView
                android:id="@+id/SecondLine"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textSize="14dip"
                android:textColor="#FFFFFF" />
        </LinearLayout>
    </RelativeLayout>

TwoLineItem:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Android.App;
using Android.Content;
using Android.OS;
using Android.Runtime;
using Android.Views;
using Android.Widget;

namespace Community_Task_Manager
{
    class TwoLineItem
    {
        public int Id { get; set; }
        public string FirstLine { get; set; }
        public string SecondLine { get; set; }

    }
}

TwoLineItemAdapter:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Android.App;
using Android.Content;
using Android.OS;
using Android.Runtime;
using Android.Views;
using Android.Widget;

namespace Community_Task_Manager
{
    class TwoLineItemAdapter : BaseAdapter<TwoLineItem>
    {
        List<TwoLineItem> items;
        Activity context;
        public TwoLineItemAdapter(Activity context, List<TwoLineItem> items) : base()
        {
            this.context = context;
            this.items = items;
        }

        public override long GetItemId(int position)
        {
            return position;
        }

        public override TwoLineItem this[int position]
        {
            get { return items[position]; }
        }

        public override int Count
        {
            get { return items.Count; }
        }

        public override View GetView(int position, View convertView, ViewGroup parent)
        {
            var item = items[position];
            View view = convertView;
            if (view == null) // no view to re-use, create new
                view = context.LayoutInflater.Inflate (Resource.Layout.TwoLineItem, null);
            view.FindViewById<TextView>(Resource.Id.FirstLine).Text = item.FirstLine;
            view.FindViewById<TextView>(Resource.Id.SecondLine).Text = item.SecondLine;

            return view;
        }
    }
}

1 个答案:

答案 0 :(得分:0)

Boot,在SherlockListFragment的所有实现中,我都没有发现我必须给列表视图本身充气。在我的所有实现中,我只是在OnActivityCreated方法中为列表定义适配器。简而言之,我的代码看起来像这样

    public override void OnActivityCreated(Bundle savedInstanceState)
    {
        base.OnActivityCreated(savedInstanceState);            
        SetHasOptionsMenu(true); 
         if (_adapter == null)
            _adapter = new SkillAdapter(Activity, MyApp, MyCharacter); //MyCustom Adapter class.
        ListAdapter = _adapter;
        ListView.OnItemLongClickListener = _adapter;               

    }

    public override void OnActivityCreated(Bundle savedInstanceState){ 
        base.OnActivityCreated(savedInstanceState);    
        // Get Data from Server
        string jsonToSend;
        DataBaseSendObject dbs = new DataBaseSendObject ();
        dbs.CommandId = 4;
        dbs.AddParameter ("@UserId", Localpreference.GetUserId (Application.Context), 2);
        jsonToSend = dbs.JsonSerialization ();
        object objRecieved;
        objRecieved = DatabaseConnection.SendToServer(dbs);

        // Bringing Items into the right Format
        list_items = BM_JsonSerializer.GetListFromJson<TwoLineItem> (objRecieved.ToString ());
        TwoLineItem createNewGroupe = new TwoLineItem ();
        createNewGroupe.Id = 0;
        createNewGroupe.FirstLine = "Create new Group";
        createNewGroupe.SecondLine = "";

        ListAdapter = new TwoLineItemAdapter(Activity, list_items);
        //SetListShown (true);
        // Adding data to the View with an Adapter
        //ListView lst = rootView.FindViewById<ListView> (Resource.Id.list);
        //lst.Adapter = new TwoLineItemAdapter(this.Activity, list_itemsss);
        //lst.ItemClick += OnListItemClick;  // to be defined




    }

因为它是一个特定的碎片我相信Sherlock在幕后做其他事情。您需要做的就是定义适配器,然后将ListAdapter设置为您定义的适配器。希望这会有所帮助。