清空listview没有错误xamarin android

时间:2017-11-24 04:37:16

标签: android listview xamarin fragment android-adapter

陷入奇怪的问题。我正在尝试用片段中的customlistadpater填充listview,但问题是列表没有填充。代码只返回列表计数,然后没有。

试图在

设置断点
public override long GetItemId (int position)
    {
        return position;
    }

但是代码没有在

之后执行
public override int Count {
        get { return list.Count; } // Count 20
    }

它只是返回计数而不是之后执行代码。

CustomListAdapter:

public class CusotmListAdapter : BaseAdapter<Post>
{
    //Activity context;
    List<Post> list;
    Fragment1 context;



    public CusotmListAdapter(Fragment1 context, List<Post> list) :base()
    {
        this.context = context;
        this.list = list;
    }

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

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

    public override Post this[int index] {
        get { return list [index]; }
    }


    public override View GetView (int position, View convertView, ViewGroup parent)
    {
        View view = convertView; 


        if (view == null)
            view = context.LayoutInflater.Inflate (Resource.Layout.ListRowLayout, parent, false);

        Post item = this [position];
        view.FindViewById<TextView>(Resource.Id.Title).Text = Android.Text.Html.FromHtml(item.article_title_mr).ToString();

        string dt = item.article_date;

        DateTime oDate = Convert.ToDateTime(dt);


        string dt2 = oDate.ToString("MMM dd, yyyy hh:mm ");





        string city = item.article_location;


        view.FindViewById<TextView>(Resource.Id.Description).Text = dt2+" | "+ city;


        using (var imageView = view.FindViewById<ImageView> (Resource.Id.Thumbnail)) {
            string url = Android.Text.Html.FromHtml (item.article_image).ToString ();

            //Download and display image
            Koush.UrlImageViewHelper.SetUrlDrawable (imageView, 
                url, Resource.Drawable.Icon);
        }
        return view;
    }
}

ListView代码:

               var httpClient = new HttpClient();
                Task<string> contentsTask = httpClient.GetStringAsync("link");

                // await! control returns to the caller and the task continues to run on another thread
                string content = await contentsTask;
                Console.Out.WriteLine("Response Body: \r\n {0}", content);

                //Convert string to JSON object
                result = JsonConvert.DeserializeObject<RootObject>(content);


           Activity.RunOnUiThread(() => {



                listView.Adapter = new CusotmListAdapter(this, result.posts);                  

           });

调试输出中没有显示错误。请帮助TIA ..

1 个答案:

答案 0 :(得分:0)

public override long GetItemId (int position)
在列表视图创建时未调用

如果要查看每个视图的创建方式,请在

中添加断点
public override View GetView (int position, View convertView, ViewGroup parent)