我正在尝试显示带有图像和文本的 ListView,我使用了 Simpleadapter 类,但我无法显示 ListView 任何人都可以帮助我,我做错了什么?
这是我的代码:
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;
}
}
}
这是我的布局 xml 文件:
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>
主机列表.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>