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

问题描述 投票:0回答:0

我正在开发一个在 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; }
}

没有编译错误和运行错误,只是数据显示失败。有人可以帮忙吗?谢谢。

xamarin xamarin.android
© www.soinside.com 2019 - 2024. All rights reserved.