我有一个简单Xamarin.Android-应用。
在这个应用程序的活动,有显示的列表。请参阅以下MyActivity.axml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android ="http://schemas.android.com/apk/res/android"
android:orientation ="vertical"
android:layout_width ="match_parent"
android:layout_height="match_parent">
<Button
android:layout_width ="match_parent"
android:layout_height="wrap_content"
android:text ="Click Me"
android:layout_marginTop ="20dp"
android:layout_marginLeft ="25dp"
android:layout_marginRight ="25dp"
android:id="@+id/button" />
<Space
android:layout_width="match_parent"
android:layout_height="25dp"
android:id="@+id/space" />
<ListView
android:minWidth="25px"
android:minHeight="25px"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/list" />
</LinearLayout>
这个列表中的一个行包含简单的两个条目,请参阅ListRow.axml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView
android:id="@+id/name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft ="5dp"
android:layout_marginTop ="10dp"
android:layout_marginBottom="10dp"
android:layout_marginRight ="5dp" />
<TextView
android:id="@+id/value"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft ="5dp"
android:layout_marginTop ="10dp"
android:layout_marginBottom="10dp"
android:layout_marginRight ="5dp" />
</RelativeLayout>
因此该活动的后台代码如下所示:
public class MyActivity : Activity
{
List<Entry> List = null;
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
SetContentView(Resource.Layout.MyActivity);
List = PopulateList();
var lv = FindViewById<ListView>(Resource.Id.list);
var adapter = new ListAdapter(this, Resource.Layout.List, List);
lv.Adapter = adapter;
FindViewById<Button>(Resource.Id.button).Click += OnButtonClick;
}
}
为了完整起见,这里是我的ListAdapter.cs类的代码:
class ListAdapter : ArrayAdapter
{
List<Entry> List;
public ListAdapter(Context Context, int ListId, List<Entry> List) : base(Context, ListId, List)
{
this.List = List;
}
public override int Count
{
get { return List.Count; }
}
public override View GetView(int position, View convertView, ViewGroup parent)
{
View v = convertView;
if (v == null)
{
LayoutInflater inflater = (LayoutInflater)Context.GetSystemService(Context.LayoutInflaterService);
v = inflater.Inflate(Resource.Layout.List, parent, false);
}
v.FindViewById<TextView>(Resource.Id.name).Text = List[position].Name;
v.FindViewById<TextView>(Resource.Id.value).Text = "Original Value";
return v;
}
}
所以我现在的问题是:假设有在该列表中的多个项目。在按钮的点击,我想改变该列表中的一个特定的文本。比方说,在列表中(Resource.Id.value)。文本(现称“原始值”),以“改值”或类似的东西,第二项。但只有第二个的。所有其他物品应保持不变。
请看下面的例子输出的目的,也许是更容易理解什么,我试图做的:
名称值 - - - - - - - - - - - - - - - - 一号原值 二原值 3号原值
[按钮点击]
名称值 - - - - - - - - - - - - - - - - 一号原值 2号更改的值 3号原值
任何人都可以或许能帮我吗/告诉我该怎么做呢?什么是我的private void OnButtonClick(object sender, EventArgs e)
法有什么样子的?我如何可以访问该列表中的一个条目?
预先感谢所有的答案和问候
你可以在适配器添加一个方法来notifyDataSetChanged();
:
class ListAdapter : ArrayAdapter
{
List<Entry> List;
private int ItemPositionToChange = -1;
public ListAdapter(Context Context, int ListId, List<Entry> List) :
base(Context, ListId, List)
{
this.List = List;
}
//this method to let adapter notify
public void ChangeItemValue(int position)
{
ItemPositionToChange = position;
NotifyDataSetChanged();
}
public override int Count
{
get { return List.Count; }
}
public override View GetView(int position, View convertView, ViewGroup parent)
{
View v = convertView;
if (v == null)
{
LayoutInflater inflater =
(LayoutInflater)Context.GetSystemService(Context.LayoutInflaterService);
v = inflater.Inflate(Resource.Layout.List, parent, false);
}
v.FindViewById<TextView>(Resource.Id.name).Text = List[position].Name;
//change the item value according to your needs
if(position == ItemPositionToChange ){
v.FindViewById<TextView>(Resource.Id.value).Text = "Changed Value";
}else{
v.FindViewById<TextView>(Resource.Id.value).Text = "Original Value";
}
return v;
}
}
那么在您的按钮点击监听器:
void OnButtonClick(object sender, EventArgs e)
{
//positionToChange is which item you want to change
adapter.ChangeItemValue(positionToChange);
}
我不是太熟悉的Entry类,但它似乎有一个Text属性。
假设你知道的字符串(以下myIndex)的指标:
private void OnButtonClick(object sender, EventArgs e) {
List[myIndex].Text = "Changed Value";
adapter.notifyDataSetChanged();
}