2014-11-14 3 views
1

Я новый программист для Android и хочу сделать несколько вкладок, которые имеют некоторые listviews. Приложение работает, но вкладка, которая должна показывать listview, не делает этого. Это код:ListView на вкладках не работает

main_menu.xml мой tabhost и MainActivity.java моя основная деятельность:

<LinearLayout 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" 
tools:context=".Pestana_eventos" > 

<TabHost 
    android:id="@android:id/tabhost" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" > 

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:orientation="vertical" > 

     <TabWidget 
      android:id="@android:id/tabs" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" /> 

     <FrameLayout 
      android:id="@android:id/tabcontent" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" > 
      <LinearLayout 
       android:id="@+id/tab1" 
       android:layout_width="match_parent" 
       android:layout_height="match_parent" 
       android:orientation="vertical" > 
       <TextView 
        android:id="@+id/bienvenida2" 
        android:layout_width="match_parent" 
        android:layout_height="match_parent" 
        android:text="Pestaña de perfil ." /> 
      </LinearLayout> 
      <LinearLayout 
       android:id="@+id/tab2" 
       android:layout_width="match_parent" 
       android:layout_height="match_parent" 
       android:orientation="vertical" > 

       <ListView 
        android:id="@+id/listaEventos" 
        android:layout_width="match_parent" 
        android:layout_height="wrap_content" 
        tools:listitem="@layout/eventolist" > 
       </ListView> 
      </LinearLayout> 

     </FrameLayout> 
    </LinearLayout> 
</TabHost> 

и активность:

public class MainActivity extends Activity { 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main_menu); 

    Resources res= getResources(); 
    TabHost contenedor= (TabHost)findViewById(android.R.id.tabhost); 
    contenedor.setup(); 

    //Intent intent= new Intent().setClass(this, Pestana_perfil.class); // Cogemos la información de la clase asociada con la pestaña 
    TabHost.TabSpec spec= contenedor.newTabSpec("pestana_perfil"); 
    spec.setContent(R.id.tab1); 
    spec.setIndicator("",res.getDrawable(R.drawable.ic_tab_perfil)); 
    contenedor.addTab(spec); 

    contenedor.setup(); 
    //intent= new Intent().setClass(this, Pestana_eventos.class); 
    TabHost.TabSpec spec2 = contenedor.newTabSpec("pestana_eventos"); 
    spec2.setContent(R.id.tab2); 
    spec2.setIndicator("",res.getDrawable(R.drawable.ic_tab_eventos)); 
    contenedor.addTab(spec2); 


    contenedor.setCurrentTab(0); 
} 

Следующая класс генерируют arrayList, которые должны отображаться на второй вкладке.

public class Pestana_eventos extends Activity { 
public void onCreate(Bundle savedInstanceState){ //Enlazamos los .java con los xml 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main_menu); 

    ListView lista= (ListView) findViewById(R.id.listaEventos); 
    ArrayList<Evento> arrayEventos= new ArrayList<Evento>(); 
    Evento evento; 

    //Introduccion de datos en el array. Aqui es donde se deben cargar los datos de la BB.DD 
    evento= new Evento(getResources().getDrawable(R.drawable.conferencia),"Nuevos metodos de programacion","Aula Magna","20/12/2014-18:00"); 
    arrayEventos.add(evento); 
    evento= new Evento(getResources().getDrawable(R.drawable.playita),"Dia en la playa","Muskiz","20/8/2015-10:00"); 
    arrayEventos.add(evento); 
    evento= new Evento(getResources().getDrawable(R.drawable.fevernight),"Sabado Noche","Fever","29/11/2014-22:00"); 
    arrayEventos.add(evento); 
    // Creamos el adapter 
    EventoAdapter adapter= new EventoAdapter(this,arrayEventos); 
    // Una vez hecha la conexión pasamos los datos. 
    lista.setAdapter(adapter); 
} 

И наконец адаптер:

@SuppressLint("InflateParams") 
public class EventoAdapter extends BaseAdapter { 

protected Activity activity; 
protected ArrayList<Evento> eventos; 

public EventoAdapter(Activity activity, ArrayList<Evento> eventos){ 
    this.activity= activity; 
    this.eventos= eventos; 
} 

@Override 
public int getCount() { 
    return eventos.size(); 
} 

@Override 
public Object getItem(int position) { 
    return eventos.get(position); 
} 

@Override 
public long getItemId(int position) { 
    return eventos.get(position).getId(); 
} 

@Override 
public View getView(int position, View convertView, ViewGroup parent) { 
    //Mejorar eficiencia con el convertView 
    View v= convertView; 
    //Asociar el layout de la lista que hemos creado. 
    if(convertView == null){ 
     LayoutInflater inf= (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
     v=inf.inflate(R.layout.eventolist,null); 
    } 
    //Definimos un objeto a partir del array, vamos a cargar el contenido 
    //de ese objeto en el view de la lista. 
    Evento evento= eventos.get(position); 
    //Cargamos la fotografía del evento. 
    ImageView foto= (ImageView)v.findViewById(R.id.fotoEvento); 
    foto.setImageDrawable(evento.getFoto()); 
    //Cargamos el nombre del evento 
    TextView nombre= (TextView)v.findViewById(R.id.nombreEvento); 
    nombre.setText(evento.nombreEvento); 
    //Cargamos el lugar del evento. 
    TextView lugar= (TextView)v.findViewById(R.id.lugarEvento); 
    lugar.setText(evento.lugar); 
    //Cargamos la fecha del evento 
    TextView fecha= (TextView)v.findViewById(R.id.fechaEvento); 
    fecha.setText(evento.fecha); 
    //Se devuelve la view cargada 
    return v; 

} 

В tabhost работает хорошо, но listView.

Надеюсь, вы можете мне помочь, спасибо большое.

+0

y can not use use? –

+0

2 линейных макета внутри рамочной компоновки имеют соответствующее значение для высоты. –

+0

Да, потому что каждый макет - одна вкладка. Это неправильно? – Asier

ответ

0

Хорошо, я нашел проблему. Pestana_eventos и Pestana_amigos не являются действиями. Списки не показывались, потому что у них не было предметов. Код, который должен был установить элементы, не вызывался.

+0

Да, в этом была проблема. Спасибо вам! – Asier

Смежные вопросы