I am trying to display ListView with Image and text,I have used Simpleadapter class,But I am unable to display ListView Could any one help me,what i did wrong?
Here My Code:
public class HostsActivity extends ListActivity {
public final static String ITEM_TITLE = "title";
public final static String ITEM_IMAGE = "Image";
private List<HostsProfile> hostsProfile1;
List<Map<String, ?>> security = new LinkedList<Map<String, ?>>();
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.hostslist);
Bundle recdData = getIntent().getExtras();
hostsProfile1 = recdData.getParcelableArrayList("hostsProfile");
List<Map<String, ?>> security1 = new LinkedList<Map<String, ?>>();
for (HostsProfile msg1 : hostsProfile1) {
// Log.v("image","Image path"+msg.getImage());
try {
URL url = new URL(msg1.getMediathumbnail());
HttpURLConnection con = (HttpURLConnection) url.openConnection();
InputStream is = con.getInputStream();
Bitmap img = BitmapFactory.decodeStream(is);
security.add(createItem(msg1.getTitle(), img));
} catch (Throwable t) {
}
}
/*for(int i=0; i<security.size(); i++){
security.remove(0);
}
*/
SimpleAdapter adapter1 = new SimpleAdapter(this, security1,
R.layout.image_text_layout1, new String[] { ITEM_IMAGE,
ITEM_TITLE }, new int[] { R.id.feed_image,
R.id.job_text });
adapter1.setViewBinder(new MyViewBinder());
this.setListAdapter(adapter1);
}
public Map<String, ?> createItem(String title, Bitmap Image) {
Map<String, Object> item = new HashMap<String, Object>();
item.put(ITEM_TITLE, title);
item.put(ITEM_IMAGE, Image);
return item;
}
//for ImageView
class MyViewBinder implements ViewBinder {
public boolean setViewValue(View view, Object data,
String textRepresentation) {
if ((view instanceof ImageView) & (data instanceof Bitmap)) {
ImageView image1 = (ImageView) view;
Bitmap bm = (Bitmap) data;
image1.setImageBitmap(bm);
return true;
}
return false;
}
}
}
Here are my layout xml files:
image_text_layout1:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal" android:layout_width="fill_parent"
android:layout_height="wrap_content">
<ImageView android:id="@+id/feed_image"
android:layout_width="60dp"
android:layout_height="60dp"
android:gravity="center_vertical"
android:paddingRight="3dp"
android:paddingLeft="2dp"/>
<TextView android:id="@+id/job_text"
android:layout_width="wrap_content"
android:layout_height="60dp"
android:textColor="#000000"
android:textStyle="bold"
android:gravity="center_vertical"/>
<!--android:paddingTop="5dip"
android:paddingBottom="28dip"
android:paddingLeft="8dip"
android:paddingRight="8dip" />-->
</LinearLayout>
hostslist.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/background"
>
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<LinearLayout
android:id="@+id/linearlayout01"
android:layout_width="300dp"
android:layout_height="45dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="52dp"
android:orientation="vertical" >
</LinearLayout>
<ListView
android:id="@android:id/list"
android:layout_width="fill_parent"
android:layout_height="230dp"
android:layout_alignParentRight="true"
android:layout_marginTop="107dp"
android:cacheColorHint="#00000000">
</ListView>
</RelativeLayout>
</LinearLayout>
Make a
CustomListview
project:CustomListView.java
Contact.java
ContactImageAdapter.java
main.xml
list.xml
Put icon in drwable, which is used in the main activity image array.
add Class For Lazy Loading FileCache.java
========
=========ImageLoader.java
======MemoryCache.java
=====Utils.java
====ContactImageAdapte.java
=====try This
I would suggest to you to use a custom listview with a custom adapter.
An example is available at the link below.
android-custom-listview-with-image-and-text
Use a viewholder for smooth scrolling and performance.
You can also use lazy list or universal image loader.
Both use caching.
Lazy List and Universal Image Loader
Check the below links for viewholder and loading bitmaps efficiently
Listview smooth scrolling using ViewHolder
The world of Listview talk by android developers at google The talk is about viewholder and list view performance.
Loading bitmaps efficiently from developer site. Load bitmaps efficiently.