本文主要是介绍ArrayAdapter和SimpleAdapter适配器,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
适配器及适配器控件
1、ArrayAdapter
实现代码
public class Main2Activity extends AppCompatActivity {private ListView listAdapter;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main2);initView();}private void initView(){listAdapter = findViewById(R.id.list_adapter);ArrayAdapter<String> adapter = new ArrayAdapter<>(getApplicationContext(),R.layout.support_simple_spinner_dropdown_item,new String[]{"音乐","电影","足球"});listAdapter.setAdapter(adapter);}
}
其中support_simple_spinner_dropdown_item为系统自带的行布局文件,也可以自己编译一个行布局文件
2、SimpleAdapter
private ListView listView ;private String [] keys = new String []{"name","role","team"};private int [] IDs = new int[]{R.id.name,R.id.role,R.id.team};private List<HashMap<String, String>> list;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main2);initView();}private void initView(){listView = (ListView)findViewById(R.id.list_adapter);list = new ArrayList<HashMap<String,String>>();HashMap<String, String> map = new HashMap<String, String>();map.put("name", "小罗");map.put("role", "足球");map.put("team", "巴萨罗那");list.add(map);HashMap<String, String> map2 = new HashMap<String, String>();map2.put("name", "乔丹");map2.put("role", "篮球");map2.put("team", "公牛");list.add(map2);SimpleAdapter adapter = new SimpleAdapter(getApplicationContext(), list, R.layout.item, keys, IDs);listView.setAdapter(adapter);}
item为自己编写的行布局:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent"android:orientation="horizontal" ><TextView
android:id="@+id/name"android:layout_width="130dp"android:layout_height="wrap_content" /><TextView
android:id="@+id/role"android:layout_width="130dp"android:layout_height="wrap_content"/><TextView
android:id="@+id/team"android:layout_width="130dp"android:layout_height="wrap_content"/>
</LinearLayout>
这篇关于ArrayAdapter和SimpleAdapter适配器的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!