Подтвердить что ты не робот

ListView Creator вызван, но нет других методов

Это моя первая работа с 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>

В моей основной деятельности я проследил все, и все вызвано.

Чтобы дать вам некоторое представление о том, как я создаю список, который я отправляю на пользовательский адаптер, который я использую. У меня есть пользовательский объект RootObject, который содержит список элементов

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

Все это работает

Конструктор My Adapter даже вызывается, и я могу подтвердить, что в моем списке находятся 2 элемента.

Однако, когда я включаю Console.WriteLine в Count и GetView в 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;
        }


    }
}

В случае необходимости я редактирую это, чтобы включить файл 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>
4b9b3361

Ответ 1

Вставьте код и протестируйте его на устройстве и эмуляторе. Я не имею проблемы, о которой вы говорили.

Но я кое-что изменил.

Это мой код активности:

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;

И изменилось это в главном макете:

<?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="50dp"
        p1:id="@+id/stores"
        p1:layout_marginBottom="16dp" />
    <Button
        p1:id="@+id/scanItem"
        p1:layout_width="fill_parent"
        p1:layout_height="wrap_content"
        p1:text="Scan Item" />
    <ListView
        p1:layout_width="match_parent"
        p1:layout_height="match_parent"
        p1:id="@+id/itemView" />
</LinearLayout>