【发布时间】:2019-02-01 15:45:17
【问题描述】:
我有一个简单的 Xamarin.Android-App。
在这个应用的Activity上,有一个列表显示。请参阅以下 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 的代码:
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).Text(现在说“原始值”)到“更改值”或类似的东西。但仅限于第二个。所有其他项目应保持不变。
请看下面的输出示例,也许更容易理解我想要做什么:
名称 值
- - - - - - - - - - - - - - - --
No.1 原始价值
No.2 原值
No.3 原始价值
[按钮点击]
名称 值
- - - - - - - - - - - - - - - --
No.1 原始价值
No.2 更改值
No.3 原始价值
谁能帮助我/告诉我该怎么做?我的private void OnButtonClick(object sender, EventArgs e)-方法必须是什么样子?如何访问该列表中的单个条目?
提前感谢所有回答和问候
【问题讨论】:
-
如果您的按钮在列表视图之外,您能否获得指示,例如当您单击 Clicked 事件时 Item 需要更改哪个位置?如果可以的话,你可以在适配器中定义一个方法,单击按钮通过适配器传递位置,然后刷新数据以更改该项的值。或者你可以给每个listitem添加一个按钮,然后点击按钮来改变对应item的值
-
您好@LeoZhu,非常感谢您的帮助。正如试图在上面的两个表中显示的那样,在这个例子中,需要更改的项目的位置是
int positionToChange = 1;(从 0 开始计数)。不是第一项,不是最后一项,只有第二项。如何通过适配器传递位置? -
你的意思是点击按钮后永远改变第二项的值吗?
-
是的。至少现在是这样。
标签: list listview xamarin.android listadapter