为什么ListView 显示空白??
package com.example.liusy.myalarm2; import android.nfc.Tag; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.util.Log; import android.widget.ListView; import android.widget.SimpleAdapter; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; public class No2SimpleList extends AppCompatActivity { private static final String TAG = "No2SimpleList"; private List<Map<String,Object>> dataList; private SimpleAdapter simpleAdapter; private ListView listView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_no2_simple_list); listView=(ListView)findViewById(R.id.ListViewSimpleList); dataList=new ArrayList<Map<String,Object>>(); getData(); Log.e(TAG, "is empty:"+dataList.isEmpty()); simpleAdapter =new SimpleAdapter(this,dataList,R.layout.support_simple_spinner_dropdown_item, new String[]{"pic","text"},new int[]{R.id.pic,R.id.text}); listView.setAdapter(simpleAdapter); Log.e(TAG, "simpleAdapter.getItem(1).toString(): " +simpleAdapter.getItem(1).toString()); } private List<Map<String,Object>> getData(){ for(int i=0;i<50;i++){ Map<String,Object> map =new HashMap<String,Object>(); String text="慕课网"+i; int id =R.drawable.ic_launcher_foreground; map.put("pic",R.drawable.ic_launcher_foreground); map.put("text","慕课网"+i); dataList.add(map); } return dataList; } }
主界面layout
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <ListView android:id="@+id/ListViewSimpleList" android:layout_width="match_parent" android:layout_height="match_parent"></ListView> </LinearLayout> 列表项layout
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="horizontal" android:layout_width="match_parent" android:layout_height="match_parent"> <ImageView android:id="@+id/pic" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="15dp" android:src="@drawable/ic_launcher_foreground" /> <TextView android:id="@+id/text" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="40sp" android:textColor="#609183" android:text="dd"/> </LinearLayout>
日志输出
06-20 07:04:47.518 7181-7181/com.example.liusy.myalarm2 E/No2SimpleList: is empty:false
06-20 07:04:47.519 7181-7181/com.example.liusy.myalarm2 E/No2SimpleList: simpleAdapter.getItem(1).toString(): {text=慕课网1, pic=2131099733}