Android : How to use Picasso Library with this ASYNCTASK

on Sunday, August 17, 2014



class LoadNews extends AsyncTask<String, String, String> {

@Override
protected void onPreExecute() {
// TODO Auto-generated method stub
super.onPreExecute();
// change this to some kind of animation for loading... to prevent
// changing fields when task is running
pDialog = new ProgressDialog(Home.this);
pDialog.setMessage("Loading news...");
pDialog.setIndeterminate(false);
pDialog.setCancelable(false);
pDialog.show();
}

@Override
protected String doInBackground(String... arg0) {
// TODO Auto-generated method stub


mNewsList = new ArrayList<HashMap<String, String>>();
JSONParser jParser = new JSONParser();
JSONObject json = jParser.getJSONFromUrl(URL);
try {
mNews = json.getJSONArray(TAG_POSTS);
for (int i = 0; i < mNews.length(); i++) {
JSONObject c = mNews.getJSONObject(i);
String news_id = c.getString(TAG_ID);
String title = c.getString(TAG_TITLE);
String body = c.getString(TAG_BODY);
String image = c.getString(TAG_IMAGE);
//JSONObject image = c.getJSONObject(TAG_IMAGE);
String date = c.getString(TAG_DATE);


HashMap<String, String> map = new HashMap<String, String>();
map.put(TAG_ID, news_id);
map.put(TAG_TITLE, title);
map.put(TAG_BODY, body);
map.put(TAG_IMAGE, image);
map.put(TAG_DATE, date);
map.put(TAG_USERNAME, date);

mNewsList.add(map);
}} catch (JSONException e) {
e.printStackTrace();}
return null;
}


protected void onPostExecute(String file_url) {
// removing progress dialog after asynctask execution
pDialog.dismiss();

ListAdapter adapter = new SimpleAdapter(Home.this, mNewsList,
R.layout.news_list,new String[]

{ TAG_TITLE,TAG_BODY,TAG_IMAGE,TAG_DATE },
new int[] { R.id.tvTitle, R.id.tvBody, R.id.tvDate});

ImageView a = (ImageView)findViewById(R.id.ivImage);

Picasso.with(Home.this).load(TAG_IMAGE).into(a);


newsList.setAdapter(adapter);


newsList.setOnItemClickListener(new OnItemClickListener(){

@Override
public void onItemClick(AdapterView<?> arg0, View arg1,int arg2, long arg3) {
// TODO Auto-generated method stub
HashMap<String, String> hashMap = mNewsList.get(arg2);
String news_id = hashMap.get(TAG_ID).toString();
Intent i = new Intent(Home.this, SingleNews.class);
i.putExtra("news_id", news_id);
startActivity(i);
}

});

}
}


How can i load an image using picasso library... i already tried it but it display force close on my screen i just add this one " ImageView a = (ImageView)findViewById(R.id.ivImage); Picasso.with(Home.this).load(TAG_IMAGE).into(a); " on my post execute


0 comments:

Post a Comment