在Android中使用SimpleListItemActivated1和自定义适配器

时间:2014-04-08 10:13:54

标签: android listview xamarin

我已经在Xamarin中为我的Android应用程序构建了一个自定义适配器,因为我想自定义ListviewItems并且它可以工作。
问题是我希望突出显示所选的Listitem。所以我想使用SimpleListItemActivated1标志。但是你可以在这个上下文中使用ArrayAdapter这个标志:

_list.Adapter = new ArrayAdapter<string>(this, Android.Resource.Layout.SimpleListItemActivated1,
                        tempNames);

但是我已经实现了自己的适配器,我需要它来自定义我的listview项目:

_adapter = new AndroidUI.KundenViewAdapter (this,_kunden);
_list.Adapter = _adapter

如何在我的适配器中使用SimpleListItemActivated1

我的适配器的代码如下:

namespace AndroidUI
{
public class KundenViewAdapter : BaseAdapter<BusinessLayer.Kunde>
{
    private readonly Activity _context;
    private List<Kunde> _kunden;

    public KundenViewAdapter(Activity context, IEnumerable<Kunde> kunden)
    {
        _kunden = kunden.ToList ();
        _context = context;
    }

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

    public override long GetItemId(int position)
    {
        return Convert.ToInt64 ( _kunden[position].ID);
    }

    public override Kunde this[int position]
    {
        get { return _kunden[position]; }
    }

    public override View GetView(int position, View convertView, ViewGroup parent)
    {
        View view = convertView;
        if (view == null)
            view = _context.LayoutInflater.Inflate(Resource.Layout.PersonListItem, null);


        Kunde kunde = _kunden[position];
        TextView textview = view.FindViewById<TextView> (Resource.Id.nameTextView);

        textview.Text = kunde.Name;

        if (String.IsNullOrEmpty (kunde.Nummer))
            view.FindViewById<TextView> (Resource.Id.NummerTextView).Visibility = ViewStates.Gone;
        else
            view.FindViewById<TextView> (Resource.Id.NummerTextView).Text = kunde.Nummer;

        return view;
    }
}

public class LieferantViewAdapter : BaseAdapter<BusinessLayer.Lieferant>
{
    private readonly Activity _context;
    private List<Lieferant> _lieferant;

    public LieferantViewAdapter(Activity context, IEnumerable<Lieferant> lieferant)
    {
        _lieferant = lieferant.ToList ();
        _context = context;
    }

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

    public override long GetItemId(int position)
    {
        return Convert.ToInt64 ( _lieferant[position].ID);
    }

    public override Lieferant this[int position]
    {
        get { return _lieferant[position]; }
    }

    public override View GetView(int position, View convertView, ViewGroup parent)
    {
        View view = convertView;
        if (view == null)
            view = _context.LayoutInflater.Inflate(Resource.Layout.PersonListItem, null);

        Lieferant lieferant = _lieferant[position];
        TextView textview = view.FindViewById<TextView> (Resource.Id.nameTextView);

        textview.Text = lieferant.Name;

        if (String.IsNullOrEmpty (lieferant.Nummer))
            view.FindViewById<TextView> (Resource.Id.NummerTextView).Visibility = ViewStates.Gone;
        else
            view.FindViewById<TextView> (Resource.Id.NummerTextView).Text = lieferant.Nummer;

        return view;
    }
}
}

1 个答案:

答案 0 :(得分:0)

首先在类中声明View selectedRow;并在setonitemclickListner

中添加以下代码
if (selectedRow != null) {
            selectedRow.setBackgroundResource(color.transparent);
        }
        selectedRow = view;
        view.setBackgroundResource(R.drawable.list_background); 

这将在3.0版及以上版本中运行