第一次写,写的不好,多多指教!
SimpleAdapter是最方便的适配器,应用起来非常方便!!!
SimpleAdapter(Context context, List extends Map
按照构造的需要,往里面添加即可。
SimpleAdapter simpleAdapter=new SimpleAdapter(this,list,R.layout.baidu,
new String[]{"Image","name","mp"},new int[]{R.id.image1,R.id.text,R.id.text1});
Context context:上下文,一般简单应用直接写this即可
List extends Map
private List
public List
注意:map中放入的关键字“name”,“Image”,“mp”要与构造里面的String[]from,int[]to数组中的内容相同
int resource:设置List View单项布局Id,自己写布局.
新建一个布局文件baidu.xml
String[] from:存放Map中的关键字,int[] to存放baidu.xml文件中布局控件的id,要一 一对应,将Map关键字对应的内容传到相应布局控件中
new String[]{"Image","name","mp"},new int[]{R.id.image1,R.id.text,R.id.text1}
构建工程;
系统自建MainActivity,java和activity_main.xml;
1、activity_main.xml中添加List View控件:
2、自建另一个xml布局文件
上面的baidu.xml
3、MainActivity.java:
public class MainActivity extends AppCompatActivity {
private ListView listView;
private String[] name={"老大","老二","老三","老四","老五"};
private int image[]={R.drawable.i_a,R.drawable.i_b,R.drawable.i_c,R.drawable.i_d,R.drawable.i_e};
private List>list=new ArrayList<>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listView=findViewById(R.id.list_item);
list=ReList();
SimpleAdapter simpleAdapter=new SimpleAdapter(this,list,R.layout.baidu,
new String[]{"Image","name","mp"},new int[]{R.id.image1,R.id.text,R.id.text1});
listView.setAdapter(simpleAdapter);
}
public List> ReList(){
List>lists=new ArrayList<>();
for(int i=0;i map=new HashMap<>();
map.put("name",name[i]);
map.put("Image",image[i]);
map.put("mp","发送消息");
lists.add(map);
}
return lists;
}
}
两个xml文件,一个Java文件就可以创建一个漂亮的List View了