3

in the following activity, I create a table and dynamically add buttons to it. The buttons aren't defined in the xml, I want to know in which location was the button (labeled 'b') was clicked. i.e: in which i and j I tried setting jClickable to i but it sets it to the final value as it's clicked after the table's created.

The aFromA, aToA, mobileA, LnameA, FnameA, fullName, aIDa are all string arrays, I've removed their initialization part because it takes a huge block of code, due to the splitting and other operations.

Any help would be greatly appreciated, thanks in advance. Here's the code:

public class ViewAssistants extends Activity implements OnClickListener{


    Button back;
    Button b;

    EditText et;
    Button change;
    TextView text;

    String time;

    int jClicked;

    int i;
    int j;

    UserFunctions userFunction;

     String [] aFromA;
     String [] aToA;
     String [] mobileA;
     String [] LnameA;
     String [] FnameA;
     String [] fullName;
     String [] aIDa;

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.viewassistants);

         text =  (TextView) findViewById(R.id.textView1);




         TableLayout tableLayout = (TableLayout) findViewById(R.id.myTableLayout);

         userFunction = new UserFunctions();
         String DR_ID = userFunction.drID;
         userFunction.viewingAssistants(DR_ID);





         String ifFull =userFunction.full;
         int ifFullint = Integer.parseInt(ifFull);

         if (ifFullint == 1){

        text.setText("");
         TableRow tableRow;
         TextView textView;





         for (i = 0; i < (FnameA.length)+1; i++) {
             tableRow = new TableRow(getApplicationContext());

             for (j = 0; j < 5; j++) {

             if(i==0){
             textView = new TextView(getApplicationContext());
             textView.setBackgroundResource(R.drawable.nm);
             textView.setTextColor(Color.BLACK);
             tableRow.addView(textView);

             switch (j){ 
             case 0:textView.setText(" ID ");break;
             case 1:textView.setText(" Name ");break;
             case 2:textView.setText(" Mobile "); break;
             case 3:textView.setText(" Shift starts "); break;
             case 4:textView.setText(" Ends "); break;
             default: textView.setText("..");
             }

             }
             else
             {

             b = new Button(getApplicationContext());
             b.setBackgroundResource(R.drawable.nm);
             b.setTextColor(Color.BLACK);



             tableRow.addView(b);



             for(int w=0;w<FnameA.length;w++){
                 fullName[w]= FnameA[w]+" "+LnameA[w];
             }

             switch (j){ 
             case 0:b.setText(""+aIDa[i-1]+"");break;
             case 1:b.setText(""+fullName[i-1]+"");break;
             case 2:b.setText(""+mobileA[i-1]+""); break;
             case 3:b.setText(""+aFromA[i-1]+"");  b.setOnClickListener(this);break;
             case 4:b.setText(""+aToA[i-1]+"");b.setOnClickListener(this);break;
            default: b.setText("..");
             }

             }
             }
             tableLayout.addView(tableRow);



             } 
         }
         else{

             text.setText("NO PATIENTS");
             System.out.println("fel elseeee");
         }


        back = (Button) findViewById(R.id.back);
        back.setOnClickListener(new View.OnClickListener() {


            @Override
            public void onClick(View view) {
                // TODO Auto-generated method stub


                Intent dashboard = new Intent(getApplicationContext(), loginAsDr.class);
                // Close all views before launching Dashboard
                dashboard.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                startActivity(dashboard);
                // Close Registration Screen
                finish();

            }

        });



    }

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub

        et=(EditText)findViewById(R.id.shiftChange); 
        et.setVisibility(View.VISIBLE);


        change=(Button) findViewById(R.id.change);
        change.setVisibility(View.VISIBLE);


        change.setOnClickListener(new View.OnClickListener() {


            @Override
            public void onClick(View view) {
                // TODO Auto-generated method stub



                jClicked=i;
                time=et.getText().toString();

                et.setText(" ");
                Log.d("TIME IS",time+"  "+jClicked);

            }

        });
    }
}

and here's the xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >
<HorizontalScrollView
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="#3b3b3b" >
<TableLayout
    android:id="@+id/myTableLayout"
    android:layout_width="match_parent"
    android:layout_height="390dp" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="NO DOCTORS" />

</TableLayout>
</HorizontalScrollView>  

<Button
    android:id="@+id/back"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentRight="true"
    android:layout_marginBottom="40dp"
    android:text="BACK" />


<EditText
    android:id="@+id/shiftChange"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_alignParentLeft="true"
    android:visibility = "gone"
    android:ems="10" />

<Button
    android:id="@+id/change"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_toRightOf="@+id/shiftChange"
     android:visibility = "gone"
    android:text="Change please" />

</RelativeLayout>

Sorry if the code is a tad long. Thanks in advance.

Salma
  • 113
  • 3
  • 9
  • Can't you set your own id's at runtime..? or **setTag()** can come in handy.. – ngesh Mar 21 '13 at 10:37
  • How do I setTag() and what does it do exactly? I'm a bit new to this.. – Salma Mar 21 '13 at 10:39
  • 1
    **setTag()** is used to keep something attached to a view.. it can be anything (i mean Object or int).. Later when you need it you say **getTag()** with the corresponding view object.. see http://stackoverflow.com/questions/5291726/what-is-the-main-purpose-of-settag-gettag-methods-of-view – ngesh Mar 21 '13 at 10:45
  • Okay thank you, I'll give it a go now and see if it works out. – Salma Mar 21 '13 at 10:49

1 Answers1

1

Using setTag would be your best bet. You can store nearly any data structure inside of it. Here, I'd recommend a simple Vector that stores your i's and j's, i.e.:

Vector<Integer> v = new Vector<Integer>();
v.add(i);
v.add(j);
b.setTag(v);

Then in your on click method:

public void onClick(View v) {
    Vector<Integer> v = (Vector<Integer>)v.getTag();
    int i = v.get(0);
    int j = v.get(1);
}
Zaid Daghestani
  • 8,555
  • 3
  • 34
  • 44