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
| package com.android.Main;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
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.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.os.StrictMode;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.Toast;
import com.android.Parser.LazyAdapter;
import com.android.Parser.XMLParser;
public class Main extends Activity {
/** Called when the activity is first created. */
// All static variables
static String URL = null;
// XML node keys
static final String KEY_ITEM_RSS = "item"; // parent node
static final String KEY_TITLE_RSS = "title";
static final String KEY_DESCRIPTION_RSS = "description";
static final String KEY_LINK_RSS = "link";
static final String KEY_GUID_RSS = "guid";
static final String KEY_PUBDATE_RSS = "pubDate";
String _param = null;
Intent monintent = null;
HashMap<String, String> map = null;
ListView list = null;
LazyAdapter adapter = null;
ArrayList<HashMap<String, String>> songsList = null;
Element e = null;
StrictMode.ThreadPolicy policy = null;
XMLParser parser = null;
String xml = null;
Document doc = null;
NodeList nl = null;
DocumentBuilder db;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
monintent = new Intent(this, Touch.class);
policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
Bundle b = getIntent().getExtras();
URL = b.getString("parametres");
Toast.makeText(this, URL, 10000).show();
monintent = new Intent(this, Touch.class);
songsList = new ArrayList<HashMap<String, String>>();
parser = new XMLParser();
// String xml = parser.getXmlFromUrl(URL); // getting XML from URL
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
db = null;
try {
db = dbf.newDocumentBuilder();
} catch (ParserConfigurationException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
dl();
}
public void dl() {
final ProgressDialog dialog = ProgressDialog.show(this,
"téléchargement",
"Téléchargement \n , patientez un instant ...", true);
final Handler handler = new Handler() {
public void handleMessage(Message msg) {
aff();
dialog.dismiss();
}
};
Thread checkUpdate = new Thread() {
public void run() {
do {
try {
doc = db.parse(URL);
} catch (SAXException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
} while (doc == null);
handler.sendEmptyMessage(0);
}
};
checkUpdate.start();
}
public void aff() {
Log.e("nl ", "sa rentre");
if (doc != null) {
nl = doc.getElementsByTagName(KEY_ITEM_RSS);
// Log.e("nl ", String.valueOf(nl.getLength()));
// nl.getLength();
for (int i = 0; i < 15; i++) { // nombre d'éléments à afficher
// creating new HashMap
map = new HashMap<String, String>();
e = (Element) nl.item(i);
map.put(KEY_TITLE_RSS, parser.getValue(e, KEY_TITLE_RSS));
map.put(KEY_DESCRIPTION_RSS,
parser.getValue(e, KEY_DESCRIPTION_RSS));
map.put(KEY_LINK_RSS, parser.getValue(e, KEY_LINK_RSS));
map.put(KEY_PUBDATE_RSS, parser.getValue(e, KEY_PUBDATE_RSS));
map.put(KEY_GUID_RSS, parser.getValue(e, KEY_GUID_RSS));
songsList.add(map);
}
} else {
Log.e("nl ", "doc null");
}
list = (ListView) findViewById(R.id.list);
// Getting adapter by passing xml data ArrayList
adapter = new LazyAdapter(this, songsList);
list.setAdapter(adapter);
list.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
Pattern p = Pattern.compile("(http://.*.(jpg|png))");
Matcher m = p.matcher(songsList.get(position).get(
KEY_DESCRIPTION_RSS));
if (m.find()) {
_param = m.group(1);
}
monintent.putExtra("parametres", _param);
startActivityForResult(monintent, 1000);
}
});
}
} |
Partager