我正在尝试单击自定义列表视图的图像视图,并希望在 onClick 事件上更改其图像。我可以单击它成功执行我的其他操作,但图像未反映在 imageview 中。
这是供参考的代码。
public class ReminderAdapter extends BaseAdapter {
private Activity activity;
private static LayoutInflater inflater=null;
ImageView firstStar,secondStar,thirdStar,fourthStar,fifthStar;
public ReminderAdapter(Activity a, ArrayList<HashMap<String, String>> d) {
activity = a;
data=d;
inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
faceBold = Typeface.createFromAsset(activity.getAssets(),
"fonts/eau_sans_bold.otf");
faceNormal = Typeface.createFromAsset(activity.getAssets(),
"fonts/eau_sans_book.otf");
}
public int getCount() {
return data.size();
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
View vi=convertView;
if(activity instanceof SearchDishoom)
{
if(convertView==null)
vi = inflater.inflate(R.layout.remind_dish_list_row, null);
firstStar = (ImageView)vi.findViewById(R.id.hotelListFirstStar);
secondStar = (ImageView)vi.findViewById(R.id.hotelListSecondStar);
thirdStar = (ImageView)vi.findViewById(R.id.hotelListThirdStar);
fourthStar = (ImageView)vi.findViewById(R.id.hotelListFourthStar);
fifthStar = (ImageView)vi.findViewById(R.id.hotelListFifthStar);
firstStar.setFocusable(false);
secondStar.setFocusable(false);
thirdStar.setFocusable(false);
fourthStar.setFocusable(false);
fifthStar.setFocusable(false);
firstStar.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
setStar(1);
}
});
secondStar.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
//ReminderList.clear();
setStar(2);
}
});
thirdStar.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
setStar(3);
}
});
fourthStar.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
setStar(4);
}
});
fifthStar.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
setStar(5);
}
});
}
return vi;
}
public void setStar(int btnNumber) {
if (btnNumber == 1) {
firstStar.setImageResource(R.drawable.rated);
secondStar.setImageResource(R.drawable.unrated);
thirdStar.setImageResource(R.drawable.unrated);
fourthStar.setImageResource(R.drawable.unrated);
fifthStar.setImageResource(R.drawable.unrated);
//postRating(1);
}
if (btnNumber == 2) {
firstStar.setImageResource(R.drawable.rated);
secondStar.setImageResource(R.drawable.rated);
thirdStar.setImageResource(R.drawable.unrated);
fourthStar.setImageResource(R.drawable.unrated);
fifthStar.setImageResource(R.drawable.unrated);
//postRating(2);
}
if (btnNumber == 3) {
firstStar.setImageResource(R.drawable.rated);
secondStar.setImageResource(R.drawable.rated);
thirdStar.setImageResource(R.drawable.rated);
fourthStar.setImageResource(R.drawable.unrated);
fifthStar.setImageResource(R.drawable.unrated);
//postRating(3);
}
if (btnNumber == 4) {
firstStar.setImageResource(R.drawable.rated);
secondStar.setImageResource(R.drawable.rated);
thirdStar.setImageResource(R.drawable.rated);
fourthStar.setImageResource(R.drawable.rated);
fifthStar.setImageResource(R.drawable.unrated);
//postRating(4);
}
if (btnNumber == 5) {
firstStar.setImageResource(R.drawable.rated);
secondStar.setImageResource(R.drawable.rated);
thirdStar.setImageResource(R.drawable.rated);
fourthStar.setImageResource(R.drawable.rated);
fifthStar.setImageResource(R.drawable.rated);
//postRating(5);
}
}
我不明白发生了什么。请给我任何参考或提示。
提前致谢。