这是我的ArrayAdapter:
public class ListArrayAdapter extends ArrayAdapter<String> {
public ArrayList<String> links = new ArrayList<String>();
public Activity activity = new Activity();
TextView linkText;
TextView linkDesc;
public Context c;
public ListArrayAdapter(Context context, int textViewResourceId,
ArrayList<String> fiokiList, Object o) {
super(context, textViewResourceId, fiokiList);
this.links = fiokiList;
activity = (Activity) (o);
c = context;
//notifyDataSetChanged();
}
public View getView(final int position, View convertView, ViewGroup parent) {
View v = convertView;
if (convertView == null) {
convertView = LayoutInflater.from(getContext()).inflate(
R.layout.list_item, parent, false);
convertView.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Toast.makeText(activity, String.valueOf(position), Toast.LENGTH_LONG).show();
}
});
}
final String link = this.links.get(position);
linkText = (TextView) convertView.findViewById(R.id.linkTitle0001);
// linkImageView = (SmartImageView) convertView.findViewById(R.id.faviconView0001);
linkText.setText(link);
//ImageView imgView = (ImageView)convertView.findViewById(R.id.imageView1);
// PieMenu pieMenu = new PieMenu(imgView, activity);
//fl.addView(pieMenu);
return convertView;
}
}
我理解如何为listView的整个项目编写onClickListener?我知道如何为每个项目编写一个,但我不知道如何为视图做到这一点?
答案 0 :(得分:3)
删除您在适配器中执行的操作,并将此代码添加到您调用adatper的主活动中
setOnItemClickListener
是你应该打电话的功能。
list_view.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0,View arg1, int position, long arg3) {
// TODO Auto-generated method stub
}
});
答案 1 :(得分:0)
我理解的是,如果我没有错,你想为每个Single ListView行中的视图设置onClickListener()。如果这是真的,那么您只需要在getView()方法中设置OnCliCkListener()
。
示例:您可以在
中找到自己的观点if (convertView == null) {
convertView = inflater.inflate(R.layout.single_row_home, null);
tv_title = (TextView) convertView
.findViewById(R.id.textViewSingleRowHomeTitle);
tv_time = (TextView) convertView
.findViewById(R.id.textViewSingleRowHomeTime);
iv_checkbox = (ImageView) convertView
.findViewById(R.id.imageViewSingleRowHomeCheckbox);
convertView.setTag(new TaskViewHolder(tv_title, tv_time,
iv_checkbox));
} else {
TaskViewHolder viewHolder = (TaskViewHolder) convertView
.getTag();
tv_title = viewHolder.get_tv_task_title();
tv_time = viewHolder.get_tv_task_time();
iv_checkbox = viewHolder.get_iv_task_checkbox();
}
在此之后,您可以将OnClickListener()
设置如下:
tv_title.setClickable(true);
tv_title.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
}
});
如果你想在ListView行中查看视图的click事件,你需要在自定义适配器的getView()
方法中实现它,否则你的ListView的onItemClickListener只能在OnCreate()
内实现。
答案 2 :(得分:0)
点击listview对你的意思是什么?如果是,请使用此::
name of your listview.setOnItemClickListener(new AdapterView.OnItemClicklistener(){
@override
public void onItemClick(AdapterView<?> adapter,View view,int position,long id){
//event up to you
//my event is
Toast.makeText(Context,text,duration)
}
});