Bonjour , je voudrais afficher un flux RSS(ici uniquement le title) ds une ListView

Mais j'ai un souci avec Les thread

je suis sur Android 4.2.2 et je dois utiliser un Thread différent pour afficher une ListView ds un fragment

Cependant il m'indique un message d'erreur

android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views

Après recherche , je devrais utilser : runOnUiThread(new Runnable() mais çà ne marche pas

Une idée, ce serait bien Merci

Voici les codes

Acitivity : Earthquake extends Activity

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
 
package com.paad.earthquake;
//
import java.util.ArrayList;
 
import android.app.Activity;
import android.app.FragmentManager;
import android.os.Bundle;
import android.widget.ArrayAdapter;
 
public class Earthquake extends Activity implements EarthquakeListFragment.AddNewQuake{
    /** Called when the activity is first created. */
	ArrayAdapter<Quake> aa;
	ArrayList<Quake> earthquakes; 
 
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
 
        FragmentManager fm =getFragmentManager();
        EarthquakeListFragment earthquakeListFragment = (EarthquakeListFragment) fm.findFragmentById(R.id.EarthquakeListFragment);
 
        earthquakes = new ArrayList<Quake>();
 
        int layoutID = android.R.layout.simple_list_item_1;
        aa = new ArrayAdapter<Quake>(this, layoutID , earthquakes);
        earthquakeListFragment.setListAdapter(aa);
    }
 
    public void ajouterNewQuake(Quake _quake) {
        // Add the new quake to our list of earthquakes.
        earthquakes.add(_quake);
 
        // Notify the array adapter of a change.
        aa.notifyDataSetChanged();
      }
}
Le fragment
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
 
package com.paad.earthquake;
 
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
 
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
 
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;
 
import android.app.Activity;
import android.app.ListFragment;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
 
public class EarthquakeListFragment extends ListFragment {
 
  public AddNewQuake addNewQuake;
  public interface AddNewQuake{
	  public void ajouterNewQuake(Quake _quake);
  }
 
  @Override
  public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle saveIinstanceState){
	  return inflater.inflate(R.layout.fragment_listview,container,false);
  }
 
  @Override
  public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
 
    Thread t = new Thread() {
 
		public void run() {
			runOnUiThread(new Runnable() {  
                @Override
                public void run() {
        			refreshEarthquakes();
                }
            });
 
		};
	};
    t.start();
  }
 
  private static final String TAG = "EARTHQUAKE";
  private static final String TAG0 = "EARTHQUAKE0";
  private static final String TAG1 = "EARTHQUAKE1";
  private static final String TAG2 = "EARTHQUAKE2";
  private static final String TAG3 = "EARTHQUAKE3";
  private static final String TAG4 = "EARTHQUAKE4";
 
  private void refreshEarthquakes() {
    // Get the XML
    URL url;
    try {
      String quakeFeed = getString(R.string.quake_feed);
      url = new URL(quakeFeed);
 
      URLConnection connection;
      connection = url.openConnection();
 
      HttpURLConnection httpConnection = (HttpURLConnection)connection;
      int responseCode = httpConnection.getResponseCode();
 
      if (responseCode == HttpURLConnection.HTTP_OK) {
        InputStream in = httpConnection.getInputStream();
 
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        DocumentBuilder db = dbf.newDocumentBuilder();
 
        // Parse the earthquake feed.
        Document dom = db.parse(in);
        Element docEle = dom.getDocumentElement();
 
        // Get a list of each earthquake entry.
        NodeList nl = docEle.getElementsByTagName("entry");
 
        if (nl != null && nl.getLength() > 0) {
          for (int i = 1 ; i < nl.getLength(); i++) {
            Element entry = (Element)nl.item(i);
            Element title = (Element)entry.getElementsByTagName("title").item(0);
 
            String details = title.getFirstChild().getNodeValue();
 
            Quake quake = new Quake(details);
            // Process a newly found earthquake
            addNewQuake.ajouterNewQuake(quake);
          }
        }
      }
    } catch (MalformedURLException e) {
      Log.d(TAG0, "MalformedURLException", e);
    } catch (IOException e) {
      Log.d(TAG1, "IOException", e);
    } catch (ParserConfigurationException e) {
      Log.d(TAG2, "Parser Configuration Exception", e);
    } catch (SAXException e) {
      Log.d(TAG3, "SAX Exception", e);
    }
     catch (Exception e) {
      Log.d(TAG4, "Exception simple", e);
     }
    finally {
    }
  }
 
  @Override
  public void onAttach(Activity activity) {
    super.onAttach(activity);
 
    try {
    	addNewQuake = (AddNewQuake)activity;
    } catch (ClassCastException e) {
      throw new ClassCastException(activity.toString() + 
                " must implement AddNewQuake");
    }
  }
}
La class Quake
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
 
package com.paad.earthquake;
 
import java.util.Date;
import java.text.SimpleDateFormat;
import android.location.Location;
 
public class Quake {
  public String details;
 
  public String getDetails() { return details; }
  public Quake(String _det) {
    details = _det; 
  }
 
  @Override
  public String toString() {
	  return details;
  }
 
}
La main.xml
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
 
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:orientation="vertical"
  android:layout_width="match_parent"
  android:layout_height="match_parent">
  <fragment android:name="com.paad.earthquake.EarthquakeListFragment"
    android:id="@+id/EarthquakeListFragment"
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
  />
</LinearLayout>
La ressource strings.xml
[CODE]
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">Earthquake</string>
<string name="quake_feed">
http://earthquake.usgs.gov/eqcenter/catalogs/1day-M2.5.xml
</string>
</resources>
[CODE]

le layout du fragment
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
 
	<ListView
        android:id="@android:id/list"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true" />
</LinearLayout>