如何将单独的参数传递给 Xamarin Android C# 中的 ListView.ItemClick 调用
How to pass a separate argument through to a ListView.ItemClick call in Xamarin Android C#
所以我试图将参数传递给 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
上单击的项目的位置,然后在该位置 select ProgressionIDs
并将其存储在变量。
能给我一个数据结构吗?一个 ProgressionName 对应一个 ProgressionID?
如果是这样,您可以为 Progression
创建一个模型,其中包含两个属性,例如以下数据结构。
public class Progression
{
public string ProgressionIDs { get; set; }
public string ProgressionNames { get; set; }
}
我使用静态数据来填充List<Progression>
。当你点击列表视图中的项目时,你可以获取位置,然后记录到位置以获取 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_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/textView1"
android:text="test1"
android:layout_marginRight="@dimen/abc_action_bar_content_inset_material"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/textView2"
android:text="test2"
/>
</LinearLayout>
这是运行 GIF。
所以我试图将参数传递给 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
上单击的项目的位置,然后在该位置 select ProgressionIDs
并将其存储在变量。
能给我一个数据结构吗?一个 ProgressionName 对应一个 ProgressionID?
如果是这样,您可以为 Progression
创建一个模型,其中包含两个属性,例如以下数据结构。
public class Progression
{
public string ProgressionIDs { get; set; }
public string ProgressionNames { get; set; }
}
我使用静态数据来填充List<Progression>
。当你点击列表视图中的项目时,你可以获取位置,然后记录到位置以获取 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_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/textView1"
android:text="test1"
android:layout_marginRight="@dimen/abc_action_bar_content_inset_material"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/textView2"
android:text="test2"
/>
</LinearLayout>
这是运行 GIF。