Je me met doucement à la programmation pour android et pour un de mes test, où j'essaye d'afficher des notes sous forme de liste, l'application plante au démarrage.
J'ai :
-une classe Note qui décris une note
-un fichier note_layout.xml qui décrit comment une note seule doit s'afficher
-un fichier liste_note.xml qui décrit l'affichage d'une liste de note (actuellement c'est le layout principal)
-une classe NoteAdapter
-l'activity principale
A l'exécution, le processus plante, a priori lors de l'appel à list.setAdapter(adapter) de LazyStudentActivity.java, est ce que quelqu'un pourrais m'expliquer ce que je fait de mal? merci
<?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/ListView01"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</ListView>
<!-- le bouton pour rajouter une note au listing -->
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="50dp"
android:text="@string/boutonAjoutNote"
/>
</LinearLayout>
package lazy.student;
import java.util.ArrayList;
public class Note {
private String nomNote;
private int coeffNote;
private double resNote;
private boolean tmp;
public Note(String nom, int coeff, double res, boolean tmp){
this.nomNote=nom;
this.coeffNote=coeff;
this.resNote=res;
this.tmp=tmp;
}
public Note(String nom, int coeff, double res){
this.nomNote=nom;
this.coeffNote=coeff;
this.resNote=res;
this.tmp=false;
}
public String getNomNote(){
return this.nomNote;
}
public int getCoeffNote(){
return this.coeffNote;
}
public double getResNote(){
return this.resNote;
}
public boolean isTmp(){
return this.tmp;
}
public static ArrayList<Note> getAListOfNotes(){
ArrayList<Note> listeNote = new ArrayList<Note>();
listeNote.add(new Note("note1", 3, 12.5));
listeNote.add(new Note("note2", 2, 10.5));
listeNote.add(new Note("note3", 1, 1.5));
return listeNote;
}
}
package lazy.student;
import java.util.ArrayList;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;
public class NoteAdapter extends BaseAdapter {
private ArrayList<Note> listeNote;
private Context mContext;
private LayoutInflater mInflater;
public NoteAdapter(Context context, ArrayList<Note> aListP) {
mContext = context;
listeNote = aListP;
mInflater = LayoutInflater.from(mContext);
}
@Override
public int getCount() {
return listeNote.size();
}
@Override
public Object getItem(int position) {
return listeNote.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
LinearLayout layoutItem;
//(1) : Réutilisation des layouts
if (convertView == null) {
//Initialisation de notre item à partir du layout XML "personne_layout.xml"
layoutItem = (LinearLayout) mInflater.inflate(R.layout.note_layout, parent, false);
} else {
layoutItem = (LinearLayout) convertView;
}
//(2) : Récupération des TextView de notre layout
TextView tv_nom = (TextView)layoutItem.findViewById(R.id.nomNote);
TextView tv_coeff = (TextView)layoutItem.findViewById(R.id.coeffNote);
TextView tv_note = (TextView)layoutItem.findViewById(R.id.resNote);
//(3) : Renseignement des valeurs
tv_nom.setText(listeNote.get(position).getNomNote());
tv_coeff.setText(listeNote.get(position).getCoeffNote());
tv_note.setText(""+listeNote.get(position).getResNote());
/*
//(4) Changement de la couleur du fond de notre item
if (mListP.get(position).genre == Personne.MASCULIN) {
layoutItem.setBackgroundColor(Color.BLUE);
} else {
layoutItem.setBackgroundColor(Color.MAGENTA);
}*/
//On retourne l'item créé.
return layoutItem;
}
}
package lazy.student;
import java.util.ArrayList;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ListView;
public class LazyStudentActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//affichage d'une liste de note
setContentView(R.layout.liste_note);
//recupération de la liste de note (normalement avec metiere.getNotes())
ArrayList<Note> notes =Note.getAListOfNotes();
//Création et initialisation de l'Adapter pour les notes
NoteAdapter adapter = new NoteAdapter(this, notes);
//Récupération du composant ListView
ListView list = (ListView)findViewById(R.id.ListView01);
//Initialisation de la liste avec les données
list.setAdapter(adapter);
//*/
}
}