Android手机操作系统是一款开源的操作系统。它的作用非常强大,可以根据用户不同的需求进行编写,灵活的满足用户的各种需求。在这里我们先来了解一下Android列表框的一些应用技巧,以加深对这方面的认知程度。#t#
● 设置Adapter,调用setAdapter()方法。
● 追加Item被点击时候的处理,调用setOnItemClickListener()方法。
● 追加Item被选择时候的处理,调用setOnItemSelectedListener()方法。
Android列表框例程源码(Java)
- ArrayAdapter< String> adapter = new ArrayAdapter< String>(
- this,
- android.R.layout.simple_list_item_1);
- adapter.add("red");
- adapter.add("green");
- adapter.add("blue");
- ListView listView = (ListView) findViewById(id.listView);
- listView.setAdapter(adapter);
- listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
- @Override
- public void onItemClick(
- AdapterView< ?> parent,
- View view,
- int position,
- long id) {
- ListView listView = (ListView) parent;
- Log.v("Test", "id = " + id + "("
- + listView.getItemAtPosition(position).toString() + ")");
- }
- });
- listView.setOnItemSelectedListener(new OnItemSelectedListener() {
- @Override
- public void onItemSelected(
- AdapterView< ?> parent,
- View view,
- int position,
- long id) {
- ListView listView = (ListView) parent;
- Log.v("Test", "id = " + id + "("
- + listView.getSelectedItem().toString() + ")");
- }
- @Override
- public void onNothingSelected(AdapterView< ?> parent) {
- }
- });
Android列表框例程源码(Resource)
- < ListView android:id="@+id/listView"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent" />