IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

Composants graphiques Android Discussion :

Adaptation de ListView


Sujet :

Composants graphiques Android

Vue hybride

Message précédent Message précédent   Message suivant Message suivant
  1. #1
    Membre actif
    Femme Profil pro
    Développeur informatique
    Inscrit en
    Juillet 2011
    Messages
    48
    Détails du profil
    Informations personnelles :
    Sexe : Femme
    Localisation : Tunisie

    Informations professionnelles :
    Activité : Développeur informatique

    Informations forums :
    Inscription : Juillet 2011
    Messages : 48
    Par défaut Adaptation de ListView
    je suis entrain de développer mon application qui permet de parser les données à partir d'un fichier XML et l'afficher sous une listview.

    l'erreur : "the constructeur SimpleAdapter is undefined "
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
     // Adding menuItems to ListView
    				ListAdapter adapter = new SimpleAdapter(this, menuItems,R.layout.item_detail_accueil,
    						new String[] { KEY_TITLE, KEY_CONTENT, KEY_TEASER }, new int[] {
    								R.id.title, R.id.teaser, R.id.content });
    voila toute la classe

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    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
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
     
    package com.android.SAXParser;
     
    import java.net.URL;
    import java.util.ArrayList;
    import java.util.HashMap;
     
    import javax.xml.parsers.SAXParser;
    import javax.xml.parsers.SAXParserFactory;
     
    import org.xml.sax.InputSource;
    import org.xml.sax.XMLReader;
     
    import android.app.ListActivity;
    import android.app.ProgressDialog;
    import android.os.Bundle;
    import android.widget.ListAdapter;
    import android.widget.SimpleAdapter;
    import android.widget.Toast;
     
    public class SAXParserActivity extends  ListActivity{
     
    	XMLGettersSetters data;
     
    	protected ProgressDialog ProgressDialog;
    	private Runnable albumsRunnable;
    	//TextView title,link,teaser,content,sound,photo,date;
     
    	static final String KEY_ARTICLE = "article"; // parent node
    	static final String KEY_TITLE = "title";
    	static final String KEY_LINK = "link";
    	static final String KEY_TEASER = "teaser";
    	static final String KEY_CONTENT = "content";
    	static final String KEY_SOUND = "sound";
    	static final String KEY_photo = "photo";
    	static final String KEY_date = "date";
     
    	ArrayList<HashMap<String, String>> menuItems = new ArrayList<HashMap<String, String>>();
    	/** Called when the activity is first created. */
    	@Override
    	public void onCreate(Bundle savedInstanceState) 
    	{
    		super.onCreate(savedInstanceState);
    		setContentView(R.layout.main);
     
     
     
    		 albumsRunnable = new Runnable()
    		  {
    		   public void run() 
    		   {
     
     
    		    getData();
     
    		   }
    		  };
     
     
    		  Thread thread = new Thread(null, albumsRunnable, "GetAlbumsThread");
    		  // Start the thread
    		  thread.start();
     
    		  // Display progress dialog
    		  ProgressDialog = ProgressDialog.show(this, null, "waiting", true);
    	}
     
     
    	 private void getData()
    	{
    		   boolean error = false;
     
     
    				try {
     
    					/**
                                             * Create a new instance of the SAX parser
                                             **/
    					SAXParserFactory saxPF = SAXParserFactory.newInstance();
    					SAXParser saxP = saxPF.newSAXParser();
    					XMLReader xmlR = saxP.getXMLReader();
     
     
    					URL url = new URL("http://www.mosaiquefm.net/smart/newscast.xml?Cat=4"); // URL of the XML
     
    					/** 
                                             * Create the Handler to handle each of the XML tags. 
                                             **/
    					XMLHandler myXMLHandler = new XMLHandler();
    					xmlR.setContentHandler(myXMLHandler);
    					xmlR.parse(new InputSource(url.openStream()));
     
    				} catch (Exception e) {
    					System.out.println(e);
    				}
     
    				data = XMLHandler.data;
     
     
    		    if (data == null)
    		    {
    		     error = true;
    		    }
     
    		   if (error == false)
    		   {
    		    runOnUiThread(albumsResultRunnable);
    		   }
    		   else
    		   {
    		    runOnUiThread(returnNullResourceRunnable);
    		   }
    		  }
     
      private Runnable albumsResultRunnable = new Runnable()
    		  {
    		   public void run()
    		   {
    			    if (ProgressDialog.isShowing())
    			    {
    			    	ProgressDialog.dismiss();
    			    }
    			   // Toast.makeText(SAXParserActivity.this, data.getTitle().get(0), Toast.LENGTH_LONG).show();
     
    			    for (int i = 0; i < data.getTitle().size(); i++) {
    					// creating new HashMap
     
    					HashMap<String, String> map = new HashMap<String, String>();
     
    					// adding each child node to HashMap key => value
    					map.put(KEY_TITLE, data.getTitle().get(i));
    					map.put(KEY_CONTENT, data.getContent().get(i));
    					map.put(KEY_TEASER, "Rs." + data.getTeaser().get(i));
    				//	map.put(KEY_DESC, parser.getValue(e, KEY_DESC));
     
    					// adding HashList to ArrayList
    					menuItems.add(map);
    				}
    			 // Adding menuItems to ListView
    				ListAdapter adapter = new SimpleAdapter(this, menuItems,R.layout.item_detail_accueil,
    						new String[] { KEY_TITLE, KEY_CONTENT, KEY_TEASER }, new int[] {
    								R.id.title, R.id.teaser, R.id.content });
     
    				setListAdapter(adapter);
     
     
    		   }
     
    		  };
     
    		  private Runnable returnNullResourceRunnable = new Runnable()
    		  {
    		   public void run()
    		   {
    		    if (ProgressDialog.isShowing())
    		    {
    		    	ProgressDialog.dismiss();
    		    }
    		   Toast.makeText(SAXParserActivity.this, "Erreur", Toast.LENGTH_LONG).show();
     
     
    		   }
    		  };
     
     
    }

  2. #2
    Membre actif
    Femme Profil pro
    Développeur informatique
    Inscrit en
    Juillet 2011
    Messages
    48
    Détails du profil
    Informations personnelles :
    Sexe : Femme
    Localisation : Tunisie

    Informations professionnelles :
    Activité : Développeur informatique

    Informations forums :
    Inscription : Juillet 2011
    Messages : 48
    Par défaut
    mon probléme c'est que les textview ne s'affiche pas malgré qu'ils se trouve dans le hashmap malgré que

    voila code
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    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
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    package com.android.SAXParser;
     
    import java.net.URL;
    import java.util.ArrayList;
    import java.util.HashMap;
     
    import javax.xml.parsers.SAXParser;
    import javax.xml.parsers.SAXParserFactory;
     
    import org.xml.sax.InputSource;
    import org.xml.sax.XMLReader;
     
    import android.app.ListActivity;
    import android.app.ProgressDialog;
    import android.os.Bundle;
    import android.widget.ListAdapter;
    import android.widget.SimpleAdapter;
    import android.widget.Toast;
     
    public class SAXParserActivity extends  ListActivity{
     
    	XMLGettersSetters data;
     
    	protected ProgressDialog ProgressDialog;
    	private Runnable albumsRunnable;
    	//TextView title,link,teaser,content,sound,photo,date;
     
    	static final String KEY_ARTICLE = "article"; // parent node
    	static final String KEY_TITLE = "title";
    	static final String KEY_LINK = "link";
    	static final String KEY_TEASER = "teaser";
    	static final String KEY_CONTENT = "content";
    	static final String KEY_SOUND = "sound";
    	static final String KEY_photo = "photo";
    	static final String KEY_date = "date";
     
    	ArrayList<HashMap<String, String>> menuItems = new ArrayList<HashMap<String, String>>();
    	/** Called when the activity is first created. */
    	@Override
    	public void onCreate(Bundle savedInstanceState) 
    	{
    		super.onCreate(savedInstanceState);
    		setContentView(R.layout.main);
     
     
     
    		 albumsRunnable = new Runnable()
    		  {
    		   public void run() 
    		   {
     
     
    		    getData();
     
    		   }
    		  };
     
    		  ListAdapter adapter = new SimpleAdapter(this, menuItems,R.layout.item_detail_accueil,
    					new String[] { KEY_TITLE, KEY_CONTENT, KEY_TEASER }, new int[] {
    							R.id.title, R.id.teaser, R.id.content });
     
    		  setListAdapter(adapter);
    		  Thread thread = new Thread(null, albumsRunnable, "GetAlbumsThread");
    		  // Start the thread
    		  thread.start();
     
    		  // Display progress dialog
    		  ProgressDialog = ProgressDialog.show(this, null, "waiting", true);
    	}
     
     
    	 private void getData()
    	{
    		   boolean error = false;
     
     
    				try {
     
    					/**
                                             * Create a new instance of the SAX parser
                                             **/
    					SAXParserFactory saxPF = SAXParserFactory.newInstance();
    					SAXParser saxP = saxPF.newSAXParser();
    					XMLReader xmlR = saxP.getXMLReader();
     
     
    					URL url = new URL("http://www.mosaiquefm.net/smart/newscast.xml?Cat=4"); // URL of the XML
     
    					/** 
                                             * Create the Handler to handle each of the XML tags. 
                                             **/
    					XMLHandler myXMLHandler = new XMLHandler();
    					xmlR.setContentHandler(myXMLHandler);
    					xmlR.parse(new InputSource(url.openStream()));
     
    				} catch (Exception e) {
    					System.out.println(e);
    				}
     
    				data = XMLHandler.data;
     
     
    		    if (data == null)
    		    {
    		     error = true;
    		    }
     
    		   if (error == false)
    		   {
    		    runOnUiThread(albumsResultRunnable);
    		   }
    		   else
    		   {
    		    runOnUiThread(returnNullResourceRunnable);
    		   }
    		  }
     
      private Runnable albumsResultRunnable = new Runnable()
    		  {
    		   public void run()
    		   {
    			    if (ProgressDialog.isShowing())
    			    {
    			    	ProgressDialog.dismiss();
    			    }
    			   // Toast.makeText(SAXParserActivity.this, data.getTitle().get(0), Toast.LENGTH_LONG).show();
     
    			    for (int i = 0; i < data.getTitle().size(); i++) {
    					// creating new HashMap
     
    					HashMap<String, String> map = new HashMap<String, String>();
     
    					// adding each child node to HashMap key => value
    					map.put(KEY_TITLE, data.getTitle().get(i));
    					map.put(KEY_CONTENT, data.getContent().get(i));
    					map.put(KEY_TEASER, "Rs." + data.getTeaser().get(i));
    				//	map.put(KEY_DESC, parser.getValue(e, KEY_DESC));
     
    					// adding HashList to ArrayList
    					menuItems.add(map);
    				}
    			 // Adding menuItems to ListView
     
     
     
     
     
    		   }
     
    		  };
     
    		  private Runnable returnNullResourceRunnable = new Runnable()
    		  {
    		   public void run()
    		   {
    		    if (ProgressDialog.isShowing())
    		    {
    		    	ProgressDialog.dismiss();
    		    }
    		   Toast.makeText(SAXParserActivity.this, "Erreur", Toast.LENGTH_LONG).show();
     
     
    		   }
    		  };
     
     
    }

  3. #3
    Expert confirmé

    Avatar de Feanorin
    Profil pro
    Inscrit en
    Avril 2004
    Messages
    4 589
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Avril 2004
    Messages : 4 589
    Par défaut
    Bonjour,

    As tu corrigé ton problème avec le constructeur de ton SimpleAdpater.

    Le constructeur a l'air cohérent.

    Après est ce que ta liste (menuItems) est mise à jour ?

    Après n'oublies pas qu'après chaque modification (ajout,suppression,..) de ta liste il faut notifier à l'adapter ce changement.

    Pour cela après l'opération faites sur ta liste appelle la fonction notifyDataSetChanged.

    Comme ceci :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
     
     
    // Création de la variable adapter 
    SimpleAdapter adapter ;
     
    // Lors de la création de ton adpater 
    adapter = new SimpleAdapter(this, menuItems,R.layout.item_detail_accueil,
    					new String[] { KEY_TITLE, KEY_CONTENT, KEY_TEASER }, new int[] {
    							R.id.title, R.id.teaser, R.id.content });
    setListAdapter((ListAdapter)adapter);
     
     
    //Après ton add sur ta liste
    adapter.notifyDataSetChanged();
    Après avoir vu ton code, peut être il serait plus intéressant d'appeller ton runnable depuis la fonction onStart (appelle que la première fois pour l'initialiser) cela sera plus cohérent.

  4. #4
    Membre actif
    Femme Profil pro
    Développeur informatique
    Inscrit en
    Juillet 2011
    Messages
    48
    Détails du profil
    Informations personnelles :
    Sexe : Femme
    Localisation : Tunisie

    Informations professionnelles :
    Activité : Développeur informatique

    Informations forums :
    Inscription : Juillet 2011
    Messages : 48
    Par défaut
    non pas encore
    maintenant je suis entrain de mettre les données XML dans un arraylist
    pour que je l'affiche après dans une listview (chaque item contient 3 listview).

    mais j'arrive pas à le faire

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    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
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    package com.android.SAXParser;
     
    import java.net.URL;
    import java.util.ArrayList;
    import java.util.HashMap;
     
    import javax.xml.parsers.SAXParser;
    import javax.xml.parsers.SAXParserFactory;
     
    import org.xml.sax.InputSource;
    import org.xml.sax.XMLReader;
     
    import android.app.Activity;
    import android.app.ProgressDialog;
    import android.os.Bundle;
    import android.widget.SimpleAdapter;
    import android.widget.Toast;
     
    public class SAXParserActivity extends Activity {
     
    	XMLGettersSetters data;
     
    	protected ProgressDialog ProgressDialog;
    	private Runnable albumsRunnable;
     
    	static final String KEY_ARTICLE = "article"; // parent node
    	static final String KEY_TITLE = "title";
    	static final String KEY_LINK = "link";
    	static final String KEY_TEASER = "teaser";
    	static final String KEY_CONTENT = "content";
    	static final String KEY_SOUND = "sound";
    	static final String KEY_photo = "photo";
    	static final String KEY_date = "date";
     
    	ArrayList<HashMap<String, String>> menuItems = new ArrayList<HashMap<String, String>>();
    	/** Called when the activity is first created. */
    	@Override
    	public void onCreate(Bundle savedInstanceState) 
    	{
    		super.onCreate(savedInstanceState);
    		setContentView(R.layout.main);
     
    		// Création de la variable adapter 
    		SimpleAdapter adapter ;
     
    		// Lors de la création de ton adpater 
    		adapter = new SimpleAdapter(this, menuItems,R.layout.item_detail_accueil,
    							new String[] { KEY_TITLE, KEY_CONTENT, KEY_TEASER }, new int[] {
    									R.id.title, R.id.teaser, R.id.content });
    		//setListAdapter((ListAdapter)adapter);
     
    		setListAdapter(adapter); 
    		//Après ton add sur ta liste
    		adapter.notifyDataSetChanged();
     
     
    		 albumsRunnable = new Runnable()
    		  {
    		   public void run() 
    		   {
     
     
    		    getData();
     
    		   }
    		  };
     
     
    		  Thread thread = new Thread(null, albumsRunnable, "GetAlbumsThread");
    		  // Start the thread
    		  thread.start();
     
    		  // Display progress dialog
    		  ProgressDialog = ProgressDialog.show(this, null, "waiting", true);
    	}
     
     
     
     
     
    	private void getData()
    	{
    		   boolean error = false;
     
     
    				try {
     
    					/**
                                             * Create a new instance of the SAX parser
                                             **/
    					SAXParserFactory saxPF = SAXParserFactory.newInstance();
    					SAXParser saxP = saxPF.newSAXParser();
    					XMLReader xmlR = saxP.getXMLReader();
     
     
    					URL url = new URL("http://www.mosaiquefm.net/smart/newscast.xml?Cat=4"); // URL of the XML
     
    					/** 
                                             * Create the Handler to handle each of the XML tags. 
                                             **/
    					XMLHandler myXMLHandler = new XMLHandler();
    					xmlR.setContentHandler(myXMLHandler);
    					xmlR.parse(new InputSource(url.openStream()));
     
    				} catch (Exception e) {
    					System.out.println(e);
    				}
     
    				data = XMLHandler.data;
     
     
    		    if (data == null)
    		    {
    		     error = true;
    		    }
     
    		   if (error == false)
    		   {
    		    runOnUiThread(albumsResultRunnable);
    		   }
    		   else
    		   {
    		    runOnUiThread(returnNullResourceRunnable);
    		   }
    		  }
     
      private Runnable albumsResultRunnable = new Runnable()
    		  {
    		   public void run()
    		   {
    			    if (ProgressDialog.isShowing())
    			    {
    			    	ProgressDialog.dismiss();
    			    }
    			 //   Toast.makeText(SAXParserActivity.this, data.getTitle().get(0), Toast.LENGTH_LONG).show();
     
     
    			    for (int i = 0; i < data.getTitle().size(); i++) {
    					// creating new HashMap
     
    					HashMap<String, String> map = new HashMap<String, String>();
     
    					// adding each child node to HashMap key => value
    					map.put(KEY_TITLE, data.getTitle().get(i));
    					map.put(KEY_CONTENT, data.getContent().get(i));
    					map.put(KEY_TEASER, "Rs." + data.getTeaser().get(i));
    				//	map.put(KEY_DESC, parser.getValue(e, KEY_DESC));
     
    					// adding HashList to ArrayList
    					menuItems.add(map);
    				}
    			 // Adding menuItems to ListView
     
     
    		   }
     
    		  };
     
    		  private Runnable returnNullResourceRunnable = new Runnable()
    		  {
    		   public void run()
    		   {
    		    if (ProgressDialog.isShowing())
    		    {
    		    	ProgressDialog.dismiss();
    		    }
    		   Toast.makeText(SAXParserActivity.this, "Erreur", Toast.LENGTH_LONG).show();
     
    		   }
    		  };
     
    }
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    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
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    package com.android.SAXParser;
     
    import java.util.ArrayList;
     
    import android.util.Log;
     
    /**
     *  This class contains all getter and setter methods to set and retrieve data.
     *  
     **/
    public class XMLGettersSetters {
     
    	private ArrayList<String> title = new ArrayList<String>();
    	private ArrayList<String> link = new ArrayList<String>();
    	private ArrayList<String> teaser = new ArrayList<String>();
    	private ArrayList<String> content = new ArrayList<String>();
    	private ArrayList<String> sound = new ArrayList<String>();
    	private ArrayList<String> photo = new ArrayList<String>();
    	private ArrayList<String> date = new ArrayList<String>();
     
    	public ArrayList<String> getTitle() {
    		return title;
    	}
     
    	public void setTitle(String title) {
    		this.title.add(title);
    		Log.i("This is the title:", title);
    	}
     
    	public ArrayList<String> getLink() {
    		return link;
    	}
     
    	public void setLink(String link) {
    		this.link.add(link);
    		Log.i("This is the link:", link);
    	}
     
    	public ArrayList<String> getTeaser() {
    		return teaser;
    	}
     
    	public void setTeaser(String teaser) {
    		this.teaser.add(teaser);
    		Log.i("This is the teaser:", teaser);
    	}
     
    	public ArrayList<String> getContent() {
    		return content;
    	}
     
    	public void setContent(String content) {
    		this.content.add(content);
    		Log.i("This is the content:", content);
    	}
     
    	public ArrayList<String> getSound() {
    		return sound;
    	}
     
    	public void setSound(String sound) {
    		this.sound.add(sound);
    		Log.i("This is the sound:", sound);
    	}
     
    	public ArrayList<String> getPhoto() {
    		return photo;
    	}
     
    	public void setPhoto(String photo) {
    		this.photo.add(photo);
    		Log.i("This is the photo:", photo);
    	}
     
    	public ArrayList<String> getDate() {
    		return date;
    	}
     
    	public void setDate(String date) {
    		this.date.add(date);
    		Log.i("This is the date:", date);
    	}
     
    }
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    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
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    package com.android.SAXParser;
     
    import org.xml.sax.Attributes;
    import org.xml.sax.SAXException;
    import org.xml.sax.helpers.DefaultHandler;
     
    public class XMLHandler extends DefaultHandler {
     
    	String elementValue = null;
    	Boolean elementOn = false;
    	public static XMLGettersSetters data = null;
     
    	public static XMLGettersSetters getXMLData() {
    		return data;
    	}
     
    	public static void setXMLData(XMLGettersSetters data) {
    		XMLHandler.data = data;
    	}
     
    	/** 
             * This will be called when the tags of the XML starts.
             **/
    	@Override
    	public void startElement(String uri, String localName, String qName,
    			Attributes attributes) throws SAXException {
     
    		elementOn = true;
     
    		if (localName.equals("articles"))
    		{
    			data = new XMLGettersSetters();
    		} else if (localName.equals("article")) {
    			/** 
                             * We can get the values of attributes for eg. if the CD tag had an attribute( <CD attr= "band">Akon</CD> ) 
                             * we can get the value "band". Below is an example of how to achieve this.
                             * 
                             * String attributeValue = attributes.getValue("attr");
                             * data.setAttribute(attributeValue);
                             * 
                             * */
    		}
    	}
     
    	/** 
             * This will be called when the tags of the XML end.
             **/
    	@Override
    	public void endElement(String uri, String localName, String qName)
    			throws SAXException {
     
    		elementOn = false;
     
    		/** 
                     * Sets the values after retrieving the values from the XML tags
                     * */ 
    		if (localName.equalsIgnoreCase("title"))
    			data.setTitle(elementValue);
    		else if (localName.equalsIgnoreCase("link"))
    			data.setLink(elementValue);
    		else if (localName.equalsIgnoreCase("teaser"))
    			data.setTeaser(elementValue);
    		else if (localName.equalsIgnoreCase("content"))
    			data.setContent(elementValue);
    		else if (localName.equalsIgnoreCase("sound"))
    			data.setSound(elementValue);
    		else if (localName.equalsIgnoreCase("photo"))
    			data.setPhoto(elementValue);
    		else if (localName.equalsIgnoreCase("date"))
    			data.setDate(elementValue);
    	}
     
    	/** 
             * This is called to get the tags value
             **/
    	@Override
    	public void characters(char[] ch, int start, int length)
    			throws SAXException {
     
    		if (elementOn) {
    			elementValue = new String(ch, start, length);
    			elementOn = false;
    		}
     
    	}
     
    }
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    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
    <?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_gravity="center_vertical"
    	   	android:paddingLeft="10px" android:layout_height="wrap_content">
     
    	    <TextView android:id="@+id/title"
    	         android:layout_width="fill_parent"
    	         android:layout_height="fill_parent"
    	         android:textSize="20px"
    	         android:textStyle="bold"
    	         />
    	    <TextView android:id="@+id/teaser"
    	         android:layout_width="fill_parent"
    	         android:layout_height="fill_parent"
    	         android:textSize="20px"
    	         android:textStyle="bold"
    	         />
    	    <TextView android:id="@+id/content"
    	         android:layout_width="fill_parent"
    	         android:layout_height="fill_parent"
    	         android:textSize="20px"
    	         android:textStyle="bold"
    	         />
     
     
     
    </LinearLayout>
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    <?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="vertical" >
     
     
     
        <ListView
            android:id="@+id/srListView"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent" />
     
    </LinearLayout>
    Fichiers attachés Fichiers attachés

  5. #5
    Expert confirmé

    Avatar de Feanorin
    Profil pro
    Inscrit en
    Avril 2004
    Messages
    4 589
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Avril 2004
    Messages : 4 589
    Par défaut
    Il faut que tu fasse ta mise à jour après que tu es inséré tes items dans ta liste (hashmap)

    En gros ici :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    for (int i = 0; i < data.getTitle().size(); i++) {
    					// creating new HashMap
     
    					HashMap<String, String> map = new HashMap<String, String>();
     
    					// adding each child node to HashMap key => value
    					map.put(KEY_TITLE, data.getTitle().get(i));
    					map.put(KEY_CONTENT, data.getContent().get(i));
    					map.put(KEY_TEASER, "Rs." + data.getTeaser().get(i));
    				//	map.put(KEY_DESC, parser.getValue(e, KEY_DESC));
     
    					// adding HashList to ArrayList
    					menuItems.add(map);
    				}
    adapter.notifyDataSetChanged();
    Sinon en quoi cela servirait de le faire juste après la création de ta liste ?

  6. #6
    Membre actif
    Femme Profil pro
    Développeur informatique
    Inscrit en
    Juillet 2011
    Messages
    48
    Détails du profil
    Informations personnelles :
    Sexe : Femme
    Localisation : Tunisie

    Informations professionnelles :
    Activité : Développeur informatique

    Informations forums :
    Inscription : Juillet 2011
    Messages : 48
    Par défaut
    Juste faire un array list pour mettre les données pour que je puisse les afficher après (listview).
    J'ai déjà mis le code. mais je n'arrive pas à le faire arraylist et listview

  7. #7
    Membre actif
    Femme Profil pro
    Développeur informatique
    Inscrit en
    Juillet 2011
    Messages
    48
    Détails du profil
    Informations personnelles :
    Sexe : Femme
    Localisation : Tunisie

    Informations professionnelles :
    Activité : Développeur informatique

    Informations forums :
    Inscription : Juillet 2011
    Messages : 48
    Par défaut
    Citation Envoyé par Feanorin Voir le message
    Bonjour,

    As tu corrigé ton problème avec le constructeur de ton SimpleAdpater.

    Le constructeur a l'air cohérent.

    Après est ce que ta liste (menuItems) est mise à jour ?

    Après n'oublies pas qu'après chaque modification (ajout,suppression,..) de ta liste il faut notifier à l'adapter ce changement.

    Pour cela après l'opération faites sur ta liste appelle la fonction notifyDataSetChanged.

    Comme ceci :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
     
     
    // Création de la variable adapter 
    SimpleAdapter adapter ;
     
    // Lors de la création de ton adpater 
    adapter = new SimpleAdapter(this, menuItems,R.layout.item_detail_accueil,
    					new String[] { KEY_TITLE, KEY_CONTENT, KEY_TEASER }, new int[] {
    							R.id.title, R.id.teaser, R.id.content });
    setListAdapter((ListAdapter)adapter);
     
     
    //Après ton add sur ta liste
    adapter.notifyDataSetChanged();
    Après avoir vu ton code, peut être il serait plus intéressant d'appeller ton runnable depuis la fonction onStart (appelle que la première fois pour l'initialiser) cela sera plus cohérent.
    j'ai besoin de faire une arraylist dynamique cad de menuItems pour mettre les données et ensuite je les affiche sous forme de listview

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. Adapter et ListView
    Par binou2 dans le forum Composants graphiques
    Réponses: 4
    Dernier message: 09/07/2014, 14h06
  2. adapter une listview par widget ratingbar ?
    Par amirmejrii dans le forum Composants graphiques
    Réponses: 0
    Dernier message: 10/02/2012, 21h58
  3. adapter dans listView
    Par anastunisie dans le forum Composants graphiques
    Réponses: 1
    Dernier message: 16/05/2011, 09h02
  4. Réponses: 2
    Dernier message: 21/02/2011, 17h00
  5. Comment adapter un ListView en fonction de l'objet appelant ?
    Par Xxbz3 dans le forum Windows Presentation Foundation
    Réponses: 12
    Dernier message: 18/12/2010, 16h42

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo