package com.example.basadper;import java.util.ArrayList;import java.util.HashMap;import java.util.Map;import android.os.Bundle;import android.app.Activity;import android.content.Context;import android.view.LayoutInflater;import android.view.Menu;import android.view.View;import android.view.ViewGroup;import android.widget.BaseAdapter;import android.widget.ImageView;import android.widget.ListView;import android.widget.TextView;public class MainActivity extends Activity {private ArrayList<Map<String,Object>>data;private ListView lV; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); lV = (ListView) findViewById(R.id.listView1); MyAdaper adaper = new MyAdaper(this); lV.setAdapter(adaper); } public ArrayList<Map<String,Object>> getdata(){ ArrayList<Map<String,Object>> list = new ArrayList<Map<String,Object>>(); for(int i=0;i<=100;i++){ Map<String,Object> map1 = new HashMap<String,Object>(); map1.put("img", R.id.imageView1); map1.put("TextView", R.id.textView1); map1.put("TextView", R.id.textView2); list.add(map1); } return list; } static class ViewHolder { public ImageView img; public TextView title; public TextView info; } class MyAdaper extends BaseAdapter{ private LayoutInflater mInflater = null; private MyAdaper(Context context) { //根据context上下文加载布局,这里的是Demo17Activity本身,即this this.mInflater = LayoutInflater.from(context); } @Override public int getCount() { // TODO Auto-generated method stub return getdata().size(); } @Override public Object getItem(int position) { // TODO Auto-generated method stub return position; } @Override public long getItemId(int position) { // TODO Auto-generated method stub return position; } @Override public View getView(int position, View convertView, ViewGroup parent) { // TODO Auto-generated method stub ViewHolder holder = null; //如果缓存convertView为空,则需要创建View if(convertView == null) { holder = new ViewHolder(); //根据自定义的Item布局加载布局 convertView = holder.img = (ImageView)convertView.findViewById(R.id.imageView1); holder.title = (TextView)convertView.findViewById(R.id.textView1); holder.info = (TextView)convertView.findViewById(R.id.textView2); //将设置好的布局保存到缓存中,并将其设置在Tag里,以便后面方便取出Tag convertView.setTag(holder); }else { holder = (ViewHolder)convertView.getTag(); } holder.img.setBackgroundResource((Integer)data.get(position).get("img")); holder.title.setText((String)data.get(position).get("title")); holder.info.setText((String)data.get(position).get("info")); return convertView; } } }
4 回答
拟人
TA贡献37条经验 获得超15个赞
空指针异常一般是指一个为空null的对象去调用方法抛出的。因为null是没有方法的,空指针异常一般都会将你有问题的代码给表示出来,你去报错信息里找找是哪一行报错了,一般来说是可以直接点击报错对应到相应的大致位置,然后你再去分析,或者是断点进去看到底是哪个对象是null的,以后提问尽量不要直接扔代码,除非那种很熟的代码,不然别人估计也看的很难受。
- 4 回答
- 0 关注
- 1536 浏览
添加回答
举报
0/150
提交
取消