关闭

adapter(适配器)

56人阅读 评论(0) 收藏 举报
分类:
    关键字  ListView ArrayAdapter , SimpleAdapter,自定义适配器
    当我们做界面的时候,想要界面跑起来,同时但我们点击的时候,机器做出回应,这时候我们
    首先就要用到
    ListView 实现界面一条一条的排列,当界面满格时可以向下滑动,但是每条界面没有信息,当
    我们想要有信息时就要用到adapter(适配器),他是数据源与ListView之间建立桥梁,是实现可编辑的信息自由显示,类似于View控件,
    但是可以对每一条编辑页面的快速生成,避免了每一条信息的都需要重复编辑
    首先是ArrayAdapter
    ArrayAdapter的实现需要1.上下文(context) 2.一条TextView.xml文档,里面不能放任何其他控件 3.一维数组
    public class List_Array_Activity extends AppCompatActivity {

    public ListView mListView;
    
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_list__array_layout);//本类的activity_list__array_layout.xml 里面需要定义一个ListView控件

        mListView= (ListView) findViewById(R.id.ListView_first_view);//定义id好找
        
        String[] array = new String[]{"java","j2se","android","ui","css","structs","javascript","html","xml"};//一维数组的每一个对象都是依次显示的对象
        ArrayAdapter arrayadapter = new ArrayAdapter(this,R.layout.activity_demo__text_view_layout,array);
        //this指代context上下文,activity_demo__text_view_layout是TextView.xml文档,array是数组

        mListView.setAdapter(arrayadapter);//注册启动
    }
}
  SimpleAdapter是实现可编辑页面的适配器
  关键字 1.context应用上下文 2.编辑页面的xml文档 3.HashMap的键值对的键 4.自定义的界面xml文档里相应的编码
  public class Simple_Adapter_Activity extends AppCompatActivity implements AdapterView.OnItemClickListener{//点击是实现事件回应

    public ListView mListView;
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_simple__adapter_layout);
        mListView= (ListView) findViewById(R.id.ListView_Simple);

       ArrayList arrayList =listData();

       String[] from = {"icon","title","content"};//HashMap的键值对的键,用来标记不同的事件
       
       int[] to  = {R.id.simple_demo_imageview,R.id.simple_demo_textviewtitle,R.id.simple_demo_textviewcontent};
       //to 里面的每一id值都是自定义的界面xml文档里相应的编码
       SimpleAdapter simpleAdapter = new SimpleAdapter(this,arrayList,R.layout.activity_demo__simple_layout,from,to);
        

       mListView.setAdapter(simpleAdapter);
      
        mListView.setOnItemClickListener(this);//注册注册注册
    }
    public ArrayList listData(){
        ArrayList arrayList = new ArrayList();
        HashMap<String,Object> item = new HashMap<>();
        item.put("icon",R.drawable.liying6);//"icon"HashMap的键值对的键 R.drawable.liying6是具体的值
        item.put("title","赵丽颖");
        item.put("content","资料");
        arrayList.add(item);

        item = new HashMap<>();
        item.put("icon",R.drawable.liying6);
        item.put("title","赵丽颖");
        item.put("content","资料");
        arrayList.add(item);
  return arrayList;
    }

    @Override
    public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {//接口 AdapterView.OnItemClickListener的方法
        switch(i){//HashMap的值从零开始
            case 0:
                Intent intent = new Intent(this, Android_log_Activity.class);
                startActivity(intent);
                break;
            case 1:
               intent = new Intent(this, Button_activity.class);
                startActivity(intent);
                break;
            case 2:
              intent = new Intent(this, Code_Activity.class);
                startActivity(intent);
                break;
            case 3:
                intent = new Intent(this, Linear_Activity.class);
                startActivity(intent);
                break;
        }
        MySimpleAdapter mySimpleAdapter = (MySimpleAdapter) adapterView.getAdapter();//得到mySimpleAdapter不能用new
        HashMap<String ,Object> item = (HashMap<String, Object>) mySimpleAdapter.getItem(i);//得到HashMap的具体对象
        String title = (String) item.get("title");
        String content = (String) item.get("content");
        
        Snackbar.make(view,title,Snackbar.LENGTH_SHORT).show();

    }
    自定义适配器
    客户的想法总是千奇百怪,为了满足需要,我们可以自己定义一个适配器
    public class MySimpleAdapter extends BaseAdapter {//自定义适配器必须继承BaseAdapter
    
    public List<HashMap<String,Object>> list = new ArrayList<>();//本适配器打算的用途
    public LayoutInflater layoutInflater;//解析器,布局文件的解析全靠它
    
    public MySimpleAdapter(Context context,List<HashMap<String,Object>> list){
        this.list=list;
        layoutInflater = LayoutInflater.from(context);
    }
    public int getCount() {
        return list.size();//List<HashMap<String,Object>>所装的对象多少
    }

    @Override
    public Object getItem(int i) {
        return list.get(i);//得到HashMap的具体对象
    }

    @Override
    public long getItemId(int i) {//得到HashMap的具体对象的id
        return i;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        View v = layoutInflater.inflate(R.layout.activity_demo__simple_layout,null);//这是我们想要界面的视图activity_demo__simple_layout是xml基本布局,v 的布局就是它

        ImageView iconimag = (ImageView) v.findViewById(R.id.simple_demo_imageview);//activity_demo__simple_layout布局里每个控件的id
        TextView titletxt = (TextView) v.findViewById(R.id.simple_demo_textviewtitle);
        TextView contenttxt = (TextView) v.findViewById(R.id.simple_demo_textviewcontent);

        HashMap<String,Object> item = (HashMap<String, Object>) getItem(i);//获取存放的每一条项
        int icon = (int) item.get("icon");//通过键获得值

        String title = (String) item.get("title");
        String content  = (String) item.get("content");

        iconimag.setImageResource(icon);//从新赋值
        titletxt.setText(title);
        contenttxt.setText(content);
        return v;
    }
}
    
   


0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:8888次
    • 积分:631
    • 等级:
    • 排名:千里之外
    • 原创:57篇
    • 转载:4篇
    • 译文:0篇
    • 评论:0条
    文章分类