问题
I created a list in which i parse a webpage. I can display the titles and the first image of the first article but what i want is display the image for each article. This is the code:
EDITED CODE
public class MainActivity extends Activity{
ProgressDialog mProgressDialog;
public static final String TAG_TITOLI = "titoli";
private static final String TAG_IMMAGINE = "immagine";
ListView lista;
Bitmap bitmap;
public ImageView immagine;
public ImageView logoimg;
static final String BLOG_URL = "http://www.multiplayer.it";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
lista = (ListView)this.findViewById(R.id.main_lista);//recupero lista da id
//View inflatedView = getLayoutInflater().inflate(R.layout.riga_listview, null);
//immaginebtn = (Button)findViewById(R.id.immaginebtn);
//creo ed eseguo l'asynctask
ParsingPaginaWeb parsing = new ParsingPaginaWeb();
parsing.execute("");
// Immagine btn
/*immaginebtn.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
// Execute Logo AsyncTask
new Logo().execute();
}
});*/
//new Logo().execute();
// Launching new screen on Selecting Single ListItem
lista.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// getting values from selected ListItem
String titoli = ((TextView) view.findViewById(R.id.riga_listview_titolo)).getText().toString();
immagine = ((ImageView) view.findViewById(R.id.imageView1));
// Starting new intent
Intent in = new Intent(getApplicationContext(), SingleActivity.class);
in.putExtra(TAG_TITOLI, titoli);
in.putExtra(TAG_IMMAGINE, bitmap);
//in.putExtra(TAG_CONTENT, cont);
startActivity(in);
}
});
}
private class ParsingPaginaWeb extends AsyncTask<String,String,String> {
ArrayList<String> titoli; //lista dei titoli
ArrayList<Bitmap> bitmap = new ArrayList<Bitmap>();
//ArrayList<String> content; //lista delle descrizioni
@Override
protected void onPreExecute()
{
//prima di eseguire il parsing inizializzo gli arraylist
mProgressDialog = new ProgressDialog(MainActivity.this);
mProgressDialog.setTitle("Multiplayer.it");
mProgressDialog.setMessage("Caricamento articoli...");
mProgressDialog.setIndeterminate(false);
mProgressDialog.show();
titoli = new ArrayList<String>();
bitmap = new ArrayList<Bitmap>();
//content = new ArrayList<String>();
}
@Override
protected String doInBackground(String... params) {
try {
Document doc = Jsoup.connect(BLOG_URL).get();
Elements nodeBlogStats = doc.select("div.news-col-0 h3"); //per multiplayer.it Elements nodeBlogStats = doc.select("div.news-col-0 h3"); per ftv #comunePartINI > option
for(Element sezione : nodeBlogStats)
{
titoli.add(sezione.text());
}
} catch (Exception e) {
// In caso di errore
Log.e("ESEMPIO", "ERRORE NEL PARSING");
}
return null;
}
@Override
protected void onPostExecute(String result)
{
// dopo che ho eseguito il parsing mostro i dati nella listview
// usando il custom array adpater ParsingArrayAdapter
ParsingArrayAdapter adapter = new ParsingArrayAdapter(MainActivity.this, titoli, bitmap);
lista.setAdapter(adapter);
mProgressDialog.dismiss();
new Logo().execute();
}
}
// Classe per caricamento immagini..
// Logo AsyncTask
private class Logo extends AsyncTask<Void, Void, Void> {
@Override
protected void onPreExecute() {
super.onPreExecute();
mProgressDialog = new ProgressDialog(MainActivity.this);
mProgressDialog.setTitle("Immagini");
mProgressDialog.setMessage("Loading images...");
mProgressDialog.setIndeterminate(false);
mProgressDialog.show();
}
@Override
protected Void doInBackground(Void... params) {
try {
// Connect to the web site
Document document = Jsoup.connect(BLOG_URL).get();
// Using Elements to get the class data
//Elements img = document.select("div.news-col-0 img[src]");
// Locate the src attribute
for(Element img : document.select("div.news-col-0 img[src]")) {
String ImgSrc = img.attr("src");
// Download image from URL
InputStream input = new java.net.URL(ImgSrc).openStream();
// Decode Bitmap
bitmap = BitmapFactory.decodeStream(input);
}
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(Void result) {
// Set downloaded image into ImageView
logoimg = (ImageView) findViewById(R.id.imageView1);
logoimg.setImageBitmap(bitmap);
mProgressDialog.dismiss();
// ParsingArrayAdapter adapter = new ParsingArrayAdapter(MainActivity.this, titoli);
// lista.setAdapter(adapter);
}
}
}
How can i solve?
EDIT: Adapter class:
public class ParsingArrayAdapter extends ArrayAdapter<String>{
//ID and resources references
private final static int LAYOUT = R.layout.riga_listview;
private final static int TITOLO = R.id.riga_listview_titolo;
private final static int IMMAGINE = R.id.imageView1;
ArrayList<Bitmap> bitmap ;
ArrayList<String> titoli; //lista dei titoli
Context c; //context
LayoutInflater inflater; //layout inflater
public ParsingArrayAdapter(Context context,ArrayList<String> titoli, ArrayList<Bitmap> bitmap)
{
super(context,TITOLO);
this.c = context;
this.titoli = titoli;
this.bitmap = bitmap;
this.inflater = LayoutInflater.from(c);
}
@Override
public int getCount()
{
return titoli.size(); //List lenght
}
@SuppressLint("NewApi")
@Override
public View getView(int pos,View view,ViewGroup parent)
{
CacheRiga cache; //cache
if(view==null)
{
view = inflater.inflate(LAYOUT, parent,false);
cache = new CacheRiga(); //inizializzo la cache
cache.titolo = (TextView) view.findViewById(TITOLO); //title
cache.immagini = (ImageView) view.findViewById(IMMAGINE);//collego descrizione
view.setTag(cache);
}
else
{
cache = (CacheRiga) view.getTag();
}
cache.titolo.setText(titoli.get(pos)); //I set up the title
//cache.immagini.setImageBitmap(bitmap.get(pos));
return view;
}
private class CacheRiga { // Cache class
public TextView titolo; // Title cache
public ImageView immagini; // Image cache
}
}
回答1:
First of all, you might consider only using English when you write a program. This especially includes naming comments and variables, classes, functions etc. You never know if eventually someone who doesn't speak your language has to read your code (e.g. when you post it on SO). Second of all, I'm unsure as to what part in your code you're refering to; there are two different methods in which HTML is being parsed.
Anyway, I assume you're refering to doInBackground()
from class Logo
. You basically already achieved exactly what you're looking for in the ParsingPaginaWeb
class. You loop over each found <img>
and fetch the respective image:
for(Element img : document.select("div.news-col-0 img[src]")) {
String src = img.attr("src");
InputStream is = new java.net.URL(src).openStream();
Bitmap bitmap = BitmapFactory.decodeStream(is);
/* save the bitmap or pass it somewhere else,
otherwise, it will simply be overwritten */
}
You are probably going to have to change the data types in which you store the bitmaps, since you're retrieving more than one. You could use a List<Bitmap>
to store them for example.
来源:https://stackoverflow.com/questions/21219448/parsing-jsoup-list