我想添加刻度线或十字形,具体取决于我的对象属性的状态。这是我在MainActivity中使用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);
};
这是我在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);
}
}
这是我创建的对象:
[DataContract]
public class Scan
{
[DataMember]
public string ScanValue { get; set; }
[DataMember]
public string Action { get; set; }
public bool Success { get; set; }
}
因此,如果对象成功属性为true,我想在ScanhistoryActivity中显示绿色勾号,如果为false则显示红色勾号。我不希望人们能够手动选择或取消选择。
列表活动是否适用于此?
答案 0 :(得分:1)
您必须创建自己的自定义适配器才能使其正常工作。
此外,您还必须创建一个布局,以便使用数据填充适配器(例如每行的布局)。
例如:
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;
}
}
}
这是使用适配器填充listview的方法
_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();
选中或取消选中勾号:
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;
}
}
答案 1 :(得分:0)
protected override void OnCreate(Bundle bundle) { base.OnCreate(束); 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;
}
}
}