Sam Ra
Sam Ra

Reputation: 255

How can I add Icons or pictures to items on ListActivity in Xamarin.Android.

I want to add either a tick or cross, depending on the state of my objects properties. This is my code in a MainActivity that passses list over using intent:

Button callHistoryButton = FindViewById<Button>(Resource.Id.ScanHistoryButton);
            callHistoryButton.Click += (sender, e) =>
            {
                var intent = new Intent(this, typeof(ScanHistoryActivity));
                intent.PutStringArrayListExtra("Codes", codes);
                StartActivity(intent);
            };

This is my code in my ListActivity:

public class ScanHistoryActivity : ListActivity
{
    protected override void OnCreate(Bundle bundle)
    {
        base.OnCreate(bundle);
        var codes = Intent.Extras.GetStringArrayList("Codes");
        codes.ToList();
        ListAdapter = new ArrayAdapter<string>(this, Android.Resource.Layout.SimpleListItemChecked, codes);
    }
}

This is the Object i created:

[DataContract]
public class Scan
{
    [DataMember]
    public string ScanValue { get; set; }
    [DataMember]
    public string Action { get; set; }

    public bool Success { get; set; }
}

So if an objects success property is true i want to display a green tick in ScanhistoryActivity and red cross if false. I dont want people to be able to select or deselect manually.

Is a list activity suitable for this?

Upvotes: 1

Views: 558

Answers (2)

CDrosos
CDrosos

Reputation: 2528

You have to create your own custom Adapter for this to work.

Also you have to create a layout in order to populate with the adapter the list with data (the layout of each row for example).

For example:

AdapterRow.axml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<CheckBox
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:id="@+id/checkBox1"
    android:focusable="false"
    android:focusableInTouchMode="false" />
<ImageView xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="50dp"
    android:layout_height="50dp"
    android:layout_gravity="center"
    android:background="#ff997eff"
    android:layout_marginLeft="10dp"
    android:id="@+id/BusinessImageView" />

Adapter.cs

using System;
using System.Collections.Generic;
using System.Globalization;
using System.Net;
using System.Threading.Tasks;

using System.Linq;

using Android.App;
using Android.Graphics;
using Android.Views;
using Android.Widget;

namespace testProject
{
public class SomeAdapter : BaseAdapter<Staff>
{
    private readonly List<Staff> _list;
    private readonly Activity _context;
    public int position;


    public SomeAdapter (Activity context, List<Staff> list)
    {
        _context = context;
        _list = list;
    }

    public List<Staff> GetList()
    {
        return _list;
    } 

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

    public override int Count
    {
        get { return _list.Count; }
    }

    public override Staff this[int position]
    {
        get { return _list[position]; }
    }

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

        if (view == null) {
            view = _context.LayoutInflater.Inflate(Resource.Layout.adapterRow, null);
        }

        ImageView img1 = view.FindViewById<ImageView> (Resource.Id.BusinessImageView);
        CheckBox chk  = view.FindViewById<CheckBox> (Resource.Id.checkBox1);
//Do aything you want with the data here using _list[position]

        return view;

    }

}

}

And this is how to populate the listview with the adapter

_ListView = FindViewById<ListView>(Resource.Id.ListView);
_ListView .ItemClick += _ListView_OnItemClick;
_staffList = new List<Staff>();
_staffList = //Fill it with data
var adapter = new SomeAdapter (this, _staffList);
_ListView.Adapter = adapter;
adapter.NotifyDataSetChanged();

And to check or uncheck the tick:

private void _ListView_OnItemClick(object sender, AdapterView.ItemClickEventArgs e)
    {
        {
            CheckBox chk = e.View.FindViewById<CheckBox>(Resource.Id.checkBox1);
            if (chk.Checked == true)
            {
                chk.Checked = false;

            }
            else
            {
                chk.Checked = true;
            }
        }

Upvotes: 1

Sam Ra
Sam Ra

Reputation: 255

protected override void OnCreate(Bundle bundle) { base.OnCreate(bundle); var codes = Intent.Extras.GetStringArrayList("Codes");

        codes.ToList();
        ListAdapter = new ArrayAdapter<string>(this, Android.Resource.Layout.SimpleListItemChecked, codes);
        ListView lv = FindViewById<ListView>(Android.Resource.Id.List);
        lv.ChoiceMode = ChoiceMode.Multiple;
        lv.Clickable = false;

        //CheckBox chk = lv.FindViewById<CheckBox>(Resource.Id.checkBox1);         
        foreach (var c in codes)
        {
            if (c.Contains("SENT SUCCESSFULLY"))
            {
                int postion = codes.IndexOf(c);
                lv.SetItemChecked(postion, true);
                lv.Clickable = false;
            }
        }
    }

Upvotes: 0

Related Questions