所以我试图通过一个参数传递给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
并将其存储在变量中。
您能给我一个数据结构吗?一个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。