Android : Set image from WebService as icon in AlertDialog

on Saturday, March 28, 2015


i am currently making an app, in which , when user long presses an item of list it pops up an alert dialog with some info related to listItem.


I want to set Icon from path provided by my API. TeamFlagIC is in TeamsMapper class, which contains the path of image.


This is my onPostExecute Method.



@Override
protected void onPostExecute(Object result) {
if (progressDialog.isShowing()) {
progressDialog.dismiss();
}

String json = result.toString();
Gson gson = new Gson();
Type type = new TypeToken<List<TeamsMapper>>() {}.getType();
final ArrayList<TeamsMapper> pd = (ArrayList<TeamsMapper>) gson.fromJson(json, type);
dataAdapter = new TeamsCustomAdapter(Teams.this, pd);
listView.setAdapter(dataAdapter);

imageLoader = new ImageLoader(Teams.this);

final AlertDialog.Builder ImageAlertBuilder = new AlertDialog.Builder(Teams.this);
inflater = (LayoutInflater) (Teams.this).getSystemService(LAYOUT_INFLATER_SERVICE);


listView.setOnItemLongClickListener(new OnItemLongClickListener() {


@Override
public boolean onItemLongClick(AdapterView<?> parent, View v,int position, long id) {

View layout = inflater.inflate(R.layout.popup_from_list,(ViewGroup) findViewById(R.id.linear_layout_popup));
imgViewDialogImage = (ImageView) layout.findViewById(R.id.imgView_fullimage);
txtViewTeamsdata = (TextView) layout.findViewById(R.id.txtView_fullimage);

imageLoader.DisplayImage(pd.get(position).TeamFlagFS,imgViewDialogImage);

String iurl = pd.get(position).TeamFlagIC;
bitMap = getBitmapFromURL(iurl);
drawable = new BitmapDrawable(getResources(),bitMap);
ImageAlertBuilder.setIcon(drawable); //<---
ImageAlertBuilder.setTitle(" "+ pd.get(position).TeamName);
ImageAlertBuilder.setView(layout);
ImageAlertBuilder.setPositiveButton(android.R.string.ok,new OnClickListener() {

@Override
public void onClick(DialogInterface dialog,
int arg1) {
dialog.dismiss();
}
});

txtViewTeamsdata.setText("\n " + pd.get(position).TeamName
+ " \n "+ pd.get(position).TeamCode
+ "\n" + pd.get(position).LeagueName);
ImageAlertBuilder.create();
ImageAlertBuilder.show();
return false;
}
});


Image downloader from Url



public static Bitmap getBitmapFromURL(String src) {
try {
URL url = new URL(src);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setDoInput(true);
connection.connect();
InputStream input = connection.getInputStream();
Bitmap myBitmap = BitmapFactory.decodeStream(input);
return myBitmap;
} catch (IOException e) {
// Log exception
return null;
}
}

0 comments:

Post a Comment