cormacc00
cormacc00

Reputation: 45

How to add checkboxes to 'Grid View' in Android using Java code

How do I add checkboxes using Java on Android. When I add a boolean to the 'CustomGrid.java' and then add the checkboxes to my 'MainActivity.java' the SDK tells me

Item mismatch: cannot convert from int[] to boolean

I have tried a few methods for converting it in Java but I am not familiar with this Java and I am very new (about 4 days). This is what I've done so far:

Mainactivity.java

import android.os.Bundle;
import android.app.Activity;
import android.content.res.Configuration;
import android.view.View;
import android.widget.AdapterView;
import android.widget.CheckBox;
import android.widget.GridView;
import android.widget.Toast;


public static void main(String[] args) throws Exception {
    int i=10;
    boolean b = (i != 0);

    System.out.println(b);
}

public class MainActivity extends Activity {
    GridView grid;
    String[] web = {
        "Google",
        "",
        "Instagram",
        "Facebook",
        "Pinterest",
        "Twitter",  
    } ;


    int[] imageId = {
        R.drawable.image1,
        R.string.share,
        R.drawable.image3,
        R.drawable.image4,
        R.drawable.image6,
        R.drawable.image8,
    };

    boolean check = {
        R.id.checkbox1
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        CustomGrid adapter = new CustomGrid(MainActivity.this, web, imageId, check);
        grid=(GridView)findViewById(R.id.grid);
        grid.setAdapter(adapter);
        grid.setOnItemClickListener(new AdapterView.OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Toast.makeText(MainActivity.this, "You Clicked at " +web[+ position], Toast.LENGTH_SHORT).show();
            }
        });
    }
}

CustomGrid.java

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

public class CustomGrid extends BaseAdapter{
    private Context mContext;
    private final String[] web;
    private final int[] Imageid;
    private final boolean check;
    public CustomGrid(Context c,String[] web,int[] Imageid,boolean check2){
        mContext = c;
        this.Imageid = Imageid;
        this.web = web;
        this.check = check2;
    }

    @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);
            TextView textView = (TextView) grid.findViewById(R.id.grid_text);
            CheckBox checkbox = (CheckBox) grid.findViewById(R.id.checkbox1);
            ImageView imageView = (ImageView)grid.findViewById(R.id.grid_image);
            textView.setText(web[position]);
            imageView.setImageResource(Imageid[position]);
            checkbox.setChecked(check);
        } else {
            grid = (View) convertView;
        }

        return grid;
    }
}

Upvotes: 0

Views: 1812

Answers (2)

Will Thomson
Will Thomson

Reputation: 885

R.id.checkbox1

is just an integer that refers to the 'checkbox1' CheckBox type View. Hence the type mismatch error you are receiving.

boolean check = {
        R.id.checkbox1

would need to look something more like this:

CheckBox checkbox = (CheckBox) grid.findViewById(R.id.checkbox1);
boolean check = checkbox.isChecked();

Upvotes: 1

Will Thomson
Will Thomson

Reputation: 885

If you are including the CheckBoxes in your activity_main XML layout file (inside your 'grid' GridView) then,

    CheckBox checkbox = (CheckBox)findViewById(R.id.'yourcheckboxid')

Or you can create and add the checkbox programmatically by,

    CheckBox checkbox = new CheckBox();
    LayoutParams checkbox_layoutparams = new GridView.LayoutParams(
        wrap_content,
        wrap_content);
    checkbox_layoutparams.addRule(GridView.'your rule')://add rules if need be
    grid.addView(checkbox,checkbox_layoutparams);

Hope this helps...

Upvotes: 1

Related Questions