SimpleAdapter 编写,运行均没报错,运行后显示是空白的,没有内容。
求大神,学霸解惑。运行结果如下图:
MainActivity.java
package com.example.test_listview; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import android.app.Activity; import android.os.Bundle; import android.widget.ListView; import android.widget.SimpleAdapter; import android.widget.TextView; public class MainActivity extends Activity { private ListView listview_simAdapter; private SimpleAdapter sim_adapter; private List<Map<String, Object>> dataList; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); dataList=new ArrayList<Map<String,Object>>(); Map<String, Object> map=new HashMap<String, Object>(); map.put("pic",R.drawable.ic_launcher ); map.put("text", "123"); dataList.add(map); listview_simAdapter=(ListView)findViewById(R.id.listView_SinpleAdapter); listview_simAdapter.setAdapter(sim_adapter); sim_adapter=new SimpleAdapter(this, dataList,R.layout.sim_adapter_item,new String[]{"pic","text"},new int[]{R.id.image_button_sim_adpter,R.id.textview_simple_adpter}); } }
activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/container" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="horizontal" tools:context="com.example.test_listview.MainActivity" tools:ignore="MergeRootFrame" > <ListView android:id="@+id/listView_SinpleAdapter" android:layout_width="match_parent" android:layout_height="wrap_content" > </ListView> </LinearLayout>
sim_adpter_item
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="horizontal" > <ImageButton android:id="@+id/image_button_sim_adpter" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="20dp" android:layout_marginTop="10dp" android:src="@drawable/ic_launcher" /> <TextView android:id="@+id/textview_simple_adpter" android:layout_width="wrap_content" android:layout_height="57dp" android:layout_marginTop="10dp" android:gravity="center_vertical" android:text="测试数据" android:textSize="20sp" /> </LinearLayout>