我正在将ListView与自定义适配器一起使用,我想添加全部具有自己的删除按钮的行。我已经设法添加了行,但是当我按下删除按钮时,无论我按下什么删除按钮,它总是删除列表中的最后一个条目。
我注意到我不是第一个出现类似问题的人,但是我无法弄清楚代码中的错误。
这是我的主要活动:
ListView mListView = FindViewById<ListView>(Resource.Id.listView1);
ListViewAdapter myAdapter = new ListViewAdapter(this);
Button btnAdd = FindViewById<Button>(Resource.Id.btnAdd);
btnAdd.Click += (s,e) =>
{
myAdapter.AddExercise();
if(mListView.Adapter == null)
{
mListView.Adapter = myAdapter;
}
};
这是我的自定义适配器中的代码:
class ListViewAdapter : BaseAdapter<Exercise>
{
private Activity mContext;
private List<Exercise> mItems = new List<Exercise>();
public void AddExercise()
{
mItems.Add(new Exercise());
NotifyDataSetChanged();
}
public ListViewAdapter (Activity context)
{
mContext = context;
}
public override Exercise this[int position]
{
get { return mItems[position]; }
}
public override int Count
{
get { return mItems.Count; }
}
public override Java.Lang.Object GetItem(int position)
{
return null;
}
public override long GetItemId(int position)
{
return position;
}
public override View GetView(int position, View convertView, ViewGroup parent)
{
MyViewHolder holder = null;
View row = convertView;
if (row != null)
{
holder = row.Tag as MyViewHolder;
}
if (holder == null)
{
row = mContext.LayoutInflater.Inflate(Resource.Layout.row, null);
holder = new MyViewHolder();
// setting the spinners
holder.exercise = row.FindViewById<Spinner>(Resource.Id.spinnerExercise);
holder.sets = row.FindViewById<Spinner>(Resource.Id.spinnerSets);
var adapterExercise = ArrayAdapter.CreateFromResource(row.Context, Resource.Array.exercises_array, Android.Resource.Layout.SimpleSpinnerItem);
adapterExercise.SetDropDownViewResource(Android.Resource.Layout.SimpleSpinnerDropDownItem);
holder.exercise.Adapter = adapterExercise;
var adapterSets = ArrayAdapter.CreateFromResource(row.Context, Resource.Array.sets_array, Android.Resource.Layout.SimpleSpinnerItem);
adapterSets.SetDropDownViewResource(Android.Resource.Layout.SimpleSpinnerDropDownItem);
holder.sets.Adapter = adapterSets;
// setting other elements
holder.reps = row.FindViewById<EditText>(Resource.Id.textReps);
holder.kgs = row.FindViewById<EditText>(Resource.Id.textKgs);
// setting button
holder.removeButton = row.FindViewById<Button>(Resource.Id.remove);
holder.removeButton.Click += (object sender, EventArgs e) =>
{
int pos = (int)(((Button)sender).GetTag(Resource.Id.remove));
mItems.RemoveAt(pos);
NotifyDataSetChanged();
Toast.MakeText(mContext, "deleted" + pos.ToString(), ToastLength.Long).Show();
};
row.Tag = holder;
}
holder.removeButton.SetTag(Resource.Id.remove, position);
return row;
}
}
class MyViewHolder : Java.Lang.Object
{
public Spinner exercise { get; set; }
public Button removeButton { get; set; }
public Spinner sets { get; set; }
public EditText reps { get; set; }
public EditText kgs { get; set; }
}
}
答案 0 :(得分:0)
使用ArrayAdapter中的remove()方法从列表中删除所需的项目。
'Insert Row Fields
With ActiveSheet.PivotTables("Rev by Facility")
With .PivotFields("Facility")
.Orientation = xlRowField
.Position = 1
End With
'Insert Column Fields
With .PivotFields("Revenue")
.Orientation = xlColumnField
.Position = 1
End With
'Insert Data Field
With .PivotFields("SubTotal 2")
.Orientation = xlValuesField
.Position = 1
.Function = xlSum
.Name = "Sum of SubTotal 2"
End With
End With
另一种方法是修改ArrayList并在ArrayAdapter上调用notifyDataSetChanged()。
findElement(By.name("username")).sendKeys(${username}) <- here I want to pass JMeter variable
WDS.sampleResult.sampleStart()
WDS.browser.get('https://www.google.com/')
WDS.browser.findElement(org.openqa.selenium.By.name("q")).
sendKeys(**${here I want to put JMeter variable}**)
WDS.sampleResult.sampleEnd()
答案 1 :(得分:0)
根据您的描述,在ListView中添加按钮,现在,您想单击Button从ListView中删除项目,我做一个示例,您可以看一下:
Layout9,显示ListView:
<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:text="Button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minWidth="25px"
android:minHeight="25px"
android:id="@+id/button1" />
<ListView
android:minWidth="25px"
android:minHeight="25px"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/listView1" />
Layout10,ListView模板:
<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:text="Name"
android:textAppearance="?android:attr/textAppearanceLarge"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:minWidth="25px"
android:minHeight="25px"
android:id="@+id/textView1" />
<TextView
android:text="Age"
android:textAppearance="?android:attr/textAppearanceLarge"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:minWidth="25px"
android:minHeight="25px"
android:id="@+id/textView2" />
<Button
android:text="Delete Button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/button1" />
CustomAdapter:
public class Customadapter : BaseAdapter<Person>
{
public Activity activity;
public ObservableCollection<Person> persons;
public Customadapter(Activity activity, ObservableCollection<Person>persons):base()
{
this.activity = activity;
this.persons = persons;
}
public override Person this[int position]
{
get
{
return persons[position];
}
}
public override int Count
{
get
{
return persons.Count();
}
}
public override Java.Lang.Object GetItem(int position)
{
return null;
}
public override long GetItemId(int position)
{
return persons[position].Id;
}
public override View GetView(int position, View convertView, ViewGroup parent)
{
Person item = persons[position];
var view = convertView ?? activity.LayoutInflater.Inflate(Resource.Layout.layout10, parent, false);
var txtName = view.FindViewById<TextView>(Resource.Id.textView1);
var txtAge = view.FindViewById<TextView>(Resource.Id.textView2);
var button = view.FindViewById<Button>(Resource.Id.button1);
txtName.Text = persons[position].Name;
txtAge.Text = " " + persons[position].Age;
if(!button.HasOnClickListeners)
{
button.Click += (object sender, EventArgs e) =>
{
int index = (int)(((Button)sender).GetTag(Resource.Id.button1));
persons.RemoveAt(index);
NotifyDataSetChanged();
};
}
button.SetTag(Resource.Id.button1,position);
return view;
}
}
MainActivity:
private Button button1;
ObservableCollection<Person> list;
private ListView listview;
private void Button1_Click(object sender, EventArgs e)
{
list = new ObservableCollection<Person>();
for(int i=0;i<20;i++)
{
Person p = new Person()
{
Name = "cherry" + i,
Age = 20 + i
};
list.Add(p);
}
listview = FindViewById<ListView>(Resource.Id.listView1);
var adapter = new Customadapter(this,list);
listview.Adapter = adapter;
}
人员班:
public class Person
{
public int Id { get; set; }
public string Name { get; set; }
public int Age { get; set; }
public Button btn { get; set; }
}