大家好,
在我的程序中,我有一个按钮和一个列表显示。单击按钮时,我只需要更改列表中特定项目的值即可。请参见以下目标输出示例:
[程序开始]
[按钮点击]
那我该怎么做呢?我的私有void OnButtonClick(object sender,EventArgs e)方法应具有什么样的外观?如何访问该列表中的特定条目?
为了更好地为我提供帮助,请让我分享以下完整的代码:
使用以下* .axml文件:
因此,我该如何访问相应的视图/在 私有void OnButtonClick(对象发送者,EventArgs e)-方法?
如果我做
只有第一项改变。那么如何通过适配器传递位置呢?如何只更改第二个(在本示例中)(如开头所示)?有人可以帮忙吗?
提前致谢,
最好的祝福
在我的程序中,我有一个按钮和一个列表显示。单击按钮时,我只需要更改列表中特定项目的值即可。请参见以下目标输出示例:
[程序开始]
名称 | 值 |
商品编号1 | 原始值 |
商品编号2 | 原始值 |
产品编号3 | 原始值 |
[按钮点击]
名称 | 值 |
商品编号1 | 原始值 |
商品编号2 | 变更值 |
产品编号3 | 原始值 |
那我该怎么做呢?我的私有void OnButtonClick(object sender,EventArgs e)方法应具有什么样的外观?如何访问该列表中的特定条目?
为了更好地为我提供帮助,请让我分享以下完整的代码:
C#:
public class MyActivity : Activity
{
List List = null;
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
SetContentView(Resource.Layout.MyActivity);
List = PopulateList();
var lv = FindViewById(Resource.Id.list);
var adapter = new ListAdapter(this, Resource.Layout.List, List);
lv.Adapter = adapter;
FindViewById(Resource.Id.button).Click += OnButtonClick;
}
}
C#:
class ListAdapter : ArrayAdapter
{
List List;
public ListAdapter(Context Context, int ListId, List 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(Resource.Id.name).Text = List[position].Name;
v.FindViewById(Resource.Id.value).Text = "Original 值";
return v;
}
}
C#:
<?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:id="@+id/button" />
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/list" />
</LinearLayout>
C#:
<?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" />
<TextView
android:id="@+id/value"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</RelativeLayout>
如果我做
C#:
私有void OnButtonClick(对象发送者,EventArgs e)
{
FindViewById(Resource.Id.value).Text = "Changed 值";
}
提前致谢,
最好的祝福