40983

Custom Listview with Text and image

Question:

I have developed a listview,listview displaying image and text.1st have to download image and text form web-service then have to display because it takes more time so we thought 1st bind text in listview and use AsyncTaskand as soon as image download image will be shown in the listview in the background of activity.But i'm unable to do that i have done some coding and it download all image and then bind both image and text(in this case we need to bind listview two times 1st before startDownload image and 2nd after download image.So if any has some idea please suggest me.

<strong>Code</strong>

public class LoadImg extends AsyncTask<String, Void, Bitmap> { Context context; String img; InputStream is = null; Bitmap bitmap = null; public LoadImg(Context context, String img) { // TODO Auto-generated constructor stub this.context = context; this.img = img; } @Override protected Bitmap doInBackground(String... params) { // TODO Auto-generated method stub Bitmap bitmap = downImg(); System.out .println("Value of bitmap=====================================" + bitmap); return bitmap; } private Bitmap downImg() { // TODO Auto-generated method stub Bitmap bitmap = null; if (img == null) { bitmap = null; } else { URL url = null; try { url = new URL(img); } catch (MalformedURLException e) { // TODO Auto-generated catch block e.printStackTrace(); } URLConnection connection = null; try { connection = url.openConnection(); } catch (IOException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } try { is = connection.getInputStream(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } bitmap = BitmapFactory.decodeStream(is); System.out.println("TV Image===" + bitmap); } return bitmap; } }

Answer1:

Try this <a href="http://www.androidchennai.blogspot.in/2012/03/android-listview-example.html" rel="nofollow">example</a> or use any adapter like this

<blockquote>

WeatherAdapter.java

</blockquote> public class WeatherAdapter extends ArrayAdapter<Weather>{ Context context; int layoutResourceId; Weather data[] = null; public WeatherAdapter(Context context, int layoutResourceId, Weather[] data) { super(context, layoutResourceId, data); this.layoutResourceId = layoutResourceId; this.context = context; this.data = data; } @Override public View getView(int position, View convertView, ViewGroup parent) { View row = convertView; WeatherHolder holder = null; if(row == null) { LayoutInflater inflater = ((Activity)context).getLayoutInflater(); row = inflater.inflate(layoutResourceId, parent, false); holder = new WeatherHolder(); holder.imgIcon = (ImageView)row.findViewById(R.id.imgIcon); holder.txtTitle = (TextView)row.findViewById(R.id.txtTitle); row.setTag(holder); } else { holder = (WeatherHolder)row.getTag(); } Weather weather = data[position]; holder.txtTitle.setText(weather.title); holder.imgIcon.setImageResource(weather.icon); return row; } static class WeatherHolder { ImageView imgIcon; TextView txtTitle; } }

Recommend

  • Undefined ctx variable
  • How to use CompletableFuture without risking a StackOverflowError?
  • getResource() to a file on runtime
  • how to get the drawing graphic on picture box in c#
  • Load image without autoscaling in Android
  • Picasso Taking time to load images
  • insert a picture into database(sqlite) with java code. what should i do?
  • c# winform DrawToBitmap offscreen
  • How to discover Font Type?
  • Python PIL to extract number from image
  • How to upload specific List image using click on Upload button
  • In Java, how can I construct a File from a resource?
  • Ruby on Rails App deployed to heroku showing “We're sorry, but something went wrong”
  • Wrong row deleted from custom listview with spinner
  • Outputting SharePoint Hyperlink Column as URL
  • Spring: No transaction manager has been configured
  • JPA flush vs commit
  • Disable Kendo Autocomplete
  • Elasticsearch script query involving root and nested values
  • Laravel: Getting Session ID oddly truncates when using foreach
  • Why use database factory in asp.net mvc?
  • How to disable all widgets inside Panel or inside Composite?
  • Read text file that is not in the main package in a runnable jar
  • How do I configure context broker accept post requests from my remote sensor?
  • How to view images from protected folder with php?
  • how to avoid repetitive constructor in children
  • Highlight and Bold text in JTextPane
  • how to do an event when i swipe from fragment to the other
  • Javascript Callbacks with Object constructor
  • Akka Routing: Reply's send to router ends up as dead letters
  • Cannot Parse HTML Data Using Android / JSOUP
  • Linker errors when using intrinsic function via function pointer
  • Windows forms listbox.selecteditem displaying “System.Data.DataRowView” instead of actual value
  • LevelDB C iterator
  • unknown Exception android
  • Observable and ngFor in Angular 2
  • How can i traverse a binary tree from right to left in java?
  • How to Embed XSL into XML
  • UserPrincipal.Current returns apppool on IIS
  • Conditional In-Line CSS for IE and Others?