• 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏吧

Android – Adapter 适配器

互联网 diligentman 2周前 (11-22) 7次浏览

Android 适配器

本文介绍两个Adapter:

ArrayAdapter:最简单的Adapter,只能展现一行文字。

XML文件:

<ListView
	android:id="@+id/listView"
	android:layout_width="match_parent"
	android:layout_height="match_parent" >
</ListView>

代码:

ListView listView = findViewById(R.id.listView);
String str[] = new String[]{"点我开通会员","我的QQ钱包","我的个性装扮","我的收藏","相册"};
ArrayAdapter arrayAdapter = new ArrayAdapter(getContext(),android.R.layout.simple_expandable_list_item_1,str);

效果图
Android - Adapter 适配器



BaseAdapter:最多使用的Adapter,实际开发中继承这个类重写相关方法。

XML文件:
主布局Android - Adapter 适配器

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal">

    <ListView
        android:id="@+id/listView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"></ListView>

</LinearLayout>

数据布局Android - Adapter 适配器

<?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">
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
    <ImageView
        android:id="@+id/imageView"
        android:layout_width="90dp"
        android:layout_height="90dp"
        android:layout_marginTop="10dp"
        android:layout_marginLeft="10dp"
        android:layout_marginRight="10dp"
        android:layout_marginBottom="10dp"
        android:background="@drawable/ic_launcher_background"></ImageView>
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            android:layout_gravity="center">
            <TextView
                android:id="@+id/textView1"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="姓名"
                android:textSize="30dp"></TextView>
            <TextView
                android:id="@+id/textView2"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="电话"
                android:textSize="20dp"></TextView>
        </LinearLayout>
    </LinearLayout>
</LinearLayout>

Java代码:
主类
因为用到图片,所以要在drawable文件夹里添加相应的图片资源

public class MainActivity extends AppCompatActivity {

    Context context = MainActivity.this;
    List list = new ArrayList();
    ListView listView;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        listView = findViewById(R.id.listView);
        list.add(new Data(R.drawable.tx,"小明","19656565656"));
        list.add(new Data(R.drawable.tx,"小红","18878787878"));
        baseAdapter adapter = new baseAdapter(context,list);
        listView.setAdapter(adapter);
    }
}

适配器

public class baseAdapter extends BaseAdapter {
    Context context;
    List <Data> list;

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

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

    @Override
    public Object getItem(int i) {
        return list.get(i);
    }

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

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        View v = LayoutInflater.from(context).inflate(R.layout.mydata, null);
        TextView textView1 = v.findViewById(R.id.textView1);
        TextView textView2 = v.findViewById(R.id.textView2);
        ImageView imageView = v.findViewById(R.id.imageView);
        imageView.setImageResource(list.get(i).tradmark);
        textView1.setText(list.get(i).name);
        textView2.setText(list.get(i).phone);
        return v;
    }
}

数据类

public class Data {
    int tradmark;
    String name;
    String phone;

    public Data(int trademark, String name,String phone){
        this.tradmark = trademark;
        this.name = name;
        this.phone = phone;
    }
}

效果图
Android - Adapter 适配器
Through love comes calm, and through calm comes thought.
爱使人平静,平静使人思考。


喜欢 (0)