2010-11-27 7 views
1

J'ai un TableLayout dans lequel j'ajoute des lignes dynamiquement, vide au début et je veux charger un xml dans cette ligne quand l'utilisateur clique dessus.Inflate a TableRow

J'ai assigné la méthode OnClick à la ligne, mais je ne sais pas comment charger le xml quand il entre dans la méthode onclick.

public void onClick(View v){ 
// CODE TO LOAD THE XML 

Toast.makeText(getApplicationContext(), "This should appear", Toast.LENGTH_SHORT).show(); 
} 

J'ai essayé d'utiliser gonflent, mais il semble que je ne l'utilise pas correctement parce que les plantages d'applications en cliquant sur une ligne.

Comment est-ce que je pourrais faire la rangée pour contenir le xml?

Répondre

0

Trouvez le TextView dans le TableRow par ID, puis définissez le code XML sur TextView.

8

Définissez d'abord la balise de tous les TableRow lorsque vous ajoutez initialement à TableLayout. puisque vous avez chargé le TableRow dynamique, je l'espère, qui vient de gonfler, comme le

suivant
TableRow inflateRow = (TableRow) View.inflate(Activity, R.layout.table_row, null); 

//Main TableLayout 

TableLayout tblAddLayout = (TableLayout) findViewById(R.id.tblAddLayout); 


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

    inflate = (TableRow) View.inflate(myActivity, R.layout.table_row, null); 
    //set tag for each TableRow 
    inflate.setTag(i); 
    //add TableRows to TableLayout 
    tblAddLayout.addView(inflate); 
    //set click listener for all TableRows 
    inflateRow.setOnClickListener(this); 
} 

public void onClick(View v){ 

    String strTag = v.getTag().toString(); 
// Find the corresponding TableRow from the Main TableLayout using the tag when you click. 

    TableRow tempTableRow = (TableRow)tblAddLayout.findViewWithTag(strTag); 
    //then add your layout in this TableRow tempTableRow. 
} 

Dans la méthode onClick ci-dessus, vous pouvez charger le fichier XML en utilisant la balise.

Questions connexes