listview收藏夹图标在滚动列表视图时更改

时间:2017-03-18 03:26:17

标签: java android listview

我有一个listView,其中有一个textview的名称和imageview的收藏按钮图标当我点击收藏的图标图标更改为填充从未填充,但当我滚动列表视图图标从填充更改为未填充这是我的代码< / p>

    public class AdapterFind extends BaseAdapter {

        ArrayList<BeanFindName> arrayListAd; /* arraylist with model class */
        Activity activity;
        DBFavourite dbfav;                   /* database class */

        private boolean[] favorites;           `ADDED`


        public AdapterFind(ArrayList<BeanFindName> arrayListAd, Activity           activity) {
            this.arrayListAd = arrayListAd;
            this.activity = activity;
            dbfav = new DBFavourite(activity);

            favorites = new boolean[this.arrayListAd.size()];    `ADDED`

        }

        @Override
        public int getCount() {
            return arrayListAd.size();
        }

        @Override
        public Object getItem(int position) {
            return arrayListAd.get(position);
        }

        @Override
        public long getItemId(int position) {
            return 0;
        }

        private class ViewHolder {

            TextView txtID;
            TextView txtName;
            ImageView imvFavourite;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {

            final ViewHolder holder;
            LayoutInflater inflater = activity.getLayoutInflater();

            if (convertView == null) {
                convertView = inflater.inflate(R.layout.activity_customized_listview, null);  /* layout file for listview row items */
                holder = new ViewHolder();

                holder.txtID = (TextView) convertView
                        .findViewById(R.id.customized_listview_tv_textId); /* textview id  */
                holder.txtName = (TextView) convertView
                        .findViewById(R.id.customized_listview_lv_names); /* texview for names */
                holder.imvFavourite = (ImageView) convertView
                       .findViewById(R.id.customized_listview_imv_favourite); /* imageview for favorite icons */

                convertView.setTag(holder);
            } else {
                holder = (ViewHolder) convertView.getTag();
            }

            holder.txtID.setText(String.valueOf(arrayListAd.get(position).getNameId())); /* model class functions */
            holder.txtName.setText(arrayListAd.get(position).getName());  /* model class functions */

            if (arrayListAd.get(position).getFinfavId() > 0 || favorites[position]) {
                holder.imvFavourite.setImageResource(R.mipmap.customized_listview_iv_favourites_boy_filled);

            } else if (arrayListAd.get(position).getFinfavId() == 0 || !favorites[position]) {
                holder.imvFavourite.setImageResource(R.mipmap.customized_listview_iv_favourites_boy_not_filled);
            }

            holder.imvFavourite.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {

`ADDED`

      if (favorites[position] || arrayListAd.get(position).getFinfavId() > 0) {  /* check for scrolling and database also to change favorite icon */
                        holder.imvFavourite.setImageResource(R.mipmap.customized_listview_iv_favourites_boy_not_filled);
                        dbfav.deleteByID(holder.txtID.getText().toString()); /* delete records from database*/
                        favorites[position] = false;
                    } else if (arrayListAd.get(position).getFinfavId() == 0 || !favorites[position]) {                          holder.imvFavourite.setImageResource(R.mipmap.customized_listview_iv_favourites_boy_filled);
         dbfav.insertDataInFavourite(holder.txtID.getText().toString()); /*inserts records to database */
                        favorites[position] = true; 
                    }
                 }               
            });
            return convertView;
        }
    }  

4 个答案:

答案 0 :(得分:0)

您需要做的是在onClick上更新您的ArrayList数据集中的fav图标ID。

arrayListAd.get(position).setFinfavId(dark or light) 
notifyDataSetInvalidated();

答案 1 :(得分:0)

您需要在listview项目本身中存储选择listview项目的信息,以便在重绘时,使用“selected”设置重绘它。为此,我们在ViewHolder类中添加一个字段来存储它。我在这里添加了代码:

public class AdapterFind extends BaseAdapter {

ArrayList<BeanFindName> arrayListAd; /* arraylist with model class */
Activity activity;
DBFavourite dbfav;                   /* database class */

public AdapterFind(ArrayList<BeanFindName> arrayListAd, Activity           activity) {
    this.arrayListAd = arrayListAd;
    this.activity = activity;
    dbfav = new DBFavourite(activity);

}

@Override
public int getCount() {
    return arrayListAd.size();
}

@Override
public Object getItem(int position) {
    return arrayListAd.get(position);
}

@Override
public long getItemId(int position) {
    return 0;
}

private class ViewHolder {

    TextView txtID;
    TextView txtName;
    ImageView imvFavourite;
    boolean isSelected;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {

    final ViewHolder holder;
    LayoutInflater inflater = activity.getLayoutInflater();

    if (convertView == null) {
        convertView = inflater.inflate(R.layout.activity_customized_listview, null);  /* layout file for listview row items */
        holder = new ViewHolder();

        holder.txtID = (TextView) convertView
                .findViewById(R.id.customized_listview_tv_textId); /* textview id  */
        holder.txtName = (TextView) convertView
                .findViewById(R.id.customized_listview_lv_names); /* texview for names */
        holder.imvFavourite = (ImageView) convertView
                .findViewById(R.id.customized_listview_imv_favourite); /* imageview for favorite icons */
        holder.isSelected = false;
        convertView.setTag(holder);
    } else {
        holder = (ViewHolder) convertView.getTag();
    }

    holder.txtID.setText(String.valueOf(arrayListAd.get(position).getNameId())); /* model class functions */
    holder.txtName.setText(arrayListAd.get(position).getName());  /* model class functions */

    if (arrayListAd.get(position).getFinfavId() > 0) {
        holder.imvFavourite.setImageResource(R.mipmap.fav_filled);
        holder.imvFavourite.setTag("Dark=" + holder.txtID.getText());

    } else if (arrayListAd.get(position).getFinfavId() == 0) {
        holder.imvFavourite.setImageResource(R.mipmap.fav_not_filled);
        holder.imvFavourite.setTag("Light=" + holder.txtID.getText());

    }
    if (holder.isSelected){
        // set the imvFavourite
    }else{
        // un set the imvFavourite. This is important!
    }

    holder.imvFavourite.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            String tagName = holder.imvFavourite.getTag().toString();
            String str[] = tagName.split("=");

            // Setting the isSelected field here
            holder.isSelected = true
            tagName = str[0];
            String strID = str[1];

            if (tagName.equalsIgnoreCase("Light")) {
                holder.imvFavourite.setImageResource(R.mipmap.fav_filled);
                holder.imvFavourite.setTag("Dark=" + strID);
                dbfav.insertDataInFavourite(strID);  /* function inserts records in database */

            } else if (tagName.equalsIgnoreCase("Dark")) {
                holder.imvFavourite.setImageResource(R.mipmap.fav_not_filled);
                holder.imvFavourite.setTag("Light=" + strID);
                dbfav.deleteByID(strID);            /* function that delete record from database */

            }
        }
    });
    return convertView;
}
}

答案 2 :(得分:0)

使用以下给定的适配器类替换您的适配器类。 首先,ViewHolder类不是静态的第二件事,永远不会将最终变量或类用于getView()方法。请参考此代码。有关更多信息,请参阅此链接 androidsimplifycodes.blogspot.in

package com.androidsimplifycodes.example.ListView;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.androidsimplifycodes.example.R;

import java.util.ArrayList;

public class ListViewAdapter extends BaseAdapter {

    private Context context;
    private ArrayList list;

    // passing the objects array list
    public ListViewAdapter(Context context, ArrayList list) {
        this.context = context;
        this.list = list;
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int i) {
        return list.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int i, View convertView, ViewGroup viewGroup) {

        ViewHolder holder;
        // this condition only first time true because when adapter call that 
        // time your View is null so fist we bind xml file then after using ViewHolder 
        // bind views.
        if (convertView == null) {
            // bind your list_item.xml here 
            convertView = LayoutInflater.from(context).inflate(R.layout.list_item, viewGroup, false);
            holder = new ViewHolder(convertView);
            // if you not set holder into View then each and every time fist bind xml
            // then after make ViewHolder calls
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        holder.tvText.setText(list.get(i).getText());

        return convertView;
    }

    // this class use for the memory utilization for example one time your 
    // xml view bind with java using findViewById then stored into ViewHolder
    // using setTag method. Here question arise but why required this procedure
    // because at run time each and every time not bind your xml view with adapter
    // it will be provide memory utilization and scrolling speed to the View.
    private static class ViewHolder {
        TextView tvText;

        ViewHolder(View rootView) {
            initView(rootView);
        }

        private void initView(View rootView) {
            tvText = (TextView) rootView.findViewById(R.id.tvText);
        }
    }
}

答案 3 :(得分:0)

滚动列表视图收藏夹图标时,

解决了问题

    public class AdapterFind extends BaseAdapter {

        ArrayList<BeanFindName> arrayListAd; /* arraylist with model class */
        Activity activity;
        DBFavourite dbfav;                   /* database class */

        private boolean[] favorites;           `ADDED`


        public AdapterFind(ArrayList<BeanFindName> arrayListAd, Activity           activity) {
            this.arrayListAd = arrayListAd;
            this.activity = activity;
            dbfav = new DBFavourite(activity);

            favorites = new boolean[this.arrayListAd.size()];    `ADDED`

        }

        @Override
        public int getCount() {
            return arrayListAd.size();
        }

        @Override
        public Object getItem(int position) {
            return arrayListAd.get(position);
        }

        @Override
        public long getItemId(int position) {
            return 0;
        }

        private class ViewHolder {

            TextView txtID;
            TextView txtName;
            ImageView imvFavourite;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {

            final ViewHolder holder;
            LayoutInflater inflater = activity.getLayoutInflater();

            if (convertView == null) {
                convertView = inflater.inflate(R.layout.activity_customized_listview, null);  /* layout file for listview row items */
                holder = new ViewHolder();

                holder.txtID = (TextView) convertView
                        .findViewById(R.id.customized_listview_tv_textId); /* textview id  */
                holder.txtName = (TextView) convertView
                        .findViewById(R.id.customized_listview_lv_names); /* texview for names */
                holder.imvFavourite = (ImageView) convertView
                       .findViewById(R.id.customized_listview_imv_favourite); /* imageview for favorite icons */

                convertView.setTag(holder);
            } else {
                holder = (ViewHolder) convertView.getTag();
            }

            holder.txtID.setText(String.valueOf(arrayListAd.get(position).getNameId())); /* model class functions */
            holder.txtName.setText(arrayListAd.get(position).getName());  /* model class functions */

            if (arrayListAd.get(position).getFinfavId() > 0 || favorites[position]) {
                holder.imvFavourite.setImageResource(R.mipmap.customized_listview_iv_favourites_boy_filled);

            } else if (arrayListAd.get(position).getFinfavId() == 0 || !favorites[position]) {
                holder.imvFavourite.setImageResource(R.mipmap.customized_listview_iv_favourites_boy_not_filled);
            }

            holder.imvFavourite.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {

`ADDED`

      if (favorites[position] || arrayListAd.get(position).getFinfavId() > 0) {  /* check for scrolling and database also to change favorite icon */
                        holder.imvFavourite.setImageResource(R.mipmap.customized_listview_iv_favourites_boy_not_filled);
                        dbfav.deleteByID(holder.txtID.getText().toString()); /* delete records from database*/
                        favorites[position] = false;
                    } else if (arrayListAd.get(position).getFinfavId() == 0 || !favorites[position]) {                          holder.imvFavourite.setImageResource(R.mipmap.customized_listview_iv_favourites_boy_filled);
         dbfav.insertDataInFavourite(holder.txtID.getText().toString()); /*inserts records to database */
                        favorites[position] = true; 
                    }
                 }               
            });
            return convertView;
        }
    }