ListView Creator调用但没有其他方法

Ste*_*ton 11 c# android listview xamarin

这是我第一次使用ListView而且遇到了一些麻烦.我确信我的技术实现不正确.然而,经过大量搜索互联网和观看列表视图的教程后,我还没有想出来.

然而,这有时会显示,大部分时间它都没有启动.当它显示时,它是在屏幕关闭时我运行应用程序并打开设备屏幕并显示列表.尽管如此,这非常受欢迎.

每次都会调用构造函数,但是之后,从不调用Count和GetView.

似乎所有内容都显示在我的main.axml文件中

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:p1="http://schemas.android.com/apk/res/android"
    p1:orientation="vertical"
    p1:layout_width="match_parent"
    p1:layout_height="match_parent"
    p1:id="@+id/linearLayout1">
    <Spinner
        p1:layout_width="match_parent"
        p1:layout_height="50.5dp"
        p1:id="@+id/stores"
        p1:layout_marginBottom="16.0dp" />
    <Button
        p1:id="@+id/scanItem"
        p1:layout_width="fill_parent"
        p1:layout_height="wrap_content"
        p1:text="Scan Item" />
    <ListView
        p1:minWidth="25px"
        p1:minHeight="25px"
        p1:layout_width="match_parent"
        p1:layout_height="match_parent"
        p1:id="@+id/itemView" />
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)

在我的主要活动中,我追踪了一切,一切都被调用了.

为了给你一些背景知识,我是如何创建我发送到我正在使用的自定义适配器的列表.我有一个名为RootObject的自定义对象,它包含一个Items列表

 var list = JsonConvert.DeserializeObject<RootObject>(response);
ListView myItems = FindViewById<ListView>(Resource.Id.itemView);
PIAdapter itemViewAdapter = new PIAdapter(this, list);
myItems.Adapter = itemViewAdapter;
Run Code Online (Sandbox Code Playgroud)

这一切似乎都有效

我的适配器构造函数甚至被调用,我可以确认我的列表中有2个项目.

但是,当我在Count和GetView中包含Console.WriteLine时,99%的时间都不会调用它们.然而,我可以调用构造函数中的所有字段并确认我已填入值,并且在某些条件下它可以正确显示.

public class PIAdapter : BaseAdapter
    {


        RootObject list = new RootObject();
        Activity context;

        public PIAdapter(Activity context, RootObject list)
        {
            this.list = list;
            this.context = context;
            Console.WriteLine("[My App] Step 10" + list.items.Count);

        }
        public override int Count
        {
            get
            {

                return list.items.Count;
            }
        }

        public override Java.Lang.Object GetItem(int position)
        {
            return null;
        }

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

        public override View GetView(int position, View convertView, ViewGroup parent)
        {
            Console.WriteLine("[My App] - Step 11");
            View view = convertView;
            if(view == null)
            {
                view = context.LayoutInflater.Inflate(Resource.Layout.myItem, null);
            }

            var item = list.items[position];

            ImageView customImage = view.FindViewById<ImageView>(Resource.Id.customImage);
            TextView customName = view.FindViewById<TextView>(Resource.Id.customName);
            TextView customBarcode = view.FindViewById<TextView>(Resource.Id.customBarcode);
            TextView customUp = view.FindViewById<TextView>(Resource.Id.customUpVote);
            TextView customDown = view.FindViewById<TextView>(Resource.Id.customDownVote);

            customName.Text = item.name;
            customBarcode.Text = item.barcode;
            customUp.Text = item.upvotes;
            customDown.Text = item.downvotes;

            //Koush.UrlImageViewHelper.SetUrlDrawable(customImage, "http://api.myurl.com/images/" + item.barcode + ".png", Resource.Drawable.myicon);

            return view;
        }


    }
}
Run Code Online (Sandbox Code Playgroud)

如果需要,我正在编辑它以包含myItem.axml文件

<?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"
    android:minWidth="25px"
    android:minHeight="25px">
    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/relativeLayout1">
        <ImageView
            android:src="@android:drawable/ic_menu_gallery"
            android:layout_width="100dp"
            android:layout_height="100dp"
            android:id="@+id/customImage" />
        <TextView
            android:text="Medium Text"
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_toRightOf="@id/customImage"
            android:id="@+id/customBarcode" />
        <TextView
            android:text="Medium Text"
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@id/customBarcode"
            android:id="@+id/customName"
            android:layout_toRightOf="@id/customImage" />
        <ImageView
            android:src="@drawable/up"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_toRightOf="@id/customImage"
            android:id="@+id/customUp"
            android:layout_below="@id/customName" />
        <TextView
            android:text="0"
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:layout_width="50dp"
            android:layout_height="wrap_content"
            android:layout_toRightOf="@id/customUp"
            android:id="@+id/customUpVote"
            android:layout_below="@id/customName" />
        <ImageView
            android:src="@drawable/down"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_toRightOf="@id/customUpVote"
            android:id="@+id/customDown"
            android:layout_below="@id/customName" />
        <TextView
            android:text="0"
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:layout_width="50dp"
            android:layout_height="wrap_content"
            android:layout_toRightOf="@id/customDown"
            android:id="@+id/customDownVote"
            android:layout_below="@id/customName" />
    </RelativeLayout>
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)

jze*_*ino 3

粘贴您的代码并在我的设备和模拟器中进行测试。我没有遇到你说的问题。

但我改变了一些事情。

这是我的活动代码:

var list = JsonConvert.DeserializeObject<RootObject>("{\"items\":[{\"barcode\": \"690443240066\",\"name\": \"gjjnn\",\"upvotes\": \"1\",\"downvotes\": \"0\",\"updated\": \"1466479409028\"},{\"barcode\": \"038000845031\",\"name\": \"fhj\",\"upvotes\": \"2\",\"downvotes\": \"1\",\"updated\": \"1466396732038\"}]}");
ListView myItems = FindViewById<ListView>(Resource.Id.itemView);
PIAdapter itemViewAdapter = new PIAdapter(this, list);
myItems.Adapter = itemViewAdapter;
Run Code Online (Sandbox Code Playgroud)

并在主布局中更改了这一点:

var list = JsonConvert.DeserializeObject<RootObject>("{\"items\":[{\"barcode\": \"690443240066\",\"name\": \"gjjnn\",\"upvotes\": \"1\",\"downvotes\": \"0\",\"updated\": \"1466479409028\"},{\"barcode\": \"038000845031\",\"name\": \"fhj\",\"upvotes\": \"2\",\"downvotes\": \"1\",\"updated\": \"1466396732038\"}]}");
ListView myItems = FindViewById<ListView>(Resource.Id.itemView);
PIAdapter itemViewAdapter = new PIAdapter(this, list);
myItems.Adapter = itemViewAdapter;
Run Code Online (Sandbox Code Playgroud)