gpt4 book ai didi

java - Android 从 gridview 中删除一个项目

转载 作者:太空狗 更新时间:2023-10-29 16:35:47 25 4
gpt4 key购买 nike

我正在开发一个包含 GridView 的应用程序。我用包含图像和文本的自定义适配器填充此网格。

adapter = new CustomGrid(this, titlearray, imageId);
grid=(GridView)findViewById(R.id.gridView1);
grid.setAdapter(adapter);

当我单击任何项​​目时,会打开一个包含按钮的对话框,我想要的是当我单击此按钮时该项目将被删除。我检查了很多解决方案,他们说我应该更换适配器或使用新的。

我尝试了以下代码,我将数据放入新数组并将它们加载到适配器中,然后在网格中使用它们:

                    String[] ntarray=new String[wcount];    
Drawable[] ndarray=new Drawable[wcount];





if(position==0)
{
for(int i=0;i<wcount;i++)
{
ntarray[i]=titlearray[i+1];
ndarray[i]=imageId[i+1];
}
}


if(position==wcount-1)
{
for(int i=0;i<wcount-1;i++)
{
ntarray[i]=titlearray[i];
ndarray[i]=imageId[i];
}
}


adapter = new CustomGrid(Wishlist.this, ntarray, ndarray);
grid.setAdapter(adapter);
adapter.notifyDataSetChanged();
dialog.hide();

我正在使用自定义网格适配器:

package com.erc.test;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
public class CustomGrid extends BaseAdapter{
private Context mContext;
private final String[] web;
private final Drawable[] Imageid;
public CustomGrid(Context c,String[] web,Drawable[] imageId ) {
mContext = c;
this.Imageid = imageId;
this.web = web;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return web.length;
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return null;
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
View grid;
LayoutInflater inflater = (LayoutInflater) mContext
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if (convertView == null) {
grid = new View(mContext);
grid = inflater.inflate(R.layout.grid_single, null);
Button textView = (Button) grid.findViewById(R.id.grid_text);
ImageView imageView = (ImageView)grid.findViewById(R.id.grid_image);
textView.setText(web[position]);
// textView.setShadowLayer(2, 1, 1, Color.BLACK);
imageView.setImageDrawable(Imageid[position]);
} else {
grid = (View) convertView;
}
return grid;
}
}

是否有任何函数或方法可以帮助我在不创建新适配器的情况下删除此项,如果是,我如何将它添加到我的 customgrid 类中?

最佳答案

进行这些更改:

    ArrayList<String> ntarray= new ArrayList<String>();    
ArrayList<Drawable> ndarray= new ArrayList<Drawable>();

if(position==0){
for(int i=0;i<wcount;i++){
ntarray.set(i, titlearray[i+1]);
ndarray.set(i, imageId[i+1]);
}
}

if(position==wcount-1){
for(int i=0;i<wcount-1;i++){
ntarray.set(i, titlearray[i]);
ndarray.set(i, imageId[i]);
}
}

adapter = new CustomGrid(Wishlist.this, ntarray, ndarray);
grid.setAdapter(adapter);
adapter.notifyDataSetChanged();
dialog.hide();

在你的适配器中:

    public class CustomGrid extends BaseAdapter{
private Context mContext;
private final ArrayList<String> web;
private final ArrayList<Drawable> Imageid;

public CustomGrid(Context c, ArrayList<String> web, ArrayList<Drawable> imageId ) {
mContext = c;
this.Imageid = imageId;
this.web = web;
}

public void removeItem(int position){
web.remove(position);
Imageid.remove(position);
notifyDataSetChanged();
}

....
}

当你想删除 gridview 中的项目时,只需调用:adapter.removeItem(TheItemPosition);

更新

您可以在适配器中添加此方法以清除所有项目:

public void clearData(){
web.clear();
Imageid.clear();
notifyDataSetChanged();
}

关于java - Android 从 gridview 中删除一个项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29233768/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com