spiderams Posted March 30, 2010 Share Posted March 30, 2010 (edited) Bonjour, J'ai réalise une listview contenant une arrayList avec 2 élément. Lorsque on clique les éléments de la listview j'affiche un toast mais elle renvoie seulement un objet (@1545) au lieu des élément contenu dans listview. comment on fait pour afficher les éléments. voici mon code: classe principale main. java import java.util.ArrayList; import android.app.ListActivity; import android.os.Bundle; import android.view.View; import android.widget.AdapterView; import android.widget.ArrayAdapter; import android.widget.ListView; import android.widget.Toast; import android.widget.AdapterView.OnItemClickListener; import android.widget.AdapterView.OnItemSelectedListener; public class Main extends ListActivity implements OnItemClickListener, OnItemSelectedListener { ArrayList tests = new ArrayList(); /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); tests.add(new test("systeme","android")); tests.add(new test("windows","xp")); //lsttext listview contenant dans le Main ListView list = (ListView) this.findViewById(R.id.lsttext); list.setOnItemClickListener(this); list.setOnItemSelectedListener(this); monAdapter adpt = new monAdapter(this,tests); // list.setAdapter(adpt); } @Override public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) { Object item = arg0.getItemAtPosition(arg2); Toast.makeText(this,"click : "+item.toString(),1000).show(); } @Override public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3) { Object item = arg0.getItemAtPosition(arg2); Toast.makeText(this,"Selectionné : "+item.toString(),1000).show(); } @Override public void onNothingSelected(AdapterView<?> arg0) { Toast.makeText(this,"rien : ",1000).show(); } } classe test public class test{ String syteme; String categorie; public test(String _systeme, String _categorie) { systeme= _systeme; categorie= _categorie; } } classe monAdapter ublic class monAdapter extends BaseAdapter { private ArrayList tests; private LayoutInflater myInflater; public monAdapter(Context context, ArrayList _tests) { this.myInflater = LayoutInflater.from(context); this.tests = _tests; } @Override public int getCount() { return this.tests.size(); } @Override public Object getItem(int arg0) { return this.tests.get(arg0); } @Override public long getItemId(int position) { return position; } public static class ViewHolder { TextView text01; TextView text02; } @Override public View getView(int position, View convertView, ViewGroup parent) { ViewHolder holder; if (convertView == null) { // listitem fichier xml contenant 2 textview convertView = myInflater.inflate(R.layout.listitem, null); holder = new ViewHolder(); holder.text01 = (TextView) convertView.findViewById(R.id.txtNom); holder.text02 = (TextView) convertView.findViewById(R.id.txtCategorie); convertView.setTag(holder); } else { holder = (ViewHolder) convertView.getTag(); } holder.text01.setText(tests.get(position).nom); holder.text02.setText(tests.get(position).categorie); return convertView; } } Edited March 30, 2010 by spiderams Quote Link to comment Share on other sites More sharing options...
Nivek Posted March 30, 2010 Share Posted March 30, 2010 je pense qu'il te manque la surcharge de toString() dans ta classe test. Quote Link to comment Share on other sites More sharing options...
spiderams Posted March 30, 2010 Author Share Posted March 30, 2010 (edited) je pense qu'il te manque la surcharge de toString() dans ta classe test. non j'ai déjà mis la méthode cela ne donne rien du tout. Edited March 30, 2010 by spiderams Quote Link to comment Share on other sites More sharing options...
Nivek Posted March 31, 2010 Share Posted March 31, 2010 En tout cas, sans, tu ne pourras pas avoir autre chose que la référence de l'objet. Il faut au minimum un toString() qui retourne systeme + " " + categorie. Petite remarque, le code que tu nous as fourni ne doit pas compiler, ton getView() essaie d'accéder à un attribut nom qui n'existe pas dans ta classe test. Autre point, mais pour faciliter la lecture cette fois, essaie de respecter la convention de nommage des classes java : toujours commencer par une majuscule, sinon on les confond avec de simples variables. => public class MonAdapter => public class Test Quote Link to comment Share on other sites More sharing options...
camslecams Posted April 1, 2010 Share Posted April 1, 2010 En effet, le message doit afficher la chaine de caractères créée par le toString() de la classe Object, à savoir sa reference... Si tu veux créer ta propre méthode toString() dans la classe test, fait attention à lui donner exactement la même signature... Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.