通过单击适配器的getView()中的imageView来更改图像

时间:2014-06-09 10:27:06

标签: android android-listview android-imageview android-adapter

目标:我想要的是,当我点击列表项目中的图片时,它会进行更改

  1. 数据库
  2. TextView(喜欢的数量增加)
  3. 更改图像本身以表示列表项目。
  4. 错误:但每当我点击图片时,它会在数据库中进行更改并增加相应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 个答案:

答案 0 :(得分:1)

image.setImageResource(R.drawable.like_done);

并设置特定位置图像更改,因为它在getview中并且使用了视图。所以你必须

1)为所选图像创建临时数组,如位置。

2)如果getview位置= = temp [i]那么

将getview中的循环放到最大临时大小
   image.setImageResource(R.drawable.like_done);

其他

   image.setImageResource(R.drawable.default);

多数民众赞成......