【发布时间】:2012-01-15 04:54:48
【问题描述】:
public class ImageAndTextListAdapter extends ArrayAdapter<ImageAndText> {
// new method
private ListView listView;
private AsyncImageLoader asyncImageLoader;
private ImageAndText imageAndText;
//constructor
public ImageAndTextListAdapter(Activity activity, List<ImageAndText> imageAndTexts) {
super(activity, 0, imageAndTexts);
asyncImageLoader = new AsyncImageLoader();
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
Activity activity = (Activity) getContext();
////////////////////////////////////////////////////////////////////////////////////////////////
// Load the image and set it on the ImageView
//new method
// Inflate the views from XML
View rowView = convertView;
ViewCache viewCache;
if (rowView == null) {
LayoutInflater inflater = activity.getLayoutInflater();
rowView = inflater.inflate(R.layout.image_and_text_row, null);
viewCache = new ViewCache(rowView);
rowView.setTag(viewCache);
} else {
viewCache = (ViewCache) rowView.getTag();
}
imageAndText = getItem(position);
Button btn2=(Button) findViewById(R.id.button1);
btn2.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
//...........
}
});
这个适配器是给ListView的,它接受image_and_text_row.xml,代表listview的行数据。当我为 btn2 设置点击侦听器时,程序崩溃了。如果监听器被删除,那么程序运行正常。
问题是为什么适配器在代码中不能有按钮点击监听器?
【问题讨论】:
-
Jason,您绝对可以在适配器中安装点击监听器。您的问题应该在您的 logcat 中。甚至将其发布在这里也可能会有所帮助。
-
你遇到了什么异常?