目录
- 数组适配器ArrayAdapter
- 简单适配器SimpleAdapter
- 自定义适配器BaseAdapter
数组适配器ArrayAdapter
简介:
ArrayAdapter是Android的列表适配器,用于ListView这类型的控件与数据之间适配用的。默认的ArrayAdapter期望接受的样式文件里只含有一个textview 使用方法:实现如下效果: XML布局:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <ListView android:id="@+id/li" android:layout_width="match_parent" android:layout_height="wrap_content"></ListView> </LinearLayout>
Java代码:
package com.contentprovide.liuliu.demo_01; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.AdapterView; import android.widget.ArrayAdapter; import android.widget.ListView; import android.widget.TextView; import android.widget.Toast; public class MainActivity extends AppCompatActivity { ArrayAdapter<String> arrayAdapter; ListView li; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); li = (ListView) findViewById(R.id.li); arrayAdapter = new ArrayAdapter(MainActivity.this,R.layout.support_simple_spinner_dropdown_item); // 给数组适配器添加内容 arrayAdapter.add("1"); arrayAdapter.add("2"); arrayAdapter.add("3"); arrayAdapter.add("4"); // 把数组适配器添加到Listview的对象中 li.setAdapter(arrayAdapter); //给ListView的对象添加点击事件:点击每个TextView选项,弹出该文本框上对应的内容 li.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) { TextView t = (TextView) view; String s = t.getText().toString(); Toast.makeText(getApplicationContext(),s,Toast.LENGTH_SHORT).show(); } }); } }
简单适配器SimpleAdapter
自定义适配器BaseAdapter
原文地址:https://www.cnblogs.com/lyd447113735/p/8251461.html
时间: 2024-10-28 16:02:05