【问题标题】:ListView Creator called but no other methods调用 ListView Creator 但没有其他方法
【发布时间】:2016-06-21 05:05:32
【问题描述】:

这是我第一次使用 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;

这一切似乎都有效

我的适配器构造函数甚至被调用,我可以确认 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;
        }


    }
}

如果需要,我正在编辑它以包含 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>

【问题讨论】:

  • Console.WriteLine("[My App] Step 10" + list.items.Count); 有什么价值?
  • 因为我忘了在 10 之后包含一个空格,所以我得到了这个。 06-21 00:56:32.266 I/mono-stdout(21134): [我的应用] 步骤 102
  • 如果有帮助,这是我的数据源。它是一个包含 2 个项目的 json {"items":[{"barcode":"690443240066","name":"gjjnn","upvotes":"1","downvotes":"0","updated": "1466479409028"},{"barcode": "038000845031","name": "fhj","upvotes": "2","downvotes": "1","updated": "1466396732038"}]}跨度>
  • 您何时以及如何填写列表?请提供代码给我们
  • myItems.Adapter = itemViewAdapter 是否在主线程下运行?

标签: c# android listview xamarin


【解决方案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>

【讨论】:

  • 感谢您的回复。这个周末我会去看看。工作了漫长的一周,这是一个副业。
  • 本周末尚未因应用程序上的其他功能而延迟。我明天去看看。
  • 我还没有机会看一遍。工作有点炸了。我授予赏金是因为您的数据是我迄今为止最好的数据,一旦有机会查看,我会在这里回复。
  • 好的,谢谢。我确保您的代码在设备和模拟器上运行良好。
  • @StevenDStanton 如果这对您有用,请将其标记为正确,以便其他人可以得到帮助。谢谢。
猜你喜欢
  • 1970-01-01
  • 2018-04-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-08-19
  • 2013-01-23
  • 2022-06-15
相关资源
最近更新 更多