除非滚动,否则Listview图像不会被更改

时间:2016-02-07 10:32:21

标签: android listview imageview sharedpreferences android-adapter

我有一个列表视图。当点击listitem时,它会打开一个新活动,其中包含一个按钮(添加到fav),它将打开的列表项添加到我的收藏夹活动(使用共享首选项),并应将listitem中心脏图像的颜色从灰色更改为红色指示它的最爱。 将listitem添加到收藏夹工作正常,但心脏图像不会从灰色变为红色,除非它从屏幕滚出用户的视图并再次滚动回到它。 为了更好地理解我的问题 look at this video

我希望瞬间改变图像

顺便把listitem对象转换为jsonString并通过intent传递它我使用的jacksons库

我使用的代码

我的列表片段的onitemclicklistener

@Override
public void onItemClick(AdapterView<?> parent, View view, int position,
                        long id) {

                            ObjectMapper mapper = new ObjectMapper();
                            Product pro = productListAdapter.getItem(position);


    try
    {


        String jsonInString = mapper.writeValueAsString(pro);

        Intent intent = new Intent(activity.getApplicationContext(), SingleItemView.class);
        intent.putExtra("selected item", jsonInString);


        startActivity(intent);
    }
    catch (JsonProcessingException e)
    {}  



}

我的列表适配器

public class ProductListAdapter extends ArrayAdapter<Product> {

private Context context;
List<Product> products;
SharedPreference sharedPreference;

public ProductListAdapter(Context context, List<Product> products) {
    super(context, R.layout.product_list_item, products);
    this.context = context;
    this.products = products;
    sharedPreference = new SharedPreference();
}

private class ViewHolder {
    TextView productNameTxt;
    TextView productDescTxt;
    TextView productPriceTxt;
    ImageView favoriteImg;
}

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

@Override
public Product getItem(int position) {
    return products.get(position);
}

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

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    ViewHolder holder = null;
    if (convertView == null) {
        LayoutInflater inflater = (LayoutInflater) context
            .getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
        convertView = inflater.inflate(R.layout.product_list_item, null);
        holder = new ViewHolder();
        holder.productNameTxt = (TextView) convertView
            .findViewById(R.id.txt_pdt_name);
        holder.productDescTxt = (TextView) convertView
            .findViewById(R.id.txt_pdt_desc);
        holder.productPriceTxt = (TextView) convertView
            .findViewById(R.id.txt_pdt_price);
        holder.favoriteImg = (ImageView) convertView
            .findViewById(R.id.imgbtn_favorite);

        convertView.setTag(holder);
    } else {
        holder = (ViewHolder) convertView.getTag();
    }
    Product product = (Product) getItem(position);
    holder.productNameTxt.setText(product.getName());
    holder.productDescTxt.setText(product.getDescription());
    holder.productPriceTxt.setText(product.getPrice() + "");

    /*If a product exists in shared preferences then set heart_red drawable
     * and set a tag*/
    if (checkFavoriteItem(product)) {
        holder.favoriteImg.setImageResource(R.drawable.heart_red);
        holder.favoriteImg.setTag("red");
    } else {
        holder.favoriteImg.setImageResource(R.drawable.heart_grey);
        holder.favoriteImg.setTag("grey");
    }

    return convertView;

}

/*Checks whether a particular product exists in SharedPreferences*/
public boolean checkFavoriteItem(Product checkProduct) {
    boolean check = false;
    List<Product> favorites = sharedPreference.getFavorites(context);
    if (favorites != null) {
        for (Product product : favorites) {
            if (product.equals(checkProduct)) {
                check = true;
                break;
            }
        }
    }
    return check;
}

@Override
public void add(Product product) {
    super.add(product);
    products.add(product);
    notifyDataSetChanged();
}

@Override
public void remove(Product product) {
    super.remove(product);
    products.remove(product);
    notifyDataSetChanged();
}   
}

单项活动

public class SingleItemView extends Activity
{
ProductListAdapter padaptr;
SharedPreference sharedPreference;

List<Product> products = null;

@Override
protected void onCreate(Bundle savedInstanceState)
{
    // TODO: Implement this method
    super.onCreate(savedInstanceState);
    setContentView(R.layout.singleitem);
    sharedPreference = new SharedPreference();
    padaptr = new ProductListAdapter(SingleItemView.this, products);





    Button btn = (Button) findViewById(R.id.singleitemButton1);
    btn.setOnClickListener(new OnClickListener(){
        @Override
        public void onClick(View v){
            products=new ArrayList<Product>();
            Bundle extras = getIntent().getExtras();

            String jsonObj = extras.getString("selected item");


            ObjectMapper mapper = new ObjectMapper();

            try
            {
                Product pro = mapper.readValue(jsonObj, Product.class);

                if (checkFavoriteItem(pro)) {

                    sharedPreference.removeFavorite(SingleItemView.this, pro);

                    Toast.makeText(SingleItemView.this,
                                   SingleItemView.this.getResources().getString(R.string.remove_favr),
                                   Toast.LENGTH_SHORT).show();
                                   padaptr.notifyDataSetChanged();
                } else {
                    sharedPreference.addFavorite(SingleItemView.this, pro);
                    Toast.makeText(SingleItemView.this,
                                   SingleItemView.this.getResources().getString(R.string.add_favr),
                                   Toast.LENGTH_SHORT).show();
                                   padaptr.notifyDataSetChanged();


                }
            }
            catch (IOException e)
            {};





        }



            private boolean checkFavoriteItem(Product checkProduct) {
                boolean check = false;
                List<Product> favorites = sharedPreference.getFavorites(getApplicationContext());
                if (favorites != null) {
                    for (Product product : favorites) {
                        if (product.equals(checkProduct)) {
                            check = true;
                            break;
                        }
                    }
                }
                return check;
            }
    });
    }


}

2 个答案:

答案 0 :(得分:0)

当您回到片段/活动时,您需要通知适配器有关更改。

使用startActivityForResult()开始您的第二项活动:

Intent intent = new Intent(activity.getApplicationContext(), SingleItemView.class);
intent.putExtra("selected item", jsonInString);
startActivityForResult(intent, 1);

然后覆盖onActivityResult()方法来处理回调,并通过notifyDataSetChanged()通知您的适配器:

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {

    if (requestCode == 1) {
        productListAdapter.notifyDataSetChanged();
    }
}

答案 1 :(得分:0)

我希望通过我的列表适配器添加toggleSelection来实现您在视频中附加的行为。这是我附加一个工作适配器,它将在单击时切换列表项背景。这可能会被修改以满足您的目的。

public class ToggleSelectionListAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

        private Cursor mCursor;
        private SparseBooleanArray selectedItems;

        public ToggleSelectionListAdapter(Cursor cursor) {
            mCursor = cursor;
            selectedItems = new SparseBooleanArray();
        }

        public void toggleSelection(int pos) {
            if (selectedItems.get(pos, false)) {
                selectedItems.delete(pos);
            } else {
                selectedItems.put(pos, true);
            }
            notifyItemChanged(pos);
        }

        public int getSelectedItemCount() {
            return selectedItems.size();
        }

        public void clearSelections() {
            selectedItems.clear();
            notifyDataSetChanged();
        }

        public class ViewHolder extends RecyclerView.ViewHolder {

            public ViewHolder(final View itemView) {
                super(itemView);

                // Initialize your items of each row here
            }

            public void bindView(int pos) {
                try {
                    if (mCursor.isClosed())
                        return;

                    mCursor.moveToPosition(pos);

                    // Maintain a checked item list so that you can have a track if the item is clicked or not
                    if (checkedItems.contains(number) itemView.setBackgroundResource(R.drawable.background_selected);
                    else itemView.setBackgroundResource(R.drawable.background_normal);

                    itemView.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                                    if (checkedItems.contains(number)) {
                                        checkedItems.remove(number);
                                    } else {
                                        checkedItems.add(number);
                                    }

                                // Get the index of which item should toggle the background
                                int idx = mRecyclerView.getChildAdapterPosition(v);
                                toggleSelection(idx);
                            }
                        }
                    });
            }
        }


        @Override
        public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

            View v;

            v = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_item_row, parent, false);

            ViewHolder vh = new ViewHolder(v);

            return vh;
        }

        @Override
        public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {

            if (holder instanceof ViewHolder) {
                ViewHolder vh = (ViewHolder) holder;

                vh.bindView(position);

            }
        }

        @Override
        public int getItemCount() {

            if (mCursor == null) {
                return 0;
            }

            int n = mCursor.getCount();
            return n;
        }

        @Override
        public int getItemViewType(int position) {
            return super.getItemViewType(position);
        }

        synchronized public void swapCursor(Cursor cursor) {
            mCursor = cursor;
            notifyDataSetChanged();
        }
    }