0

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;
    }
}
Amit Kumar Gupta
  • 17,184
  • 7
  • 46
  • 64
cormacc00
  • 45
  • 8

2 Answers2

1

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();
Will Thomson
  • 875
  • 6
  • 19
  • Thanks for that, but i may be doing something wrong. I added the "boolean check = checkbox.isChecked();" but my MainActivity.java wont detect it and gives the same error and as well as that, the sdk is warning me the local variable "check" isn't in use (So i presume its because MainActivity.java is not detecting it) What went wrong, and again thanks for trying to help – cormacc00 Aug 29 '14 at 15:02
  • my answer was more of a suggestion than a solution, that was just a mistake that jumped out at me. I'll have a look at your code a bit later today and see if I can give you more complete help! – Will Thomson Aug 29 '14 at 16:23
  • So I'm not entirely sure what the purpose of the Boolean 'check' is for in your MainActivity. At your decleration, the resource R.id.checkbox1 exists in your xml reference (and is just an integer reference to a view in your layout), but an actual CheckBox object has not been instantiated yet. You won't have any relevant boolean information about the the state of the CheckBox in question until you instantiate in in your CustomGrid. – Will Thomson Aug 29 '14 at 21:03
  • If it's a persistent state that you want for before OnCreate I might recommend storing it under SharedPreferences: http://stackoverflow.com/questions/3624280/how-to-use-sharedpreferences-in-android-to-store-fetch-and-edit-values can you elaborate a bit more on the behavior you are looking for? – Will Thomson Aug 29 '14 at 21:06
  • Yeah, what I have done is I've made an app that has checkboxes in it. But when I used 'int' in my MainActivity.java for the check box I had no control where the check boxes appeared. They just kept appearing next to the ImageID int. Continue.. – cormacc00 Aug 29 '14 at 22:23
  • So then I went into the customgrid.java and saw what the problem was. The images were controlled becasus all the images in the string.XML were made a variable. So I did the same process with image and with a little help online. But from what I saw from other peoples code and the from what the SDK told me it said to use Boolean. Now my problem is that I get the error (Also keep in mind I have an XML file that contains the textbox and image code for the elements to shoe up). The behavior I am looking for is that the checkboxes behave the same way the images show up – cormacc00 Aug 29 '14 at 22:24
  • I'm afraid I'm still unclear of what you want to happen, and I was assuming that we would be shifted in to a conversation instead of continuing like this in comments, but you have to have 20 reputation to open conversations. I'm going to try and answer your original question more directly, and if that doesn't help I would recommend asking a new question with more specific wording, and perhaps including your xml file. – Will Thomson Aug 29 '14 at 23:08
1

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...

Will Thomson
  • 875
  • 6
  • 19