无法从嵌套的静态类访问复选框对象

时间:2018-08-20 13:50:57

标签: android android-studio gridview

我为图像创建了一个网格视图,并添加了用于选择图像的复选框,但是该复选框被回收,因此还选择了其他图像。

这是我的模型课

class ImageItem
{ 
    public void setSelected(boolean selected) {
        this.selected = selected;
    }
    public boolean isSelected() {
        return selected;
    }
}

这是我的getView()。

public View getView(final int position, View convertView, @NonNull ViewGroup parent ) {
        View row = convertView;
        final ViewHolder holder;
        if (row == null) {

            LayoutInflater inflater = ((Activity) context).getLayoutInflater();
            row = inflater.inflate(layoutResourceId, parent,false);
            holder = new ViewHolder();
            //find resource...
            holder.image= (ImageView)row.findViewById(R.id.image);
            holder.imageTitle = (TextView) row.findViewById(R.id.text);
            holder.check = (CheckBox)row.findViewById(R.id.checkbox) ;
           row.setTag(holder);
       } else {
         holder = (ViewHolder) row.getTag();
       }

       holder.check.setOnCheckedChangeListener(null);
       holder.check.setFocusable(false);

       holder.check.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
           if (data.get(position).isSelected()) {
              data.get(position).setSelected(false);
           } else {
             data.get(position).setSelected(true);
           }    
        }
    });


    holder.check.setChecked(data.get(position).isSelected());
    holder.imageTitle.setText(data.get(position).getTitle());

    holder.image.setImageBitmap(data.get(position).getImage());
    return row;
}

static class ViewHolder {
    TextView imageTitle;
    ImageView image;
    public  static CheckBox check;
}

我也试图将状态保存在布尔数组中,但这也导致了同样的问题。我知道提供了许多解决方案,但对我没有任何帮助。请完整阅读我的代码。我想念什么?

建议的修改后 我将复选框设为非静态,并在mainactivity中创建了一个对象,以在longclick上设置visibl。

 static class ViewHolder {
    TextView imageTitle;
    ImageView image;
    CheckBox check;
     CheckBox method() {


        return check;
    }
gridView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
        @Override
        public boolean onItemLongClick(AdapterView<?> adapterView, View view, int i, long l) {

GridViewAdapter.ViewHolder vh = new GridViewAdapter.ViewHolder();
            check = vh.method();
for(int index=0;index< adapterView.getChildCount();index++) {
             View nextchild = (adapterView.getChildAt(index));
           CheckBox checkBox = (CheckBox) nextchild.findViewById(R.id.checkbox);
             checkBox.setVisibility(View.VISIBLE);

我在第

行将错误作为类广播异常
check = vh.method();

非常感谢到目前为止提供帮助的人

3 个答案:

答案 0 :(得分:0)

也许是因为您使用

public class Startup
{
    public Startup(IConfiguration configuration)
    {
        Configuration = configuration;
    }

    public IConfiguration Configuration { get; }

    // This method gets called by the runtime. Use this method to add services to the container.
    public void ConfigureServices(IServiceCollection services)
    {
        services.Configure<CookiePolicyOptions>(options =>
        {
        // This lambda determines whether user consent for non-essential cookies is needed for a given request.
        options.CheckConsentNeeded = context => true;
            options.MinimumSameSitePolicy = SameSiteMode.None;
        });


        services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
    }

    // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
    public void Configure(IApplicationBuilder app, IHostingEnvironment env)
    {
        if (env.IsDevelopment())
        {
            app.UseDeveloperExceptionPage();
        }
        else
        {
            app.UseExceptionHandler("/Home/Error");
        }

        app.UseStaticFiles();
        app.UseCookiePolicy();

        app.UseMvc(routes =>
        {
            routes.MapRoute(
                name: "default",
                template: "{controller=Home}/{action=Index}/{id?}");
        });
    }
}

尝试像这样删除public static CheckBox check;

static

答案 1 :(得分:0)

首先将您的复选框设为非静态

In file included from /usr/include/stdlib.h:42:0,
from _cgo_export.c:2:
/usr/include/x86_64-linux-gnu/bits/waitstatus.h:79:15: error: duplicate member ‘__w_retcode’
  unsigned int __w_retcode:8;
               ^
/usr/include/x86_64-linux-gnu/bits/waitstatus.h:80:15: error: duplicate member ‘__w_coredump’
  unsigned int __w_coredump:1;
               ^
/usr/include/x86_64-linux-gnu/bits/waitstatus.h:81:15: error: duplicate member ‘__w_termsig’
  unsigned int __w_termsig:7;
               ^
/usr/include/x86_64-linux-gnu/bits/waitstatus.h:93:15: error: duplicate member ‘__w_stopsig’
  unsigned int __w_stopsig:8; /* Stopping signal.  */
               ^
/usr/include/x86_64-linux-gnu/bits/waitstatus.h:94:15: error: duplicate member ‘__w_stopval’
  unsigned int __w_stopval:8; /* W_STOPPED if stopped.  */

然后按如下所示更改您的static class ViewHolder { TextView imageTitle; ImageView image; CheckBox check; } 方法

onClick

答案 2 :(得分:0)

check删除静态修饰符,您可以尝试以下操作:

...

private final SparseBooleanArray mCheckedItemPositions = new SparseBooleanArray();

...

public View getView(final int position, View convertView, @NonNull ViewGroup parent ) {
    ...

    holder.check.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            mCheckedItemPositions.put(position, isChecked);
        }
    });

    holder.check.setChecked(mCheckedItemPositions.get(position));
    ...
}