6 Replies - 5151 Views - Last Post: 06 June 2011 - 03:15 PM

#1 matyus  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 29
  • Joined: 22-February 09

problem create new EditText in .java

Posted 03 June 2011 - 11:05 AM

public class main extends Activity implements onclickListener {
    /** Called when the activity is first created. */
 
    //initialize a button and a counter
    Button BtnOK;
    Button BtnCalc;
    int counter = 0;
 
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
 
        // setup the layout
        setContentView(R.layout.main);
 
        // add a click-listener on the button
        BtnOK = (Button) findViewById(R.id.btnOK);
        BtnOK.setonclickListener(new View.onclickListener() {
			
			@Override
			public void onclick(View v) {
				// TODO Auto-generated method stub
		    	TableLayout table = (TableLayout) findViewById(R.id.TableLayout01); 
		    	table.removeViews(0, counter);
		    	EditText  etxtUser = (EditText)findViewById(R.id.etxtNo);
		    	counter = Integer.parseInt(etxtUser.getText().toString());
		    	
		        for(int i=0;i<counter;i++) {
		                       
		            TableRow row = new TableRow(this);
		            TextView t = new TextView(this);
		            t.setText("Network " + (i+1));
		            EditText c = new EditText(this);
		            row.addView(t);
		            row.addView(c);
		            table.addView(row,new TableLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));    
		        }
			}
		}); 
    } 
}


guys,i have a EditText and a Button,i want my app do this,user will enter a value and when button is clicked,new rows contain the EditText and TextView will be created. the number of new rows based on the input value. but i got a few errors at TableRow row = new TableRow(this);,TextView t = new TextView(this); and EditText c = new EditText(this);..why i cannot use "this"??

i modified this code from this
public class tablelayout extends Activity implements onclickListener {
    /** Called when the activity is first created. */
 
    //initialize a button and a counter
    Button btn;
    int counter = 0;
 
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
 
        // setup the layout
        setContentView(R.layout.main);
 
        // add a click-listener on the button
        btn = (Button) findViewById(R.id.Button01);
        btn.setonclickListener(this);       
 
    }
 
    // run when the button is clicked
    public void onclick(View view) {
 
        // get a reference for the TableLayout
        TableLayout table = (TableLayout) findViewById(R.id.TableLayout01);
 
        // create a new TableRow
        TableRow row = new TableRow(this);
 
        // count the counter up by one
        counter++;
 
        // create a new TextView
        TextView t = new TextView(this);
        // set the text to "text xx"
        t.setText("text " + counter);
 
        // create a CheckBox
        CheckBox c = new CheckBox(this);
 
        // add the TextView and the CheckBox to the new TableRow
        row.addView(t);
        row.addView(c);
 
        // add the TableRow to the TableLayout
        table.addView(row,new TableLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
 
    }
}


please help me.... and if this success,how can i know the id of the new EditTexts if i want to manipulate them?

Is This A Good Question/Topic? 0
  • +

Replies To: problem create new EditText in .java

#2 H3R3T1C  Icon User is offline

  • Android Expert
  • member icon

Reputation: 277
  • View blog
  • Posts: 757
  • Joined: 30-March 07

Re: problem create new EditText in .java

Posted 03 June 2011 - 11:15 AM

What exactly is the error saying? Please post the full stack trace.
EDIT:
Oh never mind I see your problem I was looking at your bottom code. The thit is wrong becuase of the scope of this. Applying this within the onclickListener is really saying onclickListener.this.
You can get your this working for the TableRow and rest of the components by doing TableRow row = new TableRow(main.this);

This post has been edited by H3R3T1C: 03 June 2011 - 11:16 AM

Was This Post Helpful? 1
  • +
  • -

#3 matyus  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 29
  • Joined: 22-February 09

Re: problem create new EditText in .java

Posted 03 June 2011 - 01:46 PM

yes!it is working! thanks a lot man! :bananaman: and another question if you dont mind,how can i know the id of these new EditTexts if i want to manipulate them? i have tried google about it but cannot find the solution.. :(
Was This Post Helpful? 0
  • +
  • -

#4 H3R3T1C  Icon User is offline

  • Android Expert
  • member icon

Reputation: 277
  • View blog
  • Posts: 757
  • Joined: 30-March 07

Re: problem create new EditText in .java

Posted 03 June 2011 - 11:08 PM

EditTexts have a method call setID(<int>) but I would not recommend using it because you may assign an id that is being used by another component in the current Activity. What you might want to do is store the EditTexts in an ArrayList like so:
In your global variable add private ArrayList<EditText> myTexts;.
Then in your onCreate method add myTexts = new ArrayList<EditText>();. This will initialize our ArrayList.
Then whenever you push the button and the onclick method is called you will first call myTexts.clear() to remove the EditTexts that were added in a previous button click. The whenever you create a new EditText just call the myTexts.add(<EditText variable>) method to add. More info and a list of all the methods available in ArrayList can be found HERE.

This post has been edited by H3R3T1C: 03 June 2011 - 11:13 PM

Was This Post Helpful? 1
  • +
  • -

#5 matyus  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 29
  • Joined: 22-February 09

Re: problem create new EditText in .java

Posted 04 June 2011 - 02:31 AM

public class main extends Activity {
    /** Called when the activity is first created. */
 
    //initialize a button and a counter
    Button BtnOK;
    Button BtnCalc;
    int counter = 0;
    private ArrayList<EditText> myTexts;
    
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
 
        // setup the layout
        setContentView(R.layout.main);
 
        // add a click-listener on the button
        BtnOK = (Button) findViewById(R.id.btnOK);
        BtnOK.setonclickListener(new View.onclickListener() {
			
			@Override
			public void onclick(View v) {
				myTexts.clear();
				myTexts = new ArrayList<EditText>();
		    	TableLayout table = (TableLayout) findViewById(R.id.TableLayout01); 
		    	table.removeViews(0, counter);
		    	EditText  etxtUser = (EditText)findViewById(R.id.etxtNo);
		    	counter = Integer.parseInt(etxtUser.getText().toString());
		    	
		        for(int i=0;i<counter;i++) {
		                       
		            TableRow row = new TableRow(main.this);
		            TextView t = new TextView(main.this);
		            t.setText("Network " + (i+1));
		            EditText c = new EditText(main.this);
		            row.addView(t);
		            row.addView(c);
		            table.addView(row,new TableLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
		            myTexts.add(c);
		        }
			}
		});
        
        BtnCalc = (Button) findViewById(R.id.btnCalc);
        BtnCalc.setonclickListener(new View.onclickListener() {
			
			@Override
			public void onclick(View v) {
				// TODO Auto-generated method stub
				int a = 0;
		        for(int i=0;i<counter;i++) {
                    
		            a += Integer.parseInt(myTexts.get(i).getText().toString());
		        }
		        TextView TxtvResult = (TextView) findViewById(R.id.txtvResult);
		        TxtvResult.setText(a);
			}
		});
 
    }
}


are you mean like this?it has no error but when i click the button,program force close..i think maybe problem at myTexts.clear(); because initial the arraylist doesnt has items..if i put private ArrayList<EditText> myTexts = new ArrayList<EditText>(); at global variable,it is working but when i click BtnCalc,it does not working,force close.. :/
Was This Post Helpful? 0
  • +
  • -

#6 H3R3T1C  Icon User is offline

  • Android Expert
  • member icon

Reputation: 277
  • View blog
  • Posts: 757
  • Joined: 30-March 07

Re: problem create new EditText in .java

Posted 04 June 2011 - 08:47 AM

Your getting a force close because you are trying to use the myTexts variable before you initialize it.
You should be initializing it like so:
public class main extends Activity {
    /** Called when the activity is first created. */
 
    //initialize a button and a counter
    Button BtnOK;
    Button BtnCalc;
    int counter = 0;
    private ArrayList<EditText> myTexts;
    
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
 
        // setup the layout
        setContentView(R.layout.main);
 	myTexts = new ArrayList<EditText>();// we initialize it here
        // add a click-listener on the button
        BtnOK = (Button) findViewById(R.id.btnOK);
        BtnOK.setonclickListener(new View.onclickListener() {
			
			@Override
			public void onclick(View v) {
				myTexts.clear();
		    	TableLayout table = (TableLayout) findViewById(R.id.TableLayout01); 
		    	table.removeViews(0, counter);
		    	EditText  etxtUser = (EditText)findViewById(R.id.etxtNo);
		    	counter = Integer.parseInt(etxtUser.getText().toString());
		    	
		        for(int i=0;i<counter;i++) {
		                       
		            TableRow row = new TableRow(main.this);
		            TextView t = new TextView(main.this);
		            t.setText("Network " + (i+1));
		            EditText c = new EditText(main.this);
		            row.addView(t);
		            row.addView(c);
		            table.addView(row,new TableLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
		            myTexts.add(c);
		        }
			}
		});
        
        BtnCalc = (Button) findViewById(R.id.btnCalc);
        BtnCalc.setonclickListener(new View.onclickListener() {
			
			@Override
			public void onclick(View v) {
				// TODO Auto-generated method stub
				int a = 0;
		        for(int i=0;i<counter;i++) {
                    
		            a += Integer.parseInt(myTexts.get(i).getText().toString());
		        }
		        TextView TxtvResult = (TextView) findViewById(R.id.txtvResult);
		        TxtvResult.setText(a);
			}
		});
 
    }
}



Was This Post Helpful? 1
  • +
  • -

#7 matyus  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 29
  • Joined: 22-February 09

Re: problem create new EditText in .java

Posted 06 June 2011 - 03:15 PM

thanks..! :clap:
another question again,with same step(arraylist),but editText change to Button,how to code the process when we click the button?we have many buttons in an arraylist,so how do application know which button the user click?this problem also i cannot find in google..aaarghh.. :(
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1