YXTS122 2018-10-07 03:53 采纳率: 100%
浏览 488
已采纳

这些子项内容怎么是空的?

 public class MainActivity extends Activity {

    private ListView lv; 
    private List<Map<String, Object>> data; 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
        super.onCreate(savedInstanceState); 
        setContentView(R.layout.activity_main); 
        lv = (ListView)findViewById(R.id.lv); 
        //获取将要绑定的数据设置到data中 
        data = selectRoadsStaus(); 
        MyAdapter adapter = new MyAdapter(this); 
        lv.setAdapter(adapter); 
    } 

    public List<Map<String, Object>> selectRoadsStaus(){
        Map<String,Object> map = new HashMap<String,Object>();
        List<Map<String, Object>> list = new ArrayList<Map<String,Object>>();
        for(int i = 1; i <= 5 ; i++){
            Map<String,Object> map1 = new HashMap<String,Object>();
            map1.put("num",String.valueOf(i));
            if("1".equals(String.valueOf(i))){
                map1.put("bak",0xff1A78FE);//map.get(String.valueOf(i))
            }else if("2".equals(String.valueOf(i))){
                map1.put("bak", Color.alpha(0xff9FC95D));//map.get(String.valueOf(i))
            }else if("3".equals(String.valueOf(i))){
                map1.put("bak", Color.alpha(0xffFFC95C));//map.get(String.valueOf(i))
            }else if("4".equals(String.valueOf(i))){
                map1.put("bak", 0xffE16567);//map.get(String.valueOf(i))
            }else if("5".equals(String.valueOf(i))){
                map1.put("bak", Color.alpha(0xffE60513));//map.get(String.valueOf(i))
            }
            list.add(map1);
        }
        System.out.print(list);
        return list;
    }


    //ViewHolder静态类 
    static class ViewHolder 
    { 
        public TextView num; 
        public TextView bak; 
    } 

    public class MyAdapter extends BaseAdapter 
    {     
        private LayoutInflater mInflater = null; 
        private MyAdapter(Context context) 
        { 
            //根据context上下文加载布局,
            this.mInflater = LayoutInflater.from(context); 
        } 

        @Override 
        public int getCount() { 
            //How many items are in the data set represented by this Adapter. 
            //在此适配器中所代表的数据集中的条目数 
            return data.size(); 
        } 

        @Override 
        public Object getItem(int position) { 
            // Get the data item associated with the specified position in the data set. 
            //获取数据集中与指定索引对应的数据项 
            return position; 
        } 

        @Override 
        public long getItemId(int position) { 
            //Get the row id associated with the specified position in the list. 
            //获取在列表中与指定索引对应的行id 
            return position; 
        } 

        //Get a View that displays the data at the specified position in the data set. 
        //获取一个在数据集中指定索引的视图来显示数据 
        @Override 
        public View getView(int position, View convertView, ViewGroup parent) { 
            ViewHolder holder = null; 
            //如果缓存convertView为空,则需要创建View 
            if(convertView == null) 
            { 
                holder = new ViewHolder(); 
                //根据自定义的Item布局加载布局 
                convertView = mInflater.inflate(R.layout.list_item, null);  
                holder.num = (TextView)convertView.findViewById(R.id.tv); 
                holder.bak = (TextView)convertView.findViewById(R.id.info); 
                //将设置好的布局保存到缓存中,并将其设置在Tag里,以便后面方便取出Tag 
                convertView.setTag(holder); 
            }else 
            { 
                holder = (ViewHolder)convertView.getTag(); 
            } 
           // holder.img.setBackgroundResource((Integer)data.get(position).get("img")); 
            holder.num.setText((String)data.get(position).get("title")); 
            holder.bak.setText((String)data.get(position).get("info")); 

            return convertView; 
        } 

    } 
}

图片说明

  • 写回答

2条回答 默认 最新

  • 大王来寻春 2018-10-08 06:05
    关注

    holder.num.setText((String)data.get(position).get("title"));
    holder.bak.setText((String)data.get(position).get("info"));

    selectRoadsStaus 这个方法在构造map的时候,根本没有用 title info 这两个key。那绘制list的时候,怎么从map里面获取title和info

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 【悬赏】canvas 椭圆形内小球边缘与小球碰撞检测
  • ¥50 Delphi5环境下图片文件怎么转换成Base64编码?
  • ¥15 将GPDO_0引|脚设首成PWM1的输出引脚 实验编程该怎么写
  • ¥50 阿里云服务器 CentOS7.9 搭建 openvpn 服务
  • ¥100 开源软件弱点处理规范
  • ¥15 excel如何根据文件名自动搜索并批量导入文件?
  • ¥15 VScode 用户代码片段图标
  • ¥15 streamingtool
  • ¥15 MATLAB图像问题
  • ¥20 树莓派5做人脸情感识别与反馈系统