package arrayadapter.listview.immoc.myapplication001;
import android.app.Activity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class MainActivity extends Activity{
private ListView listView1;
private ArrayAdapter<String> arr_adapter;
private SimpleAdapter sim_adpter;
private List<Map<String , Object>> datalist;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listView1=(ListView)findViewById(R.id.listview);
// 新建一个数据源适配器
// 新建一个数据源
String[]arr_data ={"慕课1","慕课2","慕课3","慕课4","慕课5","慕课6"};
arr_adapter =new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,arr_data);
// 视图加载适配器
// listView1.setAdapter(arr_adapter);
sim_adpter=new SimpleAdapter(this,getData(),R.layout.item ,new String [] {"pic","txt"},new int [] {R.id.pic,R.id.txt});
listView1.setAdapter(sim_adpter);
}
private List<Map<String,Object>> getData()
{
for(int i=0;i<20;i++)
{
Map<String,Object>map = new HashMap<String,Object>();
map.put("pic",R.mipmap.ic_launcher);
map.put("txt","慕课网"+i+1);
datalist.add(map);
}
return datalist;
}
}<?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">
<ImageView
android:id="@+id/pic"
android:layout_width="80dp"
android:layout_height="80dp"
android:layout_margin="15dp"
android:src="@mipmap/ic_launcher"
/>
<TextView
android:id="@+id/txt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:text="demo"
/>
</LinearLayout><?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:orientation="vertical"
>
<ListView
android:id="@+id/listview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
></ListView>
</LinearLayout>
1 回答
qq_凯_29
TA贡献7条经验 获得超1个赞
for(int i=0;i<20;i++) { Map<String,Object>map = new HashMap<String,Object>(); map.put("pic",R.mipmap.ic_launcher); map.put("txt","慕课网"+i+1); datalist.add(map); } return datalist;
没初始化吧,for前先
datalist=new List();
添加回答
举报
0/150
提交
取消