如何将单独的参数传递给 Xamarin Android C# 中的 ListView.ItemClick 调用
Posted
技术标签:
【中文标题】如何将单独的参数传递给 Xamarin Android C# 中的 ListView.ItemClick 调用【英文标题】:How to pass a separate argument through to a ListView.ItemClick call in Xamarin Android C# 【发布时间】:2020-06-10 21:17:40 【问题描述】:所以我试图将参数传递给 ListView.ItemClick 调用,但我似乎无法弄清楚如何在没有任何错误的情况下做到这一点,这是我的代码:
public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
base.OnCreateView(inflater, container, savedInstanceState);
var view = inflater.Inflate(Resource.Layout.Dialog_Load, container, false);
mListView = view.FindViewById<ListView>(Resource.Id.LoadListView);
var responseResult = Load(UserID).Result;
//Creates an array that contains all the different names and IDs
string[] responseList = responseResult.Split(",");
//Using lists to hold the different number of values as they can expand dynamically as opposed to a static array
var ProgressionIDs = new List<string>();
var ProgressionNames = new List<string>();
//Separates ChordProgressionID from ChordProgressionName
for (int index = 0; index < responseList.Length; index++)
if (int.TryParse(responseList.ElementAt(index), out _))
ProgressionIDs.Add(responseList.ElementAt(index));
else if (responseList.ElementAt(index) == "")
//Stops the last comma from responseResult creating an empty slot in the array,
//which would create an unwanted empty item in the listview.
else
ProgressionNames.Add(responseList.ElementAt(index));
//Converts name list to array so that it can be set to the adapter
string[] ProgressionNamesArr = new string[ProgressionNames.Count()];
ProgressionNamesArr = ProgressionNames.ToArray();
ArrayAdapter<string> adapter = new ArrayAdapter<string>(this.Activity, android.Resource.Layout.SimpleListItem1, ProgressionNamesArr);
mListView.Adapter = adapter;
mListView.ItemClick += mListView_ItemClick;
return view;
public void mListView_ItemClick(object sender, AdapterView.ItemClickEventArgs e)
int position = e.Position;
var id = ProgressionIDs[e.Position];
private async Task<string> Load(string UserID)
//User has clicked the save button
HttpClient client = new HttpClient();
Uri uri = new Uri("**some url**");
HttpContent formContent = new FormUrlEncodedContent(new[]
new KeyValuePair<string, string>("UserID", UserID),
);
//Gets a response in terms of connection to the server
HttpResponseMessage response = await client.PostAsync(uri, formContent);
response.EnsureSuccessStatusCode();
//Gets 'echo' response from php file.
string responseResult = await response.Content.ReadAsStringAsync();
return responseResult;
我知道我现在拥有的 OnClick 子例程没有意义,因为我实际上并没有发送 ProgressionIDs
的实例。
我需要知道解决此问题的方法,以便我可以获取在 mListView
上单击的项目的位置,然后在该位置选择 ProgressionIDs
并将其存储在变量中。
【问题讨论】:
【参考方案1】:你能给我一个数据结构吗?一个 ProgressionName 对应一个 ProgressionID?
如果是这样,您可以为Progression
创建一个模型,其中包含两个属性,如以下数据结构。
public class Progression
public string ProgressionIDs get; set;
public string ProgressionNames get; set;
我使用静态数据来填充List<Progression>
。在listview中点击item可以获取postion,然后录制到postion获取List<Progression>
中的名字
List<Progression> progressions;
public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
View view = inflater.Inflate(Resource.Layout.layout1, container, false);
ListView listView1 = view.FindViewById<ListView>(Resource.Id.listView1);
progressions = new List<Progression>();
progressions.Add(new Progression() ProgressionIDs="1" , ProgressionNames= "Vegetables" );
progressions.Add(new Progression() ProgressionIDs = "2", ProgressionNames = "Fruits" );
progressions.Add(new Progression() ProgressionIDs = "3", ProgressionNames = "Flower Buds" );
progressions.Add(new Progression() ProgressionIDs = "4", ProgressionNames = "Legumes" );
progressions.Add(new Progression() ProgressionIDs = "5", ProgressionNames = "Bulbs" );
progressions.Add(new Progression() ProgressionIDs = "6", ProgressionNames = "Tubers" );
var myAdapter=new MyAdapter(this.Activity, progressions);
listView1.Adapter = myAdapter;
listView1.ItemClick += ListView1_ItemClick;
return view;
private void ListView1_ItemClick(object sender, AdapterView.ItemClickEventArgs e)
var myPostion= e.Position;
Progression progression = progressions[myPostion];
Toast.MakeText(this.Activity,progression.ProgressionIDs+" "+progression.ProgressionNames,ToastLength.Short).Show();
您应该为您的列表视图适配器创建 BaseAdapter。
public class MyAdapter : BaseAdapter<Progression>
private Activity mActivity;
private List<Progression> items;
public MyAdapter(Activity mActivity, List<Progression> progressions)
this.mActivity = mActivity;
this.items = progressions;
public override Progression this[int position] => items[position];
public override int Count => items.Count();
public override long GetItemId(int position)
return position;
public override View GetView(int position, View convertView, ViewGroup parent)
View view = convertView;
if (view == null) // no view to re-use, create new
view = mActivity.LayoutInflater.Inflate(Resource.Layout.SimpleListItem1, null);
view.FindViewById<TextView>(Resource.Id.textView1).Text = items[position].ProgressionIDs;
view.FindViewById<TextView>(Resource.Id.textView2).Text = items[position].ProgressionNames;
return view;
这里的代码与SimpleListItem1
相关。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_
android:layout_>
<TextView
android:layout_
android:layout_
android:id="@+id/textView1"
android:text="test1"
android:layout_marginRight="@dimen/abc_action_bar_content_inset_material"
/>
<TextView
android:layout_
android:layout_
android:id="@+id/textView2"
android:text="test2"
/>
</LinearLayout>
这里正在运行 GIF。
【讨论】:
我试试这个,有没有办法让元素变大?我正在使用对话框片段来显示列表视图,我向用户显示的只是名称,我使用的是 ID,所以当我点击某些东西时,它会使用与名称相关的 ID 从服务器中获取其他数据 如果要让元素变大,请在SimpleListItem1中设置android:textSize="30dp"
为TextView
好的,现在我的程序根本不会加载对话框片段,然后它会说应用程序在等待后没有响应。
没关系,我设法解决了!原来我在 OnCreateView 中做的太多了,所以我不得不在前一个片段中进行数据库调用并传递值,然后它实际上开始像你的 gif 那样加载。以上是关于如何将单独的参数传递给 Xamarin Android C# 中的 ListView.ItemClick 调用的主要内容,如果未能解决你的问题,请参考以下文章
如何将令牌存储作为参数传递给 Symfony 3.4 中的事件监听器