Adapter

作者: _弓长_大人 | 来源:发表于2018-09-25 12:43 被阅读20次

    ArrayAdapter

    public class act3 extends AppCompatActivity{
        ListView listView;
    
    
        String[] cities={"chengdu","beijing","guangzhou","sichuan","bazhong"};
        ArrayList<String >arrayList;
        @Override
        protected void onCreate(@Nullable Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
    
            setContentView(R.layout.adater);
    
            listView=findViewById(R.id.listView);
            arrayList=new ArrayList<>(Arrays.asList(cities));
            // 数据适配器
    
            final ArrayAdapter<String> arrayAdapter=new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,arrayList);
    
            listView.setAdapter(arrayAdapter);
    
    
    
    
            listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                    Toast.makeText(act3.this,"you choose"+cities[position],Toast.LENGTH_SHORT).show();
                }
            });
    
    
    
            listView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
                @Override
                public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
    
                    arrayList.remove(position);
                    arrayAdapter.notifyDataSetChanged();
    
                    return  true;
                }
            });
    
        }
    }
    

    SimpleAdapter

    public class act3 extends AppCompatActivity{
        ListView listView;
    
    
        String[] cities={"chengdu","beijing","guangzhou","sichuan","bazhong"};
        ArrayList<HashMap<String ,String> >arrayList=new ArrayList<>();
        @Override
        protected void onCreate(@Nullable Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
    
            setContentView(R.layout.adater);
    
            listView=findViewById(R.id.listView);
    
    
            HashMap<String,String>hashMap=new HashMap<>();
            hashMap.put("city","chengdu");
            hashMap.put("province","sichuan");
            arrayList.add(hashMap);
    
            hashMap=new HashMap<>();
            hashMap.put("city","beijing");
            hashMap.put("province","shoudu");
            arrayList.add(hashMap);
    
    
            hashMap=new HashMap<>();
            hashMap.put("city","chongqing");
            hashMap.put("province","zhixia");
            arrayList.add(hashMap);
    
    
            hashMap=new HashMap<>();
            hashMap.put("city","xian");
            hashMap.put("province","shanxi");
            arrayList.add(hashMap);
    
    
    
            //数据适配器
    
            final SimpleAdapter simpleAdapter=new SimpleAdapter(this,arrayList,R.layout.city,new String[]{"city","province"},new  int[]{R.id.citycity,R.id.province});
    
            listView.setAdapter(simpleAdapter);
    
    
    
            listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                    Toast.makeText(act3.this,"you choose"+arrayList.get(position),Toast.LENGTH_SHORT).show();
                }
            });
    
    
    
            listView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
                @Override
                public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
    
                    arrayList.remove(position);
                    simpleAdapter.notifyDataSetChanged();
    
                    return  true;
                }
            });
    
        }
    }
    

    BaseAdapter
    .0

    public class act3 extends AppCompatActivity{
        ListView listView;
    
    
        String[] cities={"chengdu","beijing","guangzhou","sichuan","bazhong"};
        ArrayList<Person>arrayList=new ArrayList<>();
        @Override
        protected void onCreate(@Nullable Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
    
            setContentView(R.layout.adater);
    
            listView=findViewById(R.id.listView);
    
    
            arrayList.add(new Person("nanan",12));
            arrayList.add(new Person("gaoyueyang",34));
            arrayList.add(new Person("luyu",35));
            arrayList.add(new Person("qq",23));
    
    
    
    
    
            //数据适配器
    
         final PersonAdapter personAdapter=new PersonAdapter();
         listView.setAdapter(personAdapter);
    
            listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                    Toast.makeText(act3.this,"you choose"+arrayList.get(position),Toast.LENGTH_SHORT).show();
                }
            });
    
    
    
            listView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
                @Override
                public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
    
                    arrayList.remove(position);
                   personAdapter.notifyDataSetChanged();
    
                    return  true;
                }
            });
    
        }
    
    
        class PersonAdapter extends BaseAdapter{
    
            @Override
            public int getCount() {
                return arrayList.size();
            }
    
            @Override
            public Object getItem(int position) {
                return arrayList.get(position);
            }
    
            @Override
            public long getItemId(int position) {
                return position;
            }
    
            @Override
            public View getView(int position, View convertView, ViewGroup parent) {
                ViewHolder viewHolder;
    
                if(convertView ==null){
    
                    convertView= LayoutInflater.from(act3.this).inflate(R.layout.city,null);
    
                    viewHolder=new ViewHolder();
                    viewHolder.name=convertView.findViewById(R.id.citycity);
                    viewHolder.age=convertView.findViewById(R.id.province);
                    
                    convertView.setTag(viewHolder);
                }else {
                    viewHolder=(ViewHolder)convertView.getTag();
                }
                Person person=arrayList.get(position);
                viewHolder.name.setText(person.getName());
                viewHolder.age.setText(""+person.getAge());
                return  convertView;
    
            }
        }
        static  class  ViewHolder{
            TextView name;
            TextView age;
        }
    
    
    
    
    }
    
    

    相关文章

      网友评论

          本文标题:Adapter

          本文链接:https://www.haomeiwen.com/subject/dwxykftx.html