• ArrayAdapter、SimpleAdapter和BaseAdapter示例代码


    import android.content.Context;
    import android.util.Pair;
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.ArrayAdapter;
    import android.widget.BaseAdapter;
    import android.widget.SimpleAdapter;
    import android.widget.TextView;
    
    import com.example.rex.mytest.R;
    
    import java.util.ArrayList;
    import java.util.HashMap;
    import java.util.List;
    import java.util.Map;
    
    /**
     * Created by Rex on 2016/3/7.
     */
    public class Sample {
    
        protected void testAdapter() {
            Context context = null;
            final String[] data = new String[]{"1", "2", "3", "4", "5"};
            
            
            // ---------------------------------------ArrayAdapter-----------------------------------------
            ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(context, R.layout.dialog_list_item, data);//data是数组或List集合
    
            
            // ---------------------------------------SimpleAdapter-----------------------------------------
            List<Map<String, Object>> mData = new ArrayList<Map<String, Object>>();
            for (int i = 0; i < 10; i++) {
                Map<String, Object> item = new HashMap<String, Object>();
                item.put("image", R.drawable.star);
                item.put("title", "title-" + i);
                item.put("text", "text-" + i);
                mData.add(item);
            }
            SimpleAdapter simpleAdapter = new SimpleAdapter(context, mData, R.layout.list_item, new String[]{"image", "title", "text"}, new int[]{R.id.image, R.id.title, R.id.text});
    
            
            // ---------------------------------------BaseAdapter is abstract class-----------------------------------------
            class MyAdapter extends BaseAdapter {
                private Context context;
                private List<Pair<String, String>> datas;
    
                public MyAdapter(Context context, List<Pair<String, String>> list) {
                    this.context = context;
                    datas = list;
                }
    
                @Override
                public int getCount() {
                    return datas.size();
                }
    
                @Override
                public Object getItem(int position) {
                    return datas.get(position);
                }
    
                @Override
                public long getItemId(int position) {
                    return position;
                }
    
                @Override
                public View getView(int position, View convertView, ViewGroup parent) {
                    if (convertView == null) {
                        convertView = View.inflate(context, R.layout.list_item, null);
                    }
                    TextView title = (TextView) convertView.findViewById(R.id.title);
                    TextView text = (TextView) convertView.findViewById(R.id.text);
                    Pair<String, String> pair = datas.get(position);
                    title.setText(pair.first);
                    text.setText(pair.second);
                    return convertView;
                }
    
            }
        }
    
    }
     
  • 相关阅读:
    flexible
    arcgis
    vue 语法糖
    sass 的安装 编译 使用
    nodeJs
    微信小程序
    linux cgroups 简介
    git命令
    sublime笔记
    工程优化学习(进退法、黄金分割法、二次插值法、三次插值法、最速下降法)
  • 原文地址:https://www.cnblogs.com/wytings/p/5249203.html
Copyright © 2020-2023  润新知