0

我想在我的应用程序中创建一个列表。我正在使用 SherlockListFragment。问题是,我没有收到任何错误,我的应用程序只是向我显示一个带有以下消息的弹出窗口:“抱歉,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>

两行项目:

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;
        }
    }
}
4

1 回答 1

0

引导,在我所有的 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 设置为您定义的适配器。希望这可以帮助。

于 2014-02-07T18:07:15.083 回答