hesham
hesham

Reputation: 33

getting ids of items in xml layout

i have row that's contain check box and 2 text view

I'm using this code to repeat custom xml layout it's working ok

but i want to know each id for each component in row i mean ( id for check box and edit 1 and id edit2 )

so i can work with them .

i need for loop also to create this instances for ids .

to make my Q clear i want like following :

for ( int i = 0 ; i < 60 ; i++ ){

CheckBox "auto name here " = new (CheckBox) findViewById(R.id."the id of the current item in rep.xml layout or row ");

"auto name here".setChecked(true);

and sooo on ...

my code for creating row in for loop :

for (int i = 0; i<60 ; i++ ){

LayoutInflater  inflater =    (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = (View) inflater.inflate(R.layout.rep, null);

tl.addView(view); // tl is table-layout instance 
}

Upvotes: 1

Views: 89

Answers (2)

Lisa Anne
Lisa Anne

Reputation: 4595

EDITED EDITED

package com.example.help;

import android.os.Bundle;
import android.app.Activity;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.widget.CheckBox;

public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    CheckBox[] cb=new CheckBox[60];

    for (int i = 0; i<60 ; i++ ){

    LayoutInflater  inflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View view = (View) inflater.inflate(R.layout.rep, null);

    tl.addView(view); // tl is table-layout instance 
    cb[i]=(CheckBox)view.fidViewById(R.id.CheckBoxid);
    cb[i].setChecked(true);



    }
}

Upvotes: 0

FoamyGuy
FoamyGuy

Reputation: 46856

You should not be using a loop and a TableLayout if you need to add 60 rows.

You should instead be using an AdapterView with an Adapter to hold your data and inflate rows for you. One of the many benifits that doing it this way affords you is being able to declare what will happen when the check boxes get checked independently in each row. Another is that you will get MUCH better performance because you won't ever have all 60 rows in memory at once, because of convertView recycling.

Go here: http://developer.android.com/guide/topics/ui/binding.html for a high level intro to AdapterViews.

Study the examples for ListView and GridView. Once you've got them down adapt some of the sample code to inflate your own rows instead of the sample ones given.

Upvotes: 1

Related Questions