aszur
17/11/14, 21:21:48
Buenas, pues llevo ya una semanita que este tema me esta dando muchisimo la vara y no me funciona. Lo que quiero es tener varias pestañas en las cuales se muestre una lista de elementos con sus fotos, descripcion, etc. Los ListViews me funcionan correctamente en el momento que suprimo las pestañas, entonces ya no se si es posible juntar ambas cosas o qué narices pasa pero la lista no se muestra en las pestañas. Creo que el mayor problema esta en que la generación de esta lista no esta en la main.java sino en otro .java. Pero tampoco se me ocurre como implementarlo si no es de ese modo. Os enseño el código de lo que corresponde a una de las pestañas, porque el resto esta igual pero con otro objeto xml para mostrar en la lista y con otro adapter.
En primer lugar tenemos la clase evento ya que la lista esta compuesta por eventos.
package com.example.pestanasholacampus;
import android.graphics.drawable.*;
public class Evento {
protected Drawable foto;
protected String nombreEvento;
protected String fecha;
protected String lugar;
protected int id;
public Evento(Drawable foto, String nombreEvento, String fecha, String lugar){
super();
this.nombreEvento= nombreEvento;
this.fecha= fecha;
this.lugar= lugar;
this.foto= foto;
}
/*public Evento(Drawable foto, String nombreEvento, String fecha, String lugar, int id){
super();
this.nombreEvento= nombreEvento;
this.fecha= fecha;
this.lugar= lugar;
this.foto= foto;
this.setId(id);
}*/
public String getNombreEvento() {
return nombreEvento;
}
public void setNombreEvento(String nombreEvento) {
this.nombreEvento = nombreEvento;
}
public String getFecha() {
return fecha;
}
public void setFecha(String fecha) {
this.fecha = fecha;
}
public String getLugar() {
return lugar;
}
public void setLugar(String lugar) {
this.lugar = lugar;
}
public Drawable getFoto() {
return foto;
}
public void setFoto(Drawable foto) {
this.foto = foto;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
}
A continuación, el adapter del evento:
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) {
LayoutInflater inf=(LayoutInflater)activity.getSystemService(Cont ext.LAYOUT_INFLATER_SERVICE);
View v= inf.inflate(R.layout.eventolist,null);
//Asociar el layout de la lista que hemos creado.
//Definimos un objeto a partir del array, vamos a cargar el contenido
//de ese objeto en el view de la lista.
Evento even= eventos.get(position);
//Cargamos la fotografía del evento.
ImageView foto= (ImageView)v.findViewById(R.id.fotoEvento);
foto.setImageDrawable(even.getFoto());
//Cargamos el nombre del evento
TextView nombre= (TextView)v.findViewById(R.id.nombreEvento);
nombre.setText(even.getNombreEvento());
//Cargamos el lugar del evento.
TextView lugar= (TextView)v.findViewById(R.id.lugarEvento);
lugar.setText(even.getLugar());
//Cargamos la fecha del evento
TextView fecha= (TextView)v.findViewById(R.id.fechaEvento);
fecha.setText(even.getFecha());
//Se devuelve la view cargada
return v;
}
}
Estas dos partes estoy seguro que funcionan porque las listas las generan bien.
Esta es la clase que se encarga de rellenar la lista, en un futuro deberá cargar los datos de una base de datos, pero para empezar, debería mostrarse :rolleyes:
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.confe rencia),"Nuevos metodos de programacion","Aula Magna","20/12/2014-18:00");
arrayEventos.add(evento);
System.out.println("Evento 1 cargado:"+ arrayEventos.get(0).getNombreEvento());
evento= new Evento(getResources().getDrawable(R.drawable.playi ta),"Dia en la playa","Muskiz","20/8/2015-10:00");
arrayEventos.add(evento);
System.out.println("Evento 2 cargado.");
evento= new Evento(getResources().getDrawable(R.drawable.fever night),"Sabado Noche","Fever","29/11/2014-22:00");
arrayEventos.add(evento);
System.out.println("Evento 3 cargado.");
// Creamos el adapter
EventoAdapter adapter= new EventoAdapter(this,arrayEventos);
// Una vez hecha la conexión pasamos los datos.
lista.setAdapter(adapter);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
Aqui tenemos el xml que contenedora de los tabs y de sus layouts:
<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"
android:orientation="vertical"
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/tab2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".Pestana_eventos" >
<ListView
android:id="@+id/listaEventos"
android:layout_width="match_parent"
android:layout_height="wrap_content"
tools:listitem="@layout/eventolist" >
</ListView>
</LinearLayout>
<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/tab3"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".Pestana_amigos" >
<ListView
android:id="@+id/listaAmigos"
android:layout_width="match_parent"
android:layout_height="wrap_content"
tools:listitem="@layout/amigolist" >
</ListView>
</LinearLayout>
</FrameLayout>
</LinearLayout>
</TabHost>
</LinearLayout>
Y finalmente la mainActivity, que debería ser una actividad con varias pestañas(que se muestran) y cada actividad con su conenido (las listas no se muestran).
package com.example.pestanasholacampus;
import android.app.Activity;
import android.app.TabActivity;
import android.content.Intent;
import android.content.res.Resources;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.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);
System.out.println("1 tab cargada");
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);
System.out.println("2 tab cargada");
contenedor.setup();
TabHost.TabSpec spec3 = contenedor.newTabSpec("pestana_amigos");
spec3.setContent(R.id.tab3);
spec3.setIndicator("",res.getDrawable(R.drawable.ic_tab_amigos));
contenedor.addTab(spec3);
System.out.println("3 tab cargada");
contenedor.setCurrentTab(0);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
El codigo completo esta en esta URL:
https://www.dropbox.com/s/n0pc1ksyhfhaj7w/pestanasHolaCampus.rar?dl=0
Os agradecería muchisimo al menos una pista de si esto se puede hacer así o es una burrada.
Gracias
En primer lugar tenemos la clase evento ya que la lista esta compuesta por eventos.
package com.example.pestanasholacampus;
import android.graphics.drawable.*;
public class Evento {
protected Drawable foto;
protected String nombreEvento;
protected String fecha;
protected String lugar;
protected int id;
public Evento(Drawable foto, String nombreEvento, String fecha, String lugar){
super();
this.nombreEvento= nombreEvento;
this.fecha= fecha;
this.lugar= lugar;
this.foto= foto;
}
/*public Evento(Drawable foto, String nombreEvento, String fecha, String lugar, int id){
super();
this.nombreEvento= nombreEvento;
this.fecha= fecha;
this.lugar= lugar;
this.foto= foto;
this.setId(id);
}*/
public String getNombreEvento() {
return nombreEvento;
}
public void setNombreEvento(String nombreEvento) {
this.nombreEvento = nombreEvento;
}
public String getFecha() {
return fecha;
}
public void setFecha(String fecha) {
this.fecha = fecha;
}
public String getLugar() {
return lugar;
}
public void setLugar(String lugar) {
this.lugar = lugar;
}
public Drawable getFoto() {
return foto;
}
public void setFoto(Drawable foto) {
this.foto = foto;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
}
A continuación, el adapter del evento:
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) {
LayoutInflater inf=(LayoutInflater)activity.getSystemService(Cont ext.LAYOUT_INFLATER_SERVICE);
View v= inf.inflate(R.layout.eventolist,null);
//Asociar el layout de la lista que hemos creado.
//Definimos un objeto a partir del array, vamos a cargar el contenido
//de ese objeto en el view de la lista.
Evento even= eventos.get(position);
//Cargamos la fotografía del evento.
ImageView foto= (ImageView)v.findViewById(R.id.fotoEvento);
foto.setImageDrawable(even.getFoto());
//Cargamos el nombre del evento
TextView nombre= (TextView)v.findViewById(R.id.nombreEvento);
nombre.setText(even.getNombreEvento());
//Cargamos el lugar del evento.
TextView lugar= (TextView)v.findViewById(R.id.lugarEvento);
lugar.setText(even.getLugar());
//Cargamos la fecha del evento
TextView fecha= (TextView)v.findViewById(R.id.fechaEvento);
fecha.setText(even.getFecha());
//Se devuelve la view cargada
return v;
}
}
Estas dos partes estoy seguro que funcionan porque las listas las generan bien.
Esta es la clase que se encarga de rellenar la lista, en un futuro deberá cargar los datos de una base de datos, pero para empezar, debería mostrarse :rolleyes:
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.confe rencia),"Nuevos metodos de programacion","Aula Magna","20/12/2014-18:00");
arrayEventos.add(evento);
System.out.println("Evento 1 cargado:"+ arrayEventos.get(0).getNombreEvento());
evento= new Evento(getResources().getDrawable(R.drawable.playi ta),"Dia en la playa","Muskiz","20/8/2015-10:00");
arrayEventos.add(evento);
System.out.println("Evento 2 cargado.");
evento= new Evento(getResources().getDrawable(R.drawable.fever night),"Sabado Noche","Fever","29/11/2014-22:00");
arrayEventos.add(evento);
System.out.println("Evento 3 cargado.");
// Creamos el adapter
EventoAdapter adapter= new EventoAdapter(this,arrayEventos);
// Una vez hecha la conexión pasamos los datos.
lista.setAdapter(adapter);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
Aqui tenemos el xml que contenedora de los tabs y de sus layouts:
<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"
android:orientation="vertical"
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/tab2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".Pestana_eventos" >
<ListView
android:id="@+id/listaEventos"
android:layout_width="match_parent"
android:layout_height="wrap_content"
tools:listitem="@layout/eventolist" >
</ListView>
</LinearLayout>
<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/tab3"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".Pestana_amigos" >
<ListView
android:id="@+id/listaAmigos"
android:layout_width="match_parent"
android:layout_height="wrap_content"
tools:listitem="@layout/amigolist" >
</ListView>
</LinearLayout>
</FrameLayout>
</LinearLayout>
</TabHost>
</LinearLayout>
Y finalmente la mainActivity, que debería ser una actividad con varias pestañas(que se muestran) y cada actividad con su conenido (las listas no se muestran).
package com.example.pestanasholacampus;
import android.app.Activity;
import android.app.TabActivity;
import android.content.Intent;
import android.content.res.Resources;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.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);
System.out.println("1 tab cargada");
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);
System.out.println("2 tab cargada");
contenedor.setup();
TabHost.TabSpec spec3 = contenedor.newTabSpec("pestana_amigos");
spec3.setContent(R.id.tab3);
spec3.setIndicator("",res.getDrawable(R.drawable.ic_tab_amigos));
contenedor.addTab(spec3);
System.out.println("3 tab cargada");
contenedor.setCurrentTab(0);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
El codigo completo esta en esta URL:
https://www.dropbox.com/s/n0pc1ksyhfhaj7w/pestanasHolaCampus.rar?dl=0
Os agradecería muchisimo al menos una pista de si esto se puede hacer así o es una burrada.
Gracias