Saltar para conteúdo


Foto
- - - - -

[Dúvida] Gerar TextView dinamicamente


  • Por favor inicie sessão para responder
Não há respostas a este tópico.

#1 pedromcosta

pedromcosta

    Membro

  • Membros
  • PipPip
  • 34 mensagens

Mensagem publicada 25 March 2011 - 16:19

Boas,Eu passo a explicar a minha dúvida, porque pode vir a ser a de mais alguém daqui a uns tempos.Eu tenho este layout definido para mostrar os contactos de uma pessoa, porque existe sempre um contacto:Imagem colocadaMas eu quero que caso haja mais do que 1 contacto (no caso de haver o nr de telemovel e o nr de casa por exemplo) que o layout do segundo ou do terceiro contacto seja gerado dinamicamente.assim(com os 2 e 3 gerados dinamicamente):Imagem colocadaO meu problema é que se não tentar usar o layout já existente e criar tudo dinamicamente (scrollView, LinearLayout, RelativeLayout) os dados são mostrados como eu quero(igual à imagem de cima, mas caso queira adicionar o layout criado dinamicamente ao já existente dá sempre excepção.O que é que estou a fazer mal ?O meu códigocontacto.javapackage dreamincode.tutorials.dic_tut3;import android.app.Activity;import android.os.Bundle;import android.widget.RelativeLayout;import android.widget.ScrollView;import android.widget.LinearLayout;import android.widget.TextView;public class dic_tut3 extends Activity { int id = 1; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); //ScrollView sv = new ScrollView(this); ScrollView sv = (ScrollView) findViewById(R.id.scrollView); //LinearLayout ll = new LinearLayout(this); LinearLayout ll = (LinearLayout) findViewById(R.id.linearLayout); ll.setOrientation(LinearLayout.VERTICAL); RelativeLayout layout = new RelativeLayout(this); ll.addView(layout); sv.addView(ll); TextView tv = new TextView(this); tv.setId(id); tv.setText("Contactos Adicionais"); layout.addView(tv); RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams( RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.FILL_PARENT); TextView tv1 = new TextView(this); tv1.setId(++id); tv1.setText("Contactos: "); lp.addRule(RelativeLayout.BELOW, tv.getId()); layout.addView(tv1, lp); RelativeLayout.LayoutParams lp2 = new RelativeLayout.LayoutParams( RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.FILL_PARENT); TextView tv2 = new TextView(this); tv2.setId(++id); tv2.setText("912785535"); lp2.addRule(RelativeLayout.RIGHT_OF, tv1.getId()); lp2.addRule(RelativeLayout.BELOW, tv.getId()); layout.addView(tv2, lp2); RelativeLayout.LayoutParams lp3 = new RelativeLayout.LayoutParams( RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.FILL_PARENT); TextView tv3 = new TextView(this); tv3.setId(++id); tv3.setText("Tipo: "); lp3.addRule(RelativeLayout.BELOW, tv2.getId()); layout.addView(tv3, lp3); RelativeLayout.LayoutParams lp4 = new RelativeLayout.LayoutParams( RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.FILL_PARENT); TextView tv4 = new TextView(this); tv4.setId(++id); tv4.setText("Telefone"); lp4.addRule(RelativeLayout.RIGHT_OF, tv3.getId()); lp4.addRule(RelativeLayout.BELOW, tv2.getId()); layout.addView(tv4, lp4); RelativeLayout.LayoutParams lp5 = new RelativeLayout.LayoutParams( RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.FILL_PARENT); TextView tv5 = new TextView(this); tv5.setId(++id); tv5.setText("Observações: "); lp5.addRule(RelativeLayout.BELOW, tv4.getId()); layout.addView(tv5, lp5); RelativeLayout.LayoutParams lp6 = new RelativeLayout.LayoutParams( RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.FILL_PARENT); TextView tv6 = new TextView(this); tv6.setId(++id); tv6.setText(""); lp6.addRule(RelativeLayout.BELOW, tv4.getId()); lp6.addRule(RelativeLayout.RIGHT_OF, tv5.getId()); layout.addView(tv6, lp6); this.setContentView(sv); }}contacto.xml