我有一个ListView
. ListView 的行中有一个ImageView
。我已将 onClick 属性附加到 ImageView 并且我的 onClick 函数是
public void alarmClick(View v)
{
ImageView bell = (ImageView)v.findViewById(R.id.mEventImage);
bell.setImageResource(R.drawable.bell_on);
if(bell.getTag() == null)
{
bell.setImageResource(R.drawable.bell_on);
bell.setTag(R.drawable.bell_on);
}
else if((Integer)bell.getTag() == R.drawable.bell_on)
{
bell.setImageResource(R.drawable.bell_off);
bell.setTag(R.drawable.bell_off);
}
else
{
bell.setImageResource(R.drawable.bell_on);
bell.setTag(R.drawable.bell_on);
}
}
我正在尝试切换ImageView
源,但是当我单击当时不在屏幕上的ImageView
许多其他行时,它们也会被切换。ImageView
这可能是因为视图在ListView
. 任何建议如何为每个独特的行做。
适配器类
package com.example.drawer;
import java.util.ArrayList;
import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;
public class EventAdapter extends ArrayAdapter<EventList> {
Context context;
ArrayList<EventList> data;
int layoutResourceId;
public EventAdapter(Context context, int textViewResourceId, ArrayList<EventList> data) {
super(context, textViewResourceId, data);
this.context = context;
this.data = data;
this.layoutResourceId = textViewResourceId;
// TODO Auto-generated constructor stub
}
@Override
public View getView(int position, View convertView, ViewGroup parent)
{
View row = convertView;
EventHolder holder = null;
if(row == null)
{
LayoutInflater inflator = ((Activity)context).getLayoutInflater();
row = inflator.inflate(layoutResourceId, parent, false);
holder = new EventHolder();
holder.coord = (TextView)row.findViewById(R.id.eventCoor);
holder.icon = (ImageView)row.findViewById(R.id.icon);
holder.eventName = (TextView)row.findViewById(R.id.eventNam);
holder.location = (TextView)row.findViewById(R.id.eventLoc);
holder.time = (TextView)row.findViewById(R.id.eventTime);
row.setTag(holder);
}
else
{
holder = (EventHolder)row.getTag();
}
EventList listItem = data.get(position);
ImageHelper imageHelper = new ImageHelper();
Bitmap icon = BitmapFactory.decodeResource(context.getResources(), R.drawable.ipl);
Bitmap newIcon = imageHelper.getRoundedCornerBitmap(icon, 200);
holder.coord.setText(listItem.coord);
holder.eventName.setText(listItem.eventName);
holder.location.setText(listItem.location);
holder.time.setText(listItem.time);
holder.icon.setImageBitmap(newIcon);
holder.id = listItem.id;
holder.day = listItem.day;
//Log.d("ListView", "Executed");
return row;
}
static class EventHolder
{
ImageView icon;
TextView eventName;
TextView location;
TextView time;
TextView coord;
int id;
int day;
}
}