Problème de débutant avec setAdapter
Salut,
je commence à apprendre java sur Androïd et j'aimerai afficher une liste personnalisée. Mon appli plante et je ne trouve pas la raison.
Ca doit être facile pour un pro sachant que je débute vraiment.
Je vous remercie par avance du temps que vous pourriez me consacrer.
liste_tanieres.java
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
| import java.util.ArrayList;
import java.util.List;
import android.app.Activity;
import android.os.Bundle;
import android.widget.ListView;
public class liste_tanieres extends Activity {
ListView lvListe;
List<enigme> liste_enigmes = new ArrayList<enigme>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.liste_tanieres_layout);
lvListe = (ListView)findViewById(R.id.lvListe);
RemplirLalisteDesEnigmes();
EnigmeAdapter adapter = new EnigmeAdapter(this,liste_enigmes);
lvListe.setAdapter(adapter);
adapter.notifyDataSetChanged();
}
private void RemplirLalisteDesEnigmes(){
liste_enigmes.clear();
liste_enigmes.add(new enigme("Enigme 1",1,0));
liste_enigmes.add(new enigme("Enigme 2",2,0));
liste_enigmes.add(new enigme("Enigme 3",3,0));
}
} |
enigme.java qui correspond à l'objet de ma liste à afficher
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
| public class enigme {
private String titre;
private int numero;
private int resolue;
//Constructor
public enigme(String titre, int numero, int resolue){
this.titre = titre;
this.numero = numero;
this.resolue = resolue;
}
//Methods
public String getTitre(){
return titre;
}
public void setTitre(String titre){
this.titre = titre;
}
public int getNumero(){
return numero;
}
public void setNumero(int numero){
this.numero = numero;
}
public int getResolue(){
return resolue;
}
public void setResolue(int resolue){
this.resolue = resolue;
}
} |
EnigmeAdapter
l'erreur est sans doute ici :/
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
| import java.util.List;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
public class EnigmeAdapter extends BaseAdapter {
List<enigme> liste_enigmes;
LayoutInflater inflater;
public EnigmeAdapter(Context context,List<enigme> liste_enigmes) {
inflater = LayoutInflater.from(context);
this.liste_enigmes = liste_enigmes;
}
@Override
public int getCount() {
return liste_enigmes.size();
}
@Override
public Object getItem(int position) {
return liste_enigmes.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
private class ViewHolder {
TextView tvTitre;
TextView tvNumero;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if(convertView == null) {
holder = new ViewHolder();
convertView = inflater.inflate(R.layout.itemenigme, null);
holder.tvTitre = (TextView)convertView.findViewById(R.id.tvTitre);
holder.tvNumero = (TextView)convertView.findViewById(R.id.tvNumero);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.tvTitre.setText(liste_enigmes.get(position).getTitre());
holder.tvNumero.setText(liste_enigmes.get(position).getNumero());
return convertView;
}
} |
itemenigme.xml
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
| <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TextView
android:id="@+id/tvTitre"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/tvNumero"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
<TextView
android:id="@+id/tvResolue"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
</LinearLayout> |
liste_tanieres_layout.xml
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ListView
android:id="@+id/lvListe"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
</ListView>
</LinearLayout> |