【发布时间】:2014-02-14 22:02:03
【问题描述】:
对于Android Studio,我正在尝试显示feed 的图像,但直到现在我还没有做到这一点,我还没有找到解决方案。你能帮我吗?
那是我的Handler:
public class RSSHandler extends DefaultHandler {
final int state_unknown = 0;
final int state_title = 1;
final int state_description = 2;
final int state_link = 3;
final int state_pubdate = 4;
int currentState = state_unknown;
ImageView imm;
RSSFeed feed;
RSSItem item;
boolean itemFound = false;
RSSHandler(){
}
RSSFeed getFeed(){
return feed;
}
@Override
public void startDocument() throws SAXException {
// TODO Auto-generated method stub
feed = new RSSFeed();
item = new RSSItem();
}
@Override
public void endDocument() throws SAXException {
// TODO Auto-generated method stub
}
@Override
public void startElement(String uri, String localName, String qName,
Attributes attributes) throws SAXException {
// TODO Auto-generated method stub
if (localName.equalsIgnoreCase("item")){
itemFound = true;
item = new RSSItem();
currentState = state_unknown;
}
if ("enclosure".equals(qName)) {
int i;
for (i = 0; i < attributes.getLength(); i++)
if (attributes.getQName(i).equals("url")) ;
String url = attributes.getValue(i);
Log.d("mylog", "url= " + url);
DownloadImagesTask downloadImages = new DownloadImagesTask(imm);
downloadImages.execute(url);
}
else if (localName.equalsIgnoreCase("title")){
currentState = state_title;
}
else if (localName.equalsIgnoreCase("description")){
currentState = state_description;
}
else if (localName.equalsIgnoreCase("link")){
currentState = state_link;
}
else if (localName.equalsIgnoreCase("pubdate")){
currentState = state_pubdate;
}
else{
currentState = state_unknown;
}
}
@Override
public void endElement(String uri, String localName, String qName)
throws SAXException {
// TODO Auto-generated method stub
if (localName.equalsIgnoreCase("item")){
feed.addItem(item);
}
}
@Override
public void characters(char[] ch, int start, int length)
throws SAXException {
// TODO Auto-generated method stub
String strCharacters = new String(ch,start,length);
if (itemFound==true){
// "item" tag found, it's item's parameter
switch(currentState){
case state_title:
item.setTitle(strCharacters);
break;
case state_description:
item.setDescription(strCharacters);
break;
case state_link:
item.setLink(strCharacters);
break;
case state_pubdate:
item.setPubdate(strCharacters);
break;
default:
break;
}
}
else{
// not "item" tag found, it's feed's parameter
switch(currentState){
case state_title:
feed.setTitle(strCharacters);
break;
case state_description:
feed.setDescription(strCharacters);
break;
case state_link:
feed.setLink(strCharacters);
break;
case state_pubdate:
feed.setPubdate(strCharacters);
break;
default:
break;
}
}
currentState = state_unknown;
}
这是我的AsyncTask 班级:
public class DownloadImagesTask extends AsyncTask<String, Void, Bitmap> {
ImageView imageView = null;
String log = null;
public DownloadImagesTask(ImageView imageView){
this.imageView = imageView;
}
@Override
protected Bitmap doInBackground(String... Urls) {
return download_Image(Urls[0]);
}
private Bitmap download_Image(String url) {
Bitmap bmp =null;
try{
URL ulrn = new URL(url);
HttpURLConnection con = (HttpURLConnection)ulrn.openConnection();
InputStream is = con.getInputStream();
bmp = BitmapFactory.decodeStream(is);
if (null != bmp)
return bmp;
}
catch(Exception e){
e.printStackTrace();
Log.d(log, "url= " + url);
}
return bmp;}
@Override
protected void onPostExecute(Bitmap result) {
imageView.setImageBitmap(result);
}
最后是我的CustomListView:
public class CustomList extends ArrayAdapter<RSSItem> {
private final Activity context;
private final List<RSSItem> web;
private String url;
private AssetManager assets;
public CustomList(Activity context, List<RSSItem> web) {
super(context, R.layout.list_item, web);
this.context = context;
this.web = web;
this.url = url;
}
@Override
public View getView(int position, View view, ViewGroup parent) {
LayoutInflater inflater = context.getLayoutInflater();
final View rowView = inflater.inflate(R.layout.list_item, null, true);
Typeface myTypeface = Typeface.createFromAsset(context.getAssets(), "BPreplay.otf");
if (android.os.Build.VERSION.SDK_INT > 9) {
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
}
final TextView txtTitle = (TextView) rowView.findViewById(R.id.item);
txtTitle.setText(web.get(position).getTitle());
txtTitle.setTypeface(myTypeface);
final TextView txtTitle2 = (TextView) rowView.findViewById(R.id.item2);
txtTitle2.setText(web.get(position).getDescription() + "...");
txtTitle2.setTypeface(myTypeface);
TextView txtTitle3 = (TextView) rowView.findViewById(R.id.pub);
txtTitle3.setText(web.get(position).getPubdate());
txtTitle3.setTypeface(myTypeface);
final TextView txtTitle4 = (TextView) rowView.findViewById(R.id.linke);
txtTitle4.setText("vai all'articolo completo --> " + web.get(position).getLink());
txtTitle4.setTypeface(myTypeface);
Button btn = (Button)rowView.findViewById(R.id.btn_share);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent sendIntent = new Intent();
sendIntent.setAction(Intent.ACTION_SEND);
sendIntent.putExtra(Intent.EXTRA_TEXT, "Leggi " +"''" + txtTitle.getText().toString()
+ ": " + txtTitle2.getText().toString()
+ txtTitle4.getText().toString() + "'' " + "Condiviso da Active News");
sendIntent.setType("text/plain");
context.startActivity(Intent.createChooser(sendIntent, rowView.getResources().getText(R.string.chooser_title)));
}
});
ImageView img = (ImageView)rowView.findViewById(R.id.immagine_feed);
DownloadImagesTask downloadImages = new DownloadImagesTask(img);
downloadImages.execute(url);
return rowView;
}
你能帮帮我吗?我真的需要这件事的帮助
【问题讨论】:
标签: android image android-asynctask rss