好的,所以我在SO上搜索了一下,但似乎无法得到工作的答案。这个是关闭的,但它会删除列表中的最后一项,然后在删除最后一个值时崩溃。我只想让列表行内的按钮根据列表中的内容执行操作,然后从列表中删除该行。谢谢!
public class CitrusListAdapter extends ArrayAdapter<CitrusList>
{
//Globals
Context context;
int layoutResourceId;
ArrayList<CitrusList> data = null;
//Create the list item row
public CitrusListAdapter(Context context, int layoutResourceId, ArrayList<CitrusList> listItems)
{
super(context, layoutResourceId, listItems);
this.layoutResourceId = layoutResourceId;
this.context = context;
this.data = listItems;
}
//Expand and place items for List View onto calling page in ListView
@Override
public View getView(final int position, View convertView, ViewGroup parent)
{
View row = convertView;
CitrusListHolder holder = null;
if(row == null)
{
LayoutInflater inflater = ( (Activity) context).getLayoutInflater();
row = inflater.inflate(layoutResourceId, parent, false);
holder = new CitrusListHolder();
//holder.txtTitle = (TextView)row.findViewById(R.id.CitrusListTitle);
holder.colorSpinner = (Spinner)row.findViewById(R.id.color_spinner);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(getContext(),
R.array.colors_array, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
holder.colorSpinner.setAdapter(adapter);
holder.textureSpinner = (Spinner)row.findViewById(R.id.texture_spinner);
ArrayAdapter<CharSequence> texture_adapter = ArrayAdapter.createFromResource(getContext(),
R.array.texture_array, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
holder.textureSpinner.setAdapter(texture_adapter);
holder.soliditySpinner = (Spinner)row.findViewById(R.id.solidity_spinner);
ArrayAdapter<CharSequence> solidity_adapter = ArrayAdapter.createFromResource(getContext(),
R.array.solidity_array, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
holder.soliditySpinner.setAdapter(solidity_adapter);
Button rateButton = (Button) row.findViewById(R.id.rate_button);
rateButton.setTag(position);
rateButton.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v)
{
Integer index = (Integer) v.getTag();
data.remove(index.intValue());
notifyDataSetChanged();
}
});
row.setTag(holder);
}
else
{
holder = (CitrusListHolder)row.getTag();
}
CitrusList SingleLine = data.get(position);
//holder.txtTitle.setText(SingleLine.title);
return row;
}
//Stores the values of the items in the list view
static class CitrusListHolder
{
//TextView txtTitle;
Spinner colorSpinner;
Spinner textureSpinner;
Spinner soliditySpinner;
}
}
答案 0 :(得分:5)
您可以使用remove()
方法从数组列表中删除该项,然后调用适配器的notifyDataSetChanged()
方法。
答案 1 :(得分:2)
我认为你应该试试这个
rateButton.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v)
{
int index = Integer.parseInt(v.getTag().toString())
data.remove(index);
notifyDataSetChanged();
}
});
答案 2 :(得分:1)
要从ListView中删除行,您可以修改控制ListView的适配器所持有的数据。如果适配器是ArrayAdapter
,请在remove()
上调用ArrayAdapter
并为其指定您需要删除的项目的位置。然后使用notifyDataSetChanged()
通知适配器。适配器将刷新ListView,您的违规行将被删除。
在您的代码中,您不会将当前项目的位置存储在视图持有者中。将其更改为:
static class CitrusListHolder
{
//TextView txtTitle;
Spinner colorSpinner;
Spinner textureSpinner;
Spinner soliditySpinner;
int currentPosition;
}
然后在持有人中设置位置
Button rateButton = (Button) row.findViewById(R.id.rate_button);
rateButton.setTag(position); //delete this line
holder.currentPosition = position; //storing current position in holder
rateButton.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v)
{
CitrusListHolder currHolder = (CitrusListHolder) v.getParent().getTag(); //get button's parent which is the row
data.remove(currHolder.currentPosition); //get the stored position and remove the corresponding row
notifyDataSetChanged();
}
});
这应该有效。
答案 3 :(得分:0)
您没有为Button
设置实际位置。
将Button.setTag()
行从if
移到if-else
条件的外部,在该条件下设置文本和其他数据。所以在滚动或刷新之后,它应该在getView()
的每次调用中得到真实的位置,如下所示:
public View getView(final int position, View convertView, ViewGroup parent)
{
View row = convertView;
CitrusListHolder holder = null;
if(row == null)
{
//your implementation
}
else
{
//your implementation
}
CitrusList singleLine = data.get(position);
//holder.txtTitle.setText(singleLine.title);
rateButton.setTag(String.valueOf(position)); //<==== place it here, so it'll get the real/update position as tag, Convert it to String.
return row;
}
并在onClick()
中获取。将其转换为String
,然后将其转换为Integer
,如下所示:
int position = Integer.parseInt(v.getTag().toString());