Aller au contenu
View in the app

A better way to browse. Learn more.

Forum de Frandroid

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

Erreur lors de l'utilisation d'un adapter sur une ListView

Featured Replies

Posté(e)

Bonjour a tous,

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

voici les codes:

note_layout:

<?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="fill_parent"
android:orientation="horizontal" >

<TextView
	android:text="Nom Note"
	android:layout_height="wrap_content"
	android:layout_width="wrap_content"
	android:id="@+id/nomNote"
	/>
<TextView
	android:text="Coeff"
	android:layout_height="wrap_content"
	android:layout_width="wrap_content"
	android:id="@+id/coeffNote"
	/>
<TextView
	android:text="res Note"
	android:layout_height="wrap_content"
	android:layout_width="wrap_content"
	android:id="@+id/resNote"
	/>  
</LinearLayout>

liste_note

<?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>

Note.java

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;
}

}

NoteAdapter.java

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;
}

}

LazyStudentActivity.java

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);
	//*/

}

}

  • 2 months later...
Posté(e)

Pourrait tu poster le LogCat du plantage ça aiderais ton code étant long à lire :D

Rejoignez la conversation

Vous pouvez poster maintenant et vous enregistrez plus tard. Si vous avez un compte, connectez-vous maintenant pour poster.

Invité
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Répondre à ce sujet…

Account

Navigation

Rechercher

Rechercher

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.