到目前为止,我有一个带有自定义适配器的列表视图,列表中的每个项目都有一个按钮。 我真的很困惑;我试着做以下事情:
当用户点击列表中项目的按钮(删除按钮)时,我想知道点击了哪个项目按钮,这样我就可以知道要删除哪个项目 - 我该如何实现?我见过有关设置标签的事情,但我仍然非常迷失。
我还尝试从主要活动的列表布局中找到按钮,但无法引用它。
请您详细说明如何做我想做的事。谢谢。
ADDED适配器代码:
MultiBinding
}
答案 0 :(得分:2)
为OnClickListener
按钮实施delete
。
点击delete
按钮后,在position
删除数据源中的行,然后调用notifyDataSetChanged()
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
Holder holder=new Holder();
View rowView;
rowView = inflater.inflate(R.layout.rowlayout2, null);
holder.name =(TextView) rowView.findViewById(R.id.EditTextName);
holder.address =(TextView) rowView.findViewById(R.id.EditTextAddress);
holder.ib = (ImageButton) rowView.findViewById(R.id.Delete);
holder.name.setText(n[position]);
holder.address.setText(a[position]);
holder.ib.setTag(convertView);
//Add this for on-click of delete button
holder.ib.setOnClickListener(new OnClickListener(){
//Delete the row in your data source specified at position
});
rowView.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Toast.makeText(context, "You Clicked "+n[position], Toast.LENGTH_LONG).show();
}
});
return rowView;
}
答案 1 :(得分:1)
在适配器的getView()方法中使用它。
yourButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
System.out.println("button : "+position+ " clicked");
// Do you code here.
}
});
答案 2 :(得分:0)
您需要在onClick
中添加getView
。
示例:
http://jmsliu.com/2444/click-button-in-listview-and-get-item-position.html
这里有很多教程。
答案 3 :(得分:0)
public class SeekBarAdapter {private SeekBarListener mListener;
private ClickListener mListenerClick;
public interface SeekBarListener {
public void onProgressChanged(SeekBar seekBar, int progress,
boolean fromUser, int positionInList);
public void onStartTrackingTouch(SeekBar seekBar, int positionInList);
public void onStopTrackingTouch(SeekBar seekBar, int positionInList);
}
public interface ClickListener {
public void onClick(View v, int positionInList);
}
public listAdapter getAdapter(Context context, ItemPhotoEffect itemPhotoEff) {
return new listAdapter(context, itemPhotoEff);
}
public void setSeekBarListener(SeekBarListener listener) {
mListener = listener;
}
public void setClickListener(ClickListener listener) {
mListenerClick = listener;
}
public class listAdapter extends BaseAdapter {
private LayoutInflater mInflater;
private onSeekbarChange mSeekListener;
private onClickChange mImgClickListener;
private ArrayList<Bitmap> itemsList;
private Bitmap imgIcon;
private ArrayList<Integer> positionProgress;
private ArrayList<ColorEffectItem> colorItem;
public listAdapter(Context context, ItemPhotoEffect itemPhotoEff) {
mInflater = LayoutInflater.from(context);
if (mSeekListener == null) {
mSeekListener = new onSeekbarChange();
}
if (mImgClickListener == null) {
mImgClickListener = new onClickChange();
}
this.itemsList = itemPhotoEff.getSeekBarItem().getIconSeekBar();
this.positionProgress = itemPhotoEff.getSeekBarItem()
.getPositionProgress();
// if (itemPhotoEff.getSeekBarItem().getColorItem() != null) {
this.colorItem = itemPhotoEff.getSeekBarItem().getColorItem();
// }
}
@Override
public int getCount() {
if (itemsList != null) {
return itemsList.size();
} else {
return 0;
}
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return null;
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
final ViewHolder holder;
// if (convertView == null) {
holder = new ViewHolder();
convertView = mInflater.inflate(R.layout.seekbar_adapter_layout,
null);
holder.img = (ImageView) convertView.findViewById(R.id.imageView1);
holder.seekbar = (SeekBar) convertView.findViewById(R.id.seekBar1);
holder.colorbutton = (ImageView) convertView
.findViewById(R.id.colorImage);
holder.ln_img = (LinearLayout) convertView
.findViewById(R.id.ln_colorimg);
convertView.setTag(holder);
// } else {
// holder = (ViewHolder) convertView.getTag();
// }
holder.img.setImageBitmap(itemsList.get(position));
if (positionProgress.get(position) != null) {
holder.seekbar.setVisibility(View.VISIBLE);
holder.ln_img.setVisibility(View.GONE);
holder.seekbar.setProgress(positionProgress.get(position));
} else {
holder.ln_img.setVisibility(View.VISIBLE);
holder.seekbar.setVisibility(View.GONE);
holder.colorbutton.setBackgroundColor(Color.rgb(this.colorItem
.get(position).getRcolor(), this.colorItem
.get(position).getGcolor(), this.colorItem
.get(position).getBcolor()));
}
holder.seekbar.setOnSeekBarChangeListener(mSeekListener);
holder.ln_img.setOnClickListener(mImgClickListener);
holder.seekbar.setTag(position);
holder.ln_img.setTag(position);
return convertView;
}
}
static class ViewHolder {
ImageView img;
SeekBar seekbar;
ImageView colorbutton;
LinearLayout ln_img;
}// This is class you need to know which button has been click
public class onClickChange implements OnClickListener {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
int position = (Integer) v.getTag();
if (mListenerClick != null) {
mListenerClick.onClick(v, position);
}
}
}
public class onSeekbarChange implements OnSeekBarChangeListener {
@Override
public void onProgressChanged(SeekBar seekBar, int progress,
boolean fromUser) {
int position = (Integer) seekBar.getTag();
if (mListener != null) {
mListener.onProgressChanged(seekBar, progress, fromUser,
position);
}
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
int position = (Integer) seekBar.getTag();
if (mListener != null) {
mListener.onStartTrackingTouch(seekBar, position);
}
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
int position = (Integer) seekBar.getTag();
if (mListener != null) {
mListener.onStopTrackingTouch(seekBar, position);
}
}
}}
您可以使用mycode。在适配器中实现接口OnclickListener。
然后在活动中实施:
如果行中有1个按钮,则使用+ PositionInList。
adapter.setClickListener(new ClickListener() {
@Override
public void onClick(View v, int positionInList) {
// TODO Auto-generated method stub
Log.i("", "Click on position in list" + positionInList);
}
});
答案 4 :(得分:0)
你不应该使用Array。数组具有固定长度,因此您无法完全删除其元素。 您需要使用ArrayList代替Array并正确实现ViewHolder模式:
ArrayList<Item> items = new ArrayList<>();
//merge both the content of a and n into one object "Item"
//and use ArrayList
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
Holder holder;
if (convertView == null) {
convertView = LayoutInflater.from(getContext()).inflate(R.layout.rowlayout2, parent, false);
holder = new Holder(convertView);
convertView.setTag(holder);
} else {
holder = (Holder) convertView.getTag();
}
holder.name.setText(getItem(position).name);
...
holder.root.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
items.remove(position);
notifyDataSetChanged();
}
});
return convertView;
}
如果要在整行上设置clickable,则需要将根元素放在ViewHolder中。
class Holder{
TextView name, address, ...;
LinearLayout root; // or any Layout which is the root container of your row
}