Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/388.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 我正在使用GridView,当我滚动它时,收藏夹按钮的状态更改为默认状态_Java_Android - Fatal编程技术网

Java 我正在使用GridView,当我滚动它时,收藏夹按钮的状态更改为默认状态

Java 我正在使用GridView,当我滚动它时,收藏夹按钮的状态更改为默认状态,java,android,Java,Android,我使用gridview显示图像和文本,就像一个简单的电子商务应用程序显示收藏夹图标一样,当我滚动视图时,我标记为收藏夹的项目将更改为其默认状态,即(未标记),虽然,就像RecyclerView一样,但我没有使用它,因此无法找到视图更改的任何线索,请给出正确的解释。 适配器类 public class Category_Adapter extends BaseAdapter { private Context mContext; String name[], img[];

我使用gridview显示图像和文本,就像一个简单的电子商务应用程序显示收藏夹图标一样,当我滚动视图时,我标记为收藏夹的项目将更改为其默认状态,即(未标记),虽然,就像RecyclerView一样,但我没有使用它,因此无法找到视图更改的任何线索,请给出正确的解释。

适配器类

public class Category_Adapter extends BaseAdapter {
    private Context mContext;
    String name[], img[];
    int price[];
    LayoutInflater inflater;

    public Category_Adapter(Context context, String[] name, String[] img, int price[]) {
        this.mContext = context;
        this.price = price;
        this.name = name;
        this.img = img;
        inflater = (LayoutInflater) context.
                getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    }

    Category_Adapter(Context context) {
        this.mContext = context;
    }

    @Override
    public int getCount() {
        return name.length;
    }

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

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

    public class Holderr {
        TextView product_name;
        Button addCart;
        TextView price_pro;
        ImageView product_img, mark, marked;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        final Holderr holderr = new Holderr();
        View rowView;
        rowView = inflater.inflate(R.layout.product_category_grid, null);
        holderr.product_img = (ImageView
                ) rowView.findViewById(R.id.imageofitem);
        holderr.addCart = rowView.findViewById(R.id.cartmein);
        holderr.mark = rowView.findViewById(R.id.markFav);
        holderr.marked = rowView.findViewById(R.id.markedFav);
        holderr.product_name = (TextView) rowView.findViewById(R.id.productkanam);
        holderr.price_pro = (TextView) rowView.findViewById(R.id.priceof_item);
        holderr.price_pro.setText("Rs. " + price[position]);
        holderr.product_name.setText(name[position]);
        Glide.with(mContext).load(img[position]).into(holderr.product_img);
        holderr.addCart.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                Toasty.success(mContext, name[position] + " Added in cart", Toast.LENGTH_SHORT).show();
            }
        });
        holderr.mark.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                holderr.mark.setVisibility(View.GONE);
                holderr.marked.setVisibility(View.VISIBLE);
            }
        });
        holderr.marked.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                holderr.marked.setVisibility(View.GONE);
                holderr.mark.setVisibility(View.VISIBLE);

            }
        });

        return rowView;
    }

}
在GridView中设置适配器的类

public class ViewMoreCategory extends AppCompatActivity {
    GridView gridView;
    ImageButton back, filter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_view_more_category);
        filter = findViewById(R.id.filter_items);
        gridView = findViewById(R.id.product_cat_grid);
        String name[] = {"Shampoo", "Oil", "Ghee", "Cream", "Shampoo", "Oil", "Ghee", "Cream", "Shampoo", "Oil"};
        String img[] = {"http://pngimg.com/uploads/shampoo/shampoo_PNG18.png", "http://pngimg.com/uploads/shampoo/shampoo_PNG18.png", "http://pngimg.com/uploads/shampoo/shampoo_PNG18.png", "http://pngimg.com/uploads/shampoo/shampoo_PNG18.png", "http://pngimg.com/uploads/shampoo/shampoo_PNG18.png", "http://pngimg.com/uploads/shampoo/shampoo_PNG18.png", "http://pngimg.com/uploads/shampoo/shampoo_PNG18.png", "http://pngimg.com/uploads/shampoo/shampoo_PNG18.png", "http://pngimg.com/uploads/shampoo/shampoo_PNG18.png", "http://pngimg.com/uploads/shampoo/shampoo_PNG18.png"};
        int price[] = {500, 200, 500, 100, 50, 330, 200, 300, 900, 400};
        gridView.setAdapter(new Category_Adapter(ViewMoreCategory.this, name, img, price));
         getSupportActionBar().setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
        getSupportActionBar().setDisplayShowCustomEnabled(true);
        getSupportActionBar().setCustomView(R.layout.custom_toolbar);
        View view = getSupportActionBar().getCustomView();
        back = view.findViewById(R.id.backtoHome);
        gridView.setAdapter(new Category_Adapter(this, name, img, price));
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(ViewMoreCategory.this, Dashboard.class));
            }
        });
    }
}

我认为你需要保持所选项目的位置

  • 模型类添加属性,如下所示:
    私有int状态
  • 获取所选项目的位置,然后更改
    状态
    属性的值
  • 最后,在所需的
    Adapter
    ad中的
    getView
    中更改
    imageView
    的图像

  • 希望它能帮助你

    检查这不是一个回收视图正确阅读我的问题上面的链接只是提示你需要保持fav按钮的状态它不是一个回收视图,但它们的工作方式类似。为了不使用超过所需的内存,视图将被回收。因此,在getView方法中,您需要做的是每次设置按钮的状态,否则它将重置。@NileStrathod在我传递GridView时将RecyclerView作为参数,它无法解析该视图