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 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252
| package com.android.rss;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import android.app.Fragment;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.os.StrictMode;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;
import android.webkit.WebView;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.GridLayout;
import android.widget.SimpleAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
public class MyFragmentB extends Fragment {
String value = "";
String str;
String urlImg;
View v;
TextView text;
TextView text2;
TextView textInfo;
ArrayAdapter<String> itemAdapter;
ArrayList<String> arrayDescription;
ArrayList<String> arrayImage;
ArrayList<String> arrayTitre;
View myFragmentView;
ListView listView;
ListView listView2;
ImageView imageView;
WebView imgWeb;
XMLParser file;
Document doc;
GridLayout gridLayout;
//------------------------ Tableau qui sera inséré dans le ListView---------------------
//Création de la ArrayList qui nous permettra de remplir la listView
ArrayList<HashMap<String, String>> listData = new ArrayList<HashMap<String, String>>();
//On déclare la HashMap qui contiendra les informations pour un item
HashMap<String, String> map;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
getActivity().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
myFragmentView = inflater.inflate(R.layout.fragment_a, container, false);
arrayDescription = new ArrayList<String>();
arrayImage = new ArrayList<String>();
arrayTitre = new ArrayList<String>();
text = (TextView)myFragmentView.findViewById(R.id.txt2);
InputMethodManager imm = (InputMethodManager)getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(myFragmentView.findViewById(R.id.txt).getWindowToken(), 0);
str = httpRequest("http://tempsreel.nouvelobs.com/rss.xml");
/*Autres sites
*http://rss.lemonde.fr/c/205/f/3050/index.rss
*http://tempsreel.nouvelobs.com/rss.xml*/
setList(str);
return myFragmentView;
}
public String httpRequest (String adress){
String answer = "", temp = "";
try{
URL url = new URL(adress);
HttpURLConnection con = (HttpURLConnection) url.openConnection();
InputStream in = con.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(in));
while ((temp = reader.readLine()) != null){
answer = answer + temp;
}
reader.close();
}catch (Exception e){}
return answer;
}
public void setList(String str){
file = new XMLParser();
doc = file.getDomElement(str);
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
//doc.getElementsByTagName("title").getLength()
for(int i=2;i<doc.getElementsByTagName("title").getLength()-2;i++){
try{
map = new HashMap<String, String>();
Element e = (Element) doc.getElementsByTagName("title").item(i);
String id = file.getElementValue(e);
//Ajout du titre pour un seul item
map.put("titre", id);
Element elemDescription = (Element) doc.getElementsByTagName("description").item(i-1);
String idDescription = file.getElementValue(elemDescription);
arrayDescription.add(idDescription);
Element elemImage = (Element) doc.getElementsByTagName("enclosure").item(i-2);
String idImage = elemImage.getAttribute("url");
arrayImage.add(idImage);
Bitmap bimage = getBitmapFromURL(arrayImage.get(i-2));
Drawable d = new BitmapDrawable(getResources(),bimage);
map.put("img", String.valueOf(d));
listData.add(map);
}catch(NullPointerException e){}
}
listView = (ListView)myFragmentView.findViewById(R.id.list);
SimpleAdapter mSchedule = new SimpleAdapter (getActivity().getBaseContext(), listData, R.layout.affichageitem,new String[] {"img", "titre"}, new int[] {R.id.img, R.id.titre});
//On attribue à notre listView l'adapter que l'on vient de créer
listView.setAdapter(mSchedule);
//Reaction aux clicks sur les item de la ListView
listView.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,int position, long id) {
//When clicked, show a toast with the TextView text
Intent myIntent[] = new Intent[arrayTitre.size()];
for(int i = 0;i < arrayTitre.size(); i++){
/*try{
Intent in = new Intent(Intent.ACTION_VIEW,Uri.parse(arrayURL.get(i)));
startActivity(in);
getActivity().finish();
}catch(ActivityNotFoundException e){}*/
if(position == i){
//code specific to first list item
myIntent[i] = new Intent(view.getContext(), Article.class);
//Passage de paramètres à la nouvelle activité
myIntent[i].putExtra("str",value);
myIntent[i].putExtra("img",arrayImage.get(i));
myIntent[i].putExtra("description",arrayDescription.get(i));
myIntent[i].putExtra("titre",arrayTitre.get(i));
startActivityForResult(myIntent[i], 0);
}
}
}
});
}
public static Bitmap getBitmapFromURL(String src) {
try {
Log.e("src",src);
URL url = new URL(src);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setDoInput(true);
connection.connect();
InputStream input = connection.getInputStream();
Bitmap myBitmap = BitmapFactory.decodeStream(input);
Log.e("Bitmap","returned");
return myBitmap;
} catch (IOException e) {
e.printStackTrace();
Log.e("Exception",e.getMessage());
return null;
}
}
} |
Partager