无法在Xamarin的新页面中显示ListView数据,Android项目

q7solyqu  于 2023-04-27  发布在  Android
关注(0)|答案(1)|浏览(128)

我正在开发一个在RFID扫描仪上运行的应用程序。一个功能是,在扫描多个标签后,用户单击一个按钮,程序跳到一个新页面,并以所需格式显示所有扫描的数据。
我已经测试了我的代码,数据已经传递到了它应该在的列表中。但是我没有显示它们。

MainActivity.cs中切换页面和传递扫描数据的代码:

private void OnClickButtonScan(object sender, System.EventArgs e)
    {
        Intent intent = new Intent(this, typeof(ListviewActivity));
        intent.PutExtra("AviTagList", JsonConvert.SerializeObject(scannedAviTagList));
        StartActivity(intent);
    }

两个布局,一个是新页面,一个是列表视图行:

tag_listview.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <ListView
         android:layout_width="match_parent"
         android:layout_height="match_parent"
        android:id="@+id/taglistview"/>
</LinearLayout>

listview_row.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingLeft="5dp"
    android:paddingBottom="2dp"
    android:paddingTop="2dp">
    <TextView
        android:text="EPC"
        android:layout_width="match_parent"
        android:layout_weight="25"
        android:layout_height="wrap_content"
        android:id="@+id/txtEPC"/>
</LinearLayout>

新页面活动:

ListviewActivity.cs

[Activity(Label = "ListviewActivity")]
public class ListviewActivity : Activity
{
    private ListView tagListView;
    private List<AviTag> scannedAviTagList = new List<AviTag>();

    protected override void OnCreate(Bundle savedInstanceState)
    {
        base.OnCreate(savedInstanceState);
        SetContentView(Resource.Layout.tag_listview);
        tagListView = FindViewById<ListView>(Resource.Id.taglistview);
        // Create your application here
        string aviTagListJson = Intent.GetStringExtra("AviTagList");
        this.scannedAviTagList = JsonConvert.DeserializeObject<List<AviTag>>(aviTagListJson);

        TagListviewAdapter tagReadListAdapter = new TagListviewAdapter(this, this.scannedAviTagList);
        tagListView.Adapter = tagReadListAdapter;
    }
}

视图的数据适配器:

TagListviewAdapter.cs

public class TagListviewAdapter : BaseAdapter<AviTag>
{
    public List<AviTag> TagList { get; set; }
    private Context context;

    public TagListviewAdapter(Context context, List<AviTag> items)
    {
        this.TagList = items;
        this.context = context;
    }

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

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

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

        if (view != null)
            holder = view.Tag as TagListviewAdapterViewHolder;

        if (holder == null)
        {
            holder = new TagListviewAdapterViewHolder();
            var inflater = context.GetSystemService(Context.LayoutInflaterService).JavaCast<LayoutInflater>();
            view = inflater.Inflate(Resource.Layout.listview_row, parent, false);
            holder.EPC = view.FindViewById<TextView>(Resource.Id.txtEPC);
            view.Tag = holder;
        }
        var tagReadData = TagList[position];

        if (!string.IsNullOrEmpty(tagReadData.EPC))
        {
            holder.EPC.Text = tagReadData.EPC;
            holder.EPC.Visibility = ViewStates.Visible;
        }
        else
        {
            holder.EPC.Visibility = ViewStates.Gone;
        }

        return view;
    }

    //Fill in cound here, currently 0
    public override int Count
    {
        get
        {
            return 0;
        }
    }

    public override AviTag this[int position]
    {
        get { return TagList[position]; }
    }
}

internal class TagListviewAdapterViewHolder : Java.Lang.Object
{
    //Your adapter views to re-use
    public TextView EPC { get; set; }
}

然后是数据类:

public class AviTag
{
    public string EPC { get; set; }
}

没有编译错误或运行时错误,但我只是无法显示数据。有人可以帮助吗?谢谢。

unguejic

unguejic1#

我强烈建议你切换到使用RecyclerView而不是使用ListView。
Adapter中的一个突出问题是Count总是返回0。相反,您应该将其更改为:

public override int Count => TagList?.Count ?? 0;

如果总是返回0,则Adapter将永远不会尝试创建行。

相关问题