Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: Need Help with Referencing Dynamically Created Inputs

  1. #1
    Junior Member
    Join Date
    Oct 2012
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Need Help with Referencing Dynamically Created Inputs

    Hello everyone, I am trying to build an android app that will allow the user to create two matrices (A and B) and then perform some operation to get Matrix C IE (A+B=C). The code down below pulls the size of the two arrays from another class. The Class then creates Input boxes for the respective size of the matrix. The problem I am having is referencing the dynamically created matrices so that when the user hits a button Array C can pull values from the created input boxes. If my question is unclear please comment, and I can try to clarify as best as I can. My code is below.

    package zach.etier.osu.MC;
     
    import android.app.Activity;
    import android.content.DialogInterface;
    import android.os.Bundle;
    import android.view.Gravity;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.view.ViewGroup.LayoutParams;
    import android.widget.Button;
    import android.widget.EditText;
    import android.widget.LinearLayout;
    import android.widget.TableLayout;
    import android.widget.TableRow;
     
     
    public class  Matrix  extends Activity implements OnClickListener  {
    LinearLayout A,B,C,Container;
    String gotBread,gotBread2,gotBread3,gotBread4;
    int Arow,Acol,Brow,Bcol;
    Button infocalc;
    TableRow rowA,rowB;
    String[]  myTextViewsA, myTextViewsB;   
     
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.home);
        infocalc=(Button)findViewById(R.id.calc);
        infocalc.setOnClickListener(this);
        A=(LinearLayout)findViewById(R.id.arrayA);
        B=(LinearLayout)findViewById(R.id.arrayB);
        C=(LinearLayout)findViewById(R.id.arrayC);
        Container=(LinearLayout)findViewById(R.id.Container);
        Container.setVisibility(View.INVISIBLE);
        Bundle gotBasket =getIntent().getExtras();
        gotBread=gotBasket.getString("key");
        Arow=Integer.parseInt(gotBread);
        gotBread2=gotBasket.getString("key2");
        Acol=Integer.parseInt(gotBread2);
        gotBread3=gotBasket.getString("key3");
        Brow=Integer.parseInt(gotBread3);
        gotBread4=gotBasket.getString("key4");
        Bcol=Integer.parseInt(gotBread4);
     
        final EditText[] myTextViewsA = new EditText[Arow];
        for (int i = 0; i < Arow; i++) {
            TableRow rowA = new TableRow(this);
            rowA.setId(i);
            for (int j = 0; j < Acol; j++) {
                EditText cell = new EditText(this);
                cell.setHint("(" + i + ", " + j + ")");
                rowA.addView(cell);
                myTextViewsA[j] = cell;
            }
            A.addView(rowA, new TableLayout.LayoutParams(
                    LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT));
                    rowA.setGravity(Gravity.CENTER);
        }
     
        final EditText[] myTextViewsB = new EditText[Brow];
        for (int i = 0; i < Brow; i++) {
            TableRow rowB = new TableRow(this); 
            rowB.setId(i);
            for (int j = 0; j < Bcol; j++) {
                EditText cell = new EditText(this);
                cell.setHint("(" + i + ", " + j + ")");
                rowB.addView(cell);
                myTextViewsB[i] = cell;
            }
            B.addView(rowB, new TableLayout.LayoutParams(
                    LayoutParams.FILL_PARENT,
                    LayoutParams.WRAP_CONTENT));
                    rowB.setGravity(Gravity.CENTER);
        }
        Container.setVisibility(View.VISIBLE);
     
    }
        public void onClick(View v) {
            // TODO Auto-generated method stub  
            final EditText[] myTextViewsC = new EditText[Arow];
            for (int i = 0; i < Arow; i++) {
                TableRow rowC = new TableRow(this);      
                for (int j = 0; j < Acol; j++) {
                    EditText cell = new EditText(this);
                    cell.setText("(" + i + ", " + j + ")");
                    rowC.addView(cell);
                }
                C.addView(rowC, new TableLayout.LayoutParams(
                        LayoutParams.FILL_PARENT,
                        LayoutParams.WRAP_CONTENT));
                        rowC.setGravity(Gravity.CENTER);
        }
    }
    }
    Last edited by jps; November 1st, 2012 at 04:37 AM. Reason: added code tags


  2. #2
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: Need Help with Referencing Dynamically Created Inputs

    Hi and welcome.

    Please see the announcements page for the use of code tags when posting code.


    If my question is unclear please comment,
    Your question is unclear. Does the code compile and run? Post any error messages you may be getting. Explain what the code does do if it does run, and explain what you are stuck on. Trouble with the listeners? Trouble with passing information and if so from where to where? Trouble getting values from input boxes? Giving great details about exactly where you are having problems makes it much easier to browse the code and offer a suggestion.

  3. #3
    Junior Member
    Join Date
    Oct 2012
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Need Help with Referencing Dynamically Created Inputs

    Quote Originally Posted by jps View Post
    Your question is unclear. Does the code compile and run? Post any error messages you may be getting. Explain what the code does do if it does run, and explain what you are stuck on. Trouble with the listeners? Trouble with passing information and if so from where to where? Trouble getting values from input boxes? Giving great details about exactly where you are having problems makes it much easier to browse the code and offer a suggestion.
    Thank you jps, I am sorry for the unclear question I will try to elaborate. The code I posted will compile and run, however if you copy and paste this code it will not run. This is because I am importing the variables Arow,Acol,Brow,Bcol from another class. Because the code will run for me there are no error logs. My problem is that once the edit texts for the two arrays are created I'm not sure how to 1) Set there ID and 2) call there ID to get there values later in the program.

  4. #4
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: Need Help with Referencing Dynamically Created Inputs

    You can set the id through java.
    In eclipse you can easily set the id of an element with the properties window.
    You can also edit the xml directly as a text file or by using the tool that comes with the ADK.

    You can use View.getid() to get the id. Seriously read the entire page linked to here. There is a wealth of information on the long page, but read it. Not just what is on that page but the related links give a great idea of other possibilities you may not be aware exists.

    On a side note, anyone learning android should pick one semi-random page from the docs to read every day.

Similar Threads

  1. Referencing values from ValidationMessages.properties
    By tarkal in forum JavaServer Pages: JSP & JSTL
    Replies: 1
    Last Post: March 11th, 2012, 01:24 PM
  2. [SOLVED] Referencing to Method. Incompatible Types Error.
    By mwebb in forum Object Oriented Programming
    Replies: 12
    Last Post: February 5th, 2012, 02:50 PM
  3. my pointer's value is the same when referencing
    By jack_nutt in forum What's Wrong With My Code?
    Replies: 14
    Last Post: July 6th, 2011, 03:24 PM
  4. Excel Column Letter Referencing
    By aussiemcgr in forum What's Wrong With My Code?
    Replies: 3
    Last Post: July 21st, 2010, 03:47 PM