通过在 Adapter 的 getView() 中单击 imageView 更改图像
Posted
技术标签:
【中文标题】通过在 Adapter 的 getView() 中单击 imageView 更改图像【英文标题】:Change Image by clicking imageView in getView() of Adapter 【发布时间】:2014-07-29 21:02:08 【问题描述】:目标:我想要的是,当我在列表项中单击图像时,它会在
-
数据库
TextView(随着点赞数的增加)
更改图像本身以表示列表项被喜欢。
错误:但每当我点击图片时,它会在数据库中进行更改并增加相应 TextView 的数量。
But it change images of other list items as well than of just clicked item.
请帮助我使用以下代码:
public class PopularTabRowAdapter extends BaseAdapter
List<APIDataModel> videoList;
private Activity activity;
private static LayoutInflater inflater = null;
ImageLoader imageloader;
PopularDataSource datasource;
int pos;
ViewHolder holder;
ListView mListView;
public PopularTabRowAdapter(Activity activity,
List<APIDataModel> videoList, ListView list)
this.activity = activity;
this.videoList = videoList;
inflater = (LayoutInflater) this.activity
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
imageloader = new ImageLoader(activity);
mListView = list;
this.datasource = new PopularDataSource(activity);
@Override
public int getCount()
// TODO Auto-generated method stub
return videoList.size();
@Override
public Object getItem(int arg0)
// TODO Auto-generated method stub
return videoList.get(arg0);
@Override
public long getItemId(int arg0)
// TODO Auto-generated method stub
return videoList.get(arg0).getID();
@Override
public View getView(int position, View convertView, ViewGroup parent)
// TODO Auto-generated method stub
View row = convertView;
holder = null;
pos = position;
if (row == null)
row = inflater.inflate(R.layout.popular_tab_custum_row, parent,
false);
holder = new ViewHolder();
holder.descriptiontxt = (TextView) row
.findViewById(R.id.decription_popCustomRow);
holder.listtxt = (TextView) row
.findViewById(R.id.list_popCustomRow);
holder.imageView = (ImageView) row
.findViewById(R.id.image_popCustomRow);
holder.likeImage = (ImageView) row
.findViewById(R.id.likimage_popCustRow);
holder.likeImage.setOnClickListener(likeImageListner);
holder.numbLike = (TextView) row
.findViewById(R.id.noLik_popCustRow);
row.setTag(holder);
else
holder = (ViewHolder) row.getTag();
holder.descriptiontxt.setText(videoList.get(position).getDecription());
holder.listtxt.setText(videoList.get(position).getList());
imageloader.DisplayImage(ApplicationMain
.getYouTubeVideoImageURL(videoList.get(position).getURL()),
holder.imageView);
holder.numbLike.setText(String.valueOf(videoList.get(position)
.getNumberOfLikes()));
if (videoList.get(position).getLikeStatus() == 1)
holder.likeImage.setImageResource(R.drawable.like_done);
return row;
OnClickListener likeImageListner = new OnClickListener()
@Override
public void onClick(View v)
// TODO Auto-generated method stub
ViewHolder vh = getViewHolder(v);
int positionVh = mListView.getPositionForView((View) v.getParent());
if (isNetworkAvailable())
Toast.makeText(activity, positionVh + "", Toast.LENGTH_LONG)
.show();
if (videoList.get(positionVh).getLikeStatus() == 0)
HitLike like = new HitLike(positionVh, vh.likeImage,
vh.numbLike);
like.execute();
;
public ViewHolder getViewHolder(View v)
if (v.getTag() == null)
return getViewHolder((View) v.getParent());
return (ViewHolder) v.getTag();
class HitLike extends AsyncTask<Void, Void, String>
ImageView image;
int position;
TextView text;
public HitLike(int position, ImageView image, TextView text)
this.position = position;
this.image = image;
this.text = text;
@Override
protected void onPreExecute()
// TODO Auto-generated method stub
super.onPreExecute();
activity.setProgressBarIndeterminateVisibility(true);
@Override
protected String doInBackground(Void... params)
// TODO Auto-generated method stub
ServiceHandler sh = new ServiceHandler();
String responce = sh.makeServiceCall(
ApplicationMain.Instance.getPOPLikeAPI(),
ServiceHandler.POST);
Log.i("PopularRowAdapterResponce", responce);
return responce;
@Override
protected void onPostExecute(String result)
// TODO Auto-generated method stub
super.onPostExecute(result);
activity.setProgressBarIndeterminateVisibility(false);
datasource.open();
if (datasource.addLike(videoList.get(position).getID(), videoList
.get(position).getNumberOfLikes()))
APIDataModel model = new APIDataModel(videoList.get(position)
.getList(), videoList.get(position).getDecription(),
videoList.get(position).getURL());
model.setNumberOfLikes(videoList.get(position)
.getNumberOfLikes() + 1);
model.setLikeStatus(1);
videoList.set(position, model);
notifyDataSetChanged();
image.setImageResource(R.drawable.like_done);
datasource.close();
private boolean isNetworkAvailable()
ConnectivityManager connectivityManager = (ConnectivityManager) activity
.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo activeNetworkInfo = connectivityManager
.getActiveNetworkInfo();
return activeNetworkInfo != null && activeNetworkInfo.isConnected();
static class ViewHolder
TextView descriptiontxt;
TextView listtxt;
ImageView imageView;
ImageView likeImage;
TextView numbLike;
谢谢:)
【问题讨论】:
【参考方案1】:image.setImageResource(R.drawable.like_done);
并设置特定位置的图像更改,因为它在getview中并且使用了viewholder。所以你必须
1) 为所选图像创建临时数组,例如位置。
2) 如果 getview position==temp[i] then 则在 getview 中放入 for 循环并达到临时大小
image.setImageResource(R.drawable.like_done);
否则
image.setImageResource(R.drawable.default);
就是这样……
【讨论】:
以上是关于通过在 Adapter 的 getView() 中单击 imageView 更改图像的主要内容,如果未能解决你的问题,请参考以下文章
1.0 listview的adapter中getview的三种写法
自定义 Adapter 的 getView() 中的空指针异常
[转]Android Adapter以及getView()方法的理解