Adapter(自定义适配器)Listview

Android系统中自带的SimpleAdapter,有很多局限,所以很多用自定义的

//系统的SimpleAdapter

a.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">
<ListView
    android:id="@+id/list_view"
    android:scrollbars="none"
    android:listSelector="#00000000"
    android:divider="@null"
    android:dividerHeight="40dp"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"></ListView>
</LinearLayout>

listview.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">
    <ImageView
        android:layout_centerVertical="true"
        android:id="@+id/listview_tx_iv"
        android:src="@mipmap/ic_launcher"
        android:layout_marginLeft="20dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
    <TextView
        android:id="@+id/listview_name_tv"
        android:text="姓名:"
        android:layout_marginLeft="35dp"
        android:layout_alignTop="@+id/listview_tx_iv"
        android:layout_toRightOf="@+id/listview_tx_iv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
    <TextView
        android:id="@+id/listview_na_tv"
        android:text="123"
        android:layout_alignBottom="@+id/listview_name_tv"
        android:layout_alignParentRight="true"
        android:layout_marginRight="20dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
    <TextView
        android:id="@+id/listview_age_tv"
        android:text="年龄:"
        android:layout_alignLeft="@+id/listview_name_tv"
        android:layout_toRightOf="@+id/listview_tx_iv"
        android:layout_alignBottom="@+id/listview_tx_iv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
    <TextView
        android:id="@+id/listview_ag_tv"
        android:text="123"
        android:layout_alignLeft="@+id/listview_na_tv"
        android:layout_alignBottom="@+id/listview_age_tv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        />

</RelativeLayout>

UIUU.java

package com.example.shuai.listview;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by shuai on 2016/5/6.
 */
public class UIUU extends Activity {
    List<Map<String,String>> list=new ArrayList<>();
    ListView lv;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        inview();
        setContentView(R.layout.a);
        lv= (ListView) findViewById(R.id.list_view);
        SimpleAdapter adapter=new SimpleAdapter(this,list,R.layout.listview,new String[]{"name","age"},new int[]{R.id.listview_na_tv,R.id.listview_ag_tv});
        lv.setAdapter(adapter);
        lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Toast.makeText(UIUU.this,  list.get(position).toString(),Toast.LENGTH_SHORT).show();
            }
        });
    }
    void inview(){
        for(int i=0;i<20;i++){
            Map<String,String> map = new HashMap<>();
            map.put("name","lili"+i);
            map.put("age","12"+i);
            list.add(map);
        }
    }
}

//自定义Adapter

list.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">
    <ListView
        android:id="@+id/list_view"
        android:dividerHeight="40dp"
        android:scrollbars="none"
        android:divider="#00000000"
        android:layout_width="match_parent"
        android:layout_height="match_parent"></ListView>
</LinearLayout>

a.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">
<ImageView
    android:id="@+id/a_tx_iv"
    android:background="@mipmap/ic_launcher"
    android:layout_width="50dp"
    android:layout_centerVertical="true"
    android:layout_marginLeft="20dp"
    android:layout_height="50dp" />
    <TextView
        android:id="@+id/a_name_tv"
        android:text="姓名:"
        android:layout_toRightOf="@+id/a_tx_iv"
        android:layout_alignTop="@+id/a_tx_iv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
    <TextView
        android:id="@+id/a_nameshow_tv"
        android:text="123"
        android:layout_alignParentRight="true"
        android:layout_marginRight="20dp"
        android:layout_alignBottom="@+id/a_name_tv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
    <TextView
        android:id="@+id/a_age_tv"
        android:text="年龄:"
        android:layout_width="wrap_content"
        android:layout_alignLeft="@+id/a_name_tv"
        android:layout_alignBottom="@+id/a_tx_iv"
        android:layout_height="wrap_content" />
    <TextView
        android:id="@+id/a_agshow_tv"
        android:text="123"
        android:layout_alignBottom="@+id/a_age_tv"
        android:layout_alignLeft="@+id/a_nameshow_tv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
</RelativeLayout>

MyAdapter.java

package com.example.shuai.adapter;

import android.content.Context;
import android.support.v4.view.LayoutInflaterFactory;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Adapter;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.List;

/**
 * Created by shuai on 2016/5/6.
 */
public class MyAdapter extends BaseAdapter {
    Context context;
    List<User> list;

    public MyAdapter(Context context, List<User> list) {
        this.context = context;
        this.list = list;
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return position;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder = null;
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.a, null);
            viewHolder = new ViewHolder();
            viewHolder.name = (TextView) convertView.findViewById(R.id.a_nameshow_tv);
            viewHolder.age = (TextView) convertView.findViewById(R.id.a_agshow_tv);
            viewHolder.iv = (ImageView) convertView.findViewById(R.id.a_tx_iv);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        User u = list.get(position);
        viewHolder.name.setText(u.getName());
        viewHolder.age.setText(u.getAge());
        viewHolder.iv.setBackgroundResource(u.getImg());
        return convertView;
    }

    class ViewHolder {
        TextView name;
        TextView age;
        ImageView iv;
    }
}

MainActivity.java

package com.example.shuai.adapter;

import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {

    ListView lv;
    List<User> list;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.list);
        inview();
        lv= (ListView) findViewById(R.id.list_view);
        MyAdapter myAdapter=new MyAdapter(this,list);
        lv.setAdapter(myAdapter);
        lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Toast.makeText(MainActivity.this,list.get(position).toString(),Toast.LENGTH_SHORT).show();
            }
        });
    }

    void inview(){
        list=new ArrayList<>();
        for(int i=0;i<2;i++){
            User u=new User("lili","2"+i,R.mipmap.ic_launcher);
            list.add(u);
    }
        for(int i=0;i<2;i++){
            User u=new User("lvlv","1"+i,R.mipmap.bigtree);
            list.add(u);
        }
        for(int i=0;i<5;i++){
            User u=new User("kk","3"+i,R.mipmap.star);
            list.add(u);
        }
    }
}

小Demo自用

//m.notifyDataSetChanged();
//lv.invalidate();
刷新listview,和适配器

posted on 2016-05-06 21:25  huaihuaibuguai  阅读(224)  评论(0编辑  收藏  举报

导航