I am developing an app that runs on a RFID scanner. One feature is, after scanning multiple tags, user clicks a button and the program jumps to a new page and display all the scanned data in desired format.
I’ve tested my code that the data has been passed to the List where it should be. But I just failed to display them.
Code in MainActivity.cs that switching page and passing scanned data:
private void OnClickButtonScan(object sender, System.EventArgs e)
{
Intent intent = new Intent(this, typeof(ListviewActivity));
intent.PutExtra("AviTagList", JsonConvert.SerializeObject(scannedAviTagList));
StartActivity(intent);
}
Two layouts, one is the new page, one is the listview row:
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>
The new page activity:
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;
}
}
The data adapter to the view:
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; }
}
Then the data class:
public class AviTag
{
public string EPC { get; set; }
}
There is no compile error or runtime error, but I just fail to display the data. Could someone help? Thank you.
>Solution :
I highly suggest you switch to using RecyclerView instead of using ListView.
One glaring issue you have in your Adapter is that Count always returns 0. Instead you should change it to:
public override int Count => TagList?.Count ?? 0;
If you always return 0, the Adapter won’t ever try to create rows.