主要活动
public class MainActivity extends Activity {
ArrayList<HashMap<String, String>> name;
ListView maListRSS;
Bitmap img;
ArrayList<News> News_data ;
private ProgressDialog mProgressDialog;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
maListRSS = (ListView) findViewById(R.id.list);
name = new ArrayList<HashMap<String, String>>();
News_data = new ArrayList<News>();
AsyncTask<Void, Void, Void> task = new AsyncTask<Void, Void, Void>() {
protected void onPreExecute() {
mProgressDialog = new ProgressDialog(MainActivity.this);
mProgressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
mProgressDialog.setMessage("جاري التحميل....");
mProgressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
mProgressDialog.show();
}
protected Void doInBackground(Void... params) {
// TODO Auto-generated method stub
URL url = null;
try {
url = new URL("http://ahsaber.org/?feed=rss2");
} catch (MalformedURLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = null;
try {
db = dbf.newDocumentBuilder();
} catch (ParserConfigurationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
org.w3c.dom.Document doc = null;
try {
doc = db.parse(new InputSource(url.openStream()));
} catch (SAXException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
doc.getDocumentElement().normalize();
NodeList nodeList = doc.getElementsByTagName("item");
for (int i = 0; i < nodeList.getLength(); i++) {
Node node = nodeList.item(i);
Element fstElmnt = (Element) node;
NodeList nameList = fstElmnt.getElementsByTagName("title");
Element nameElement = (Element) nameList.item(0);
nameList = ((Node) nameElement).getChildNodes();
NodeList websiteList =fstElmnt.getElementsByTagName("link");
Element websiteElement = (Element) websiteList.item(0);
websiteList = ((Node) websiteElement).getChildNodes();
NodeList descriptionList =fstElmnt.getElementsByTagName("description");
Element descriptionElement = (Element) descriptionList.item(0);
descriptionList = ((Node) descriptionElement).getChildNodes();
NodeList encodedList =fstElmnt.getElementsByTagName("content:encoded");
Element EnocodeElement = (Element) encodedList.item(0);
encodedList = ((Node) EnocodeElement).getChildNodes();
int start =EnocodeElement.getTextContent().indexOf("http://ahsaber.org");
int end =EnocodeElement.getTextContent().indexOf(".jpg");
HashMap<String, String> map = new HashMap<String, String>();
map.put("title", ((Node) nameList.item(0)).getNodeValue());
map.put("tel", ((Node) websiteList.item(0)).getNodeValue());
map.put("des", ((Node) descriptionList.item(0)).getNodeValue());
if (end>start)
{
map.put("imageURL",EnocodeElement.getTextContent().substring(start,end)+".jpg");
System.out.println(EnocodeElement.getTextContent().substring(start,end));
try {
URL lien = new URL(EnocodeElement.getTextContent().substring(start,end)+".jpg");
HttpURLConnection connection = null;
connection = (HttpURLConnection) lien.openConnection();
InputStream is = null;
is = connection.getInputStream();
img = BitmapFactory.decodeStream(is);
News N=new News(img,((Node) nameList.item(0)).getNodeValue());
News_data.add(N);
name.add(map);
}
catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
return null;
}
protected void onPostExecute(Void result) {
mProgressDialog.dismiss();
NewsAdapter adapter = new NewsAdapter(MainActivity.this,
R.layout.listviewrow, News_data);
adapter.notifyDataSetChanged();
maListRSS.setAdapter(adapter);
}
};
task.execute((Void[]) null);
}
}
这是自定义适配器类:
public class NewsAdapter extends ArrayAdapter<News> {
Context context;
int layoutResourceId;
ArrayList<News> data =new ArrayList<News>() ;
public NewsAdapter(Context asyncTask, int layoutResourceId, ArrayList<News> news_data) {
super(asyncTask, layoutResourceId, news_data);
this.layoutResourceId = layoutResourceId;
this.context = asyncTask;
for (int i=0;i<news_data.size();i++)
{
data.add(news_data.get(i));
}
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View row = convertView;
NewsHolder holder = null;
if(row == null)
{
LayoutInflater inflater = ((Activity)context).getLayoutInflater();
row = inflater.inflate(layoutResourceId, parent, false);
holder = new NewsHolder();
holder.imgIcon = (ImageView)row.findViewById(R.id.newsimage);
holder.txtTitle = (TextView)row.findViewById(R.id.newstitle);
row.setTag(holder);
}
else
{
holder = (NewsHolder)row.getTag();
}
News N= data.get(position);
holder.txtTitle.setText(N.title);
holder.imgIcon.setImageBitmap(N.icon);
return row;
}
public int getCount() {
// TODO Auto-generated method stub
return data.size();
}
static class NewsHolder
{
ImageView imgIcon;
TextView txtTitle;
}
}
新闻类:
public class News {
public Bitmap icon;
public String title;
public News() {
// TODO Auto-generated constructor stub
super();
}
public News(Bitmap icon, String title) {
super();
this.icon = icon;
this.title = title;
}
}
所以这是logcat:
01-08 10:02:11.584: E/AndroidRuntime(967): FATAL EXCEPTION: main
01-08 10:02:11.584: E/AndroidRuntime(967): java.lang.NullPointerException
01-08 10:02:11.584: E/AndroidRuntime(967): at com.example.albir.MainActivity$1.onPostExecute(MainActivity.java:139)
01-08 10:02:11.584: E/AndroidRuntime(967): at com.example.albir.MainActivity$1.onPostExecute(MainActivity.java:1)
01-08 10:02:11.584: E/AndroidRuntime(967): at android.os.AsyncTask.finish(AsyncTask.java:631)
01-08 10:02:11.584: E/AndroidRuntime(967): at android.os.AsyncTask.access$600(AsyncTask.java:177)
01-08 10:02:11.584: E/AndroidRuntime(967): at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:644)
01-08 10:02:11.584: E/AndroidRuntime(967): at android.os.Handler.dispatchMessage(Handler.java:99)
01-08 10:02:11.584: E/AndroidRuntime(967): at android.os.Looper.loop(Looper.java:137)
01-08 10:02:11.584: E/AndroidRuntime(967): at android.app.ActivityThread.main(ActivityThread.java:4745)
01-08 10:02:11.584: E/AndroidRuntime(967): at java.lang.reflect.Method.invokeNative(Native Method)
01-08 10:02:11.584: E/AndroidRuntime(967): at java.lang.reflect.Method.invoke(Method.java:511)
01-08 10:02:11.584: E/AndroidRuntime(967): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
01-08 10:02:11.584: E/AndroidRuntime(967): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
01-08 10:02:11.584: E/AndroidRuntime(967): at dalvik.system.NativeStart.main(Native Method)