我有一个ListView
,我有一个自定义ListItem,它有一些TextView
和一个ImageView
。我想在点击时更改该特定图像。我试过但是当我点击ListView
中的图像时,下面的listItem图像正在改变。
例如,如果我点击第0个位置图像,那么图像正在改变第1个位置ListIem,当我向上和向下滚动列表时,它会随机变化。
我不知道它发生了什么,我在我的适配器上使用了notifydatasetChanged,但是它无效,我的代码如下。
请帮助我,谢谢,
代码
private class RssAdapter extends ArrayAdapter<RSSFeed_SelectedHotelResult> {
private List<RSSFeed_SelectedHotelResult> rssFeedLst;
int selectedPosition;
public RssAdapter(Context context, int textViewResourceId,
List<RSSFeed_SelectedHotelResult> rssFeedLst) {
super(context, textViewResourceId, rssFeedLst);
this.rssFeedLst = rssFeedLst;
Boolean addtoShotlist;
}
public View getView(final int position, View convertView,
ViewGroup parent) {
View view = convertView;
if (convertView == null) {
view = View.inflate(HotelListActivity.this, R.layout.list_row,
null);
rssHolder = new RssHolder();
rssHolder.iv_add = (ImageView) view.findViewById(R.id.iv_add);
rssHolder.rssTitleView = (TextView) view
.findViewById(R.id.title);
rssHolder.tv_offer = (TextView) view.findViewById(R.id.tv_ofr);
rssHolder.rssImagHotel = (ImageView) view
.findViewById(R.id.hotelImage);
rssHolder.rssImageHotelRate = (ImageView) view
.findViewById(R.id.rateHotel2);
rssHolder.rssHotelPrice = (TextView) view
.findViewById(R.id.textHotelRate);
rssHolder.rssHotelAddress = (TextView) view
.findViewById(R.id.textHotelDesc);
// rssHolder.adres = (TextView) view.findViewById(R.id.adres);
// rssHolder.rssHotelRating = (TextView)
// view.findViewById(R.id.textHotelRating);
rssHolder.rating_hotel = (RatingBar) view
.findViewById(R.id.rateHotelImage);
rssHolder.tv_currcode = (TextView) view
.findViewById(R.id.tv_currcode);
view.setTag(rssHolder);
} else {
rssHolder = (RssHolder) view.getTag();
}
final RSSFeed_SelectedHotelResult rssFeed = rssFeedLst
.get(position);
rssHolder.rssTitleView.setText(rssFeed.getName());
imageLoader.DisplayImage(rssFeed.getHotel_image(),
rssHolder.rssImagHotel);
imageLoader.DisplayImage_rating(rssFeed.getHote_rate_image(),
rssHolder.rssImageHotelRate);
rssHolder.rssHotelPrice.setText(rssFeed.getHotel_price());
rssHolder.rssHotelAddress.setText(rssFeed.getHotel_desc());
rssHolder.rating_hotel.setRating(Float.valueOf(rssFeed
.getHotel_rate()));
rssHolder.tv_currcode.setText(Consts.currencyCode);
if (rssFeed.getoffer() != null) {
rssHolder.tv_offer.setText("**" + rssFeed.getoffer() + "**");
} else {
rssHolder.tv_offer.setText("");
}
rssHolder.iv_add.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
selectedPosition = position;
try {
if (position == selectedPosition) {
rssHolder.iv_add
.setBackgroundResource(R.drawable.fill);
} else {
rssHolder.iv_add
.setBackgroundResource(R.drawable.plus12);
}
} catch (IndexOutOfBoundsException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out
.println("::::::::::::My data in side hotel List activity:::::::::;"
+ position
+ ""
+ rssFeed.getName()
+ "\n"
+ rssFeed.getHotel_price());
/*
* RSSFeed_SelectedHotelResult rssFeed1 = rssFeedLst
* .get(position);
*/
}
});
return view;
}
}
答案 0 :(得分:1)
对于通知数据集,更改了您调用错误方法的正确方法是adapter.notifyDataSetChanged()
答案 1 :(得分:0)
1。)将此代码写入自定义适配器getView:
if (position == selectedPosition) {
imageview.setBackgroundResource(R.drawable.image1);
} else {
imageview.setBackgroundResource(R.drawable.normal);
}
2.。)在自定义适配器中创建一个方法:
public void setSelected(int position) {
selectedPosition = position;
}
//where selectPosition is private int selectedPosition = -1;
3.。)从活动列表项中调用此方法,点击:
((Category_Adapter) adapter).setSelected(position);
listview.invalidate();
答案 2 :(得分:0)
private class RssAdapter extends ArrayAdapter<RSSFeed_SelectedHotelResult> {
private List<RSSFeed_SelectedHotelResult> rssFeedLst;
private int selectedPosition =-1;// initalize position
public RssAdapter(Context context, int textViewResourceId,
List<RSSFeed_SelectedHotelResult> rssFeedLst) {
super(context, textViewResourceId, rssFeedLst);
this.rssFeedLst = rssFeedLst;
Boolean addtoShotlist;
}
//make this method
public void setSelected(int position) {
selectedPosition = position;
}
public View getView(final int position, View convertView,
ViewGroup parent) {
View view = convertView;
if (convertView == null) {
view = View.inflate(HotelListActivity.this, R.layout.list_row,
null);
rssHolder = new RssHolder();
rssHolder.iv_add = (ImageView) view.findViewById(R.id.iv_add);
rssHolder.rssTitleView = (TextView) view
.findViewById(R.id.title);
rssHolder.tv_offer = (TextView) view.findViewById(R.id.tv_ofr);
rssHolder.rssImagHotel = (ImageView) view
.findViewById(R.id.hotelImage);
rssHolder.rssImageHotelRate = (ImageView) view
.findViewById(R.id.rateHotel2);
rssHolder.rssHotelPrice = (TextView) view
.findViewById(R.id.textHotelRate);
rssHolder.rssHotelAddress = (TextView) view
.findViewById(R.id.textHotelDesc);
// rssHolder.adres = (TextView) view.findViewById(R.id.adres);
// rssHolder.rssHotelRating = (TextView)
// view.findViewById(R.id.textHotelRating);
rssHolder.rating_hotel = (RatingBar) view
.findViewById(R.id.rateHotelImage);
rssHolder.tv_currcode = (TextView) view
.findViewById(R.id.tv_currcode);
view.setTag(rssHolder);
} else {
rssHolder = (RssHolder) view.getTag();
}
final RSSFeed_SelectedHotelResult rssFeed = rssFeedLst
.get(position);
rssHolder.rssTitleView.setText(rssFeed.getName());
imageLoader.DisplayImage(rssFeed.getHotel_image(),
rssHolder.rssImagHotel);
imageLoader.DisplayImage_rating(rssFeed.getHote_rate_image(),
rssHolder.rssImageHotelRate);
rssHolder.rssHotelPrice.setText(rssFeed.getHotel_price());
rssHolder.rssHotelAddress.setText(rssFeed.getHotel_desc());
rssHolder.rating_hotel.setRating(Float.valueOf(rssFeed
.getHotel_rate()));
rssHolder.tv_currcode.setText(Consts.currencyCode);
if (rssFeed.getoffer() != null) {
rssHolder.tv_offer.setText("**" + rssFeed.getoffer() + "**");
} else {
rssHolder.tv_offer.setText("");
}
rssHolder.iv_add.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
//remove selectposition = position
try {
if (position == selectedPosition) {
rssHolder.iv_add
.setBackgroundResource(R.drawable.fill);
} else {
rssHolder.iv_add
.setBackgroundResource(R.drawable.plus12);
}
} catch (IndexOutOfBoundsException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out
.println("::::::::::::My data in side hotel List activity:::::::::;"
+ position
+ ""
+ rssFeed.getName()
+ "\n"
+ rssFeed.getHotel_price());
/*
* RSSFeed_SelectedHotelResult rssFeed1 = rssFeedLst
* .get(position);
*/
}
});
return view;
}
}
最后从活动列表项中调用适配器选择的方法单击并设置位置。