Adnroid开发中ListView使用SimpleAdapt

2017-02-20  本文已影响26人  kingZXY2009

学习Android的同学注意了!!!

学习过程中遇到什么问题或者想获取学习资源的话,欢迎加入Android学习交流群,群号码:364595326  我们一起学Android!

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.view.Menu;

//import  android.view.MenuItem;

public class MainActivity extends Activity

{

private ListView lv_main;

//

private String data[];

private String imageName;

private String name;

private String content;

@SuppressWarnings("rawtypes")

private Map map;

private String from[];

private int to[];

@SuppressWarnings("unchecked")

@override

protected void onCreate(Bundle savedInstanceState)

{

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

lv_main = (ListView)

findViewById(R.id.lv_main);

//准备集合数据

List> data = new

ArrayList>();

map = new

HashMap();

map.put("icon",R.drawable.f1);

map.put("name","name---1");

map.put("content","content---1");

data.add(map);

for(int i = 2;i <= 10;i ++){

name =

"name---";

content = "content---";

imageName = "R.drawable.f";

name = name+i;

content = content+i;

imageName = imageName+i;

map = new

HashMap();

map.put("icon",imageName);

map.put("name",name);

map.put("content",content);

data.add(map);

}

//map对象key的数组,用于得到value的值

from = new

String[]{"icon","name","content"};

//item布局文件中子view的id数组

to = new

int[]{R.id.iv_item_icon,R.id.tv_item_name,R.id.tv_item_content};

//准备SimpleAdapter

SimpleAdapter simpleAdapter=

newSimpleAdapter(this,data,R.layout.item_simple_adapter,from,to);

//设置Adapter显示列表

lv_main.setAdapter(simpleAdapter);

}

}

请问为什么用下面的代码,可以显示图片

map = new HashMap();

map.put("icon",R.drawable.f1);

map.put("name","name---1");

map.put("content","content---1");

data.add(map);

而用下面的for循环,不能显示图片

for(int i = 2;i <= 10;i++){

name= "name---";

content = "content---";

imageName = "R.drawable.f";

name= name+i;

content = content+i;

imageName =imageName+i;

map =new HashMap();

map.put("icon",imageName);

map.put("name",name);

map.put("content",content);

data.add(map);

}

请各位大神指点,谢谢!!!

学习过程中遇到什么问题或者想获取学习资源的话,欢迎加入Android学习交流群,群号码:364595326  我们一起学Android!

上一篇下一篇

猜你喜欢

热点阅读