0

I have this code:

package com.app.BoomBase;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.LinearLayout;

public class CheckList extends Activity{
ImageButton bAddBoom, bAddTools, bAddCloth, bRemoveBoom, bRemoveTools, bRemoveCloth;



@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.check_list);
    final LinearLayout llBooms = (LinearLayout) findViewById(R.id.llBooms);
    ImageButton bAddBoom = (ImageButton) findViewById(R.id.bAddBoom);


    bAddBoom.setOnClickListener(new View.OnClickListener() {

        @SuppressWarnings("deprecation")
        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            AlertDialog alert = new AlertDialog.Builder(CheckList.this).create();
            final EditText input = new EditText(CheckList.this); 
            alert.setTitle("Name of category?");
            alert.setView(input);
            alert.setButton("OK", new DialogInterface.OnClickListener() {
                 public void onClick(DialogInterface dialog, int which) {
                     String userin = input.getText().toString();
                     if(userin != null){
                        return; 
                        }
                        else{   
                        for(int i = 0; i < 1; i++) {
                            CheckBox cb1 = new CheckBox(getBaseContext());
                            cb1.setText(userin+i);
                            cb1.setId(i+1);
                            llBooms.addView(cb1);
                        }
                        }

                    }
                });
                alert.show(); 
            alert.setButton("Cancel", new DialogInterface.OnClickListener() {

                @Override
                public void onClick(DialogInterface dialog, int which) {
                    // TODO Auto-generated method stub
                }

                });

        }
    });
}

}

And I cannot figure out how to make the checkbox have name of the variable "userin".. can anyone help me ? Another question: How do i handle the newly created checkbox in the code, so that i can check the content if it is checked or not ?

Thanks !

10
  • what`s the issue? You can see the checkbox but the text? Commented Jun 2, 2013 at 19:01
  • The checkbox isn't created when i press "ok" in the dialog.. or if i just set a text whith in "" in the cb1.setText() Commented Jun 2, 2013 at 19:03
  • That is what I actually don't know.. I've tried Log.d.. but didn't get any result.. Commented Jun 2, 2013 at 19:07
  • so actually you do not know if the code that create and add the checkbox is executed, am I wrong? Commented Jun 2, 2013 at 19:08
  • That is correct... But can you see if I'm doing something wrong ? Commented Jun 2, 2013 at 19:11

1 Answer 1

1

@Lasse,

there are a couple of "gaps" in your understanding of Android programming that you have to fill before going on. Even if someone would actually answer you, it would definitly not help as the same problem would occur again after a couple of minutes if add more code to your app, the way you do it.

I don't wanna offend with this answer, but your question is basically : how can I debug anything on Android ? Your problem is to know whether or not, and why some code gets executed and you have to understand how to check this all the time when programming.

So you should / could :

  • learn how to use a debugger. In android, you can debug a program and see how code gets executed by running it line by line, having access to values of variables, of expressions, etc.
  • learn how to trace a program, basically by using Log.x() as you did, but also printing variables values, checking branches of conditionnal code, etc.
  • learn to read the docs. You are actually using the method setButton(...) which has been deprecated in SDK 3 (actual version is 17...). Learn how to read this.
  • Follow the warnings inside your IDE and try to remove them all to increase quality of your apps.
  • Use Lint, it will advise you even more about potential problems in your apps. Again, make sure there is no Lint warning when you program, even more when you release.

This is a better advice than giving you the answer.

Nevertheless, here the problem can have 2 causes according to me :

  • using setButton twice in your program, that will override your first listener.
  • using a deprecated API that is not maintained. Try using the new method that replaces it and things should work better.
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks! I will look in through it :-)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.