Lec 124. 亳仄亠
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.listview);
SimpleAdapter adapter = new SimpleAdapter(this,
createSensorsList(), android.R.layout.simple_list_item_2,
new String[] {"title", "vendor"},
new int[] {android.R.id.text1, android.R.id.text2});
ListView lv = (ListView)findViewById(R.id.list);
lv.setAdapter(adapter);
}
5. 亳仄亠
private List<Map<String, ?>> createSensorsList() {
SensorManager sensorManager =
(SensorManager)getSystemService(Context.SENSOR_SERVICE);
List<Sensor> sensors = sensorManager.getSensorList(Sensor.TYPE_ALL);
List<Map<String, ?>> items = new ArrayList<Map<String, ?>>();
for (Sensor s : sensors) {
Map<String, Object> map = new HashMap<String, Object>();
map.put("title", s.getName());
map.put("vendor", s.getVendor());
items.add(map);
}
return items;
}
6. 亳仄亠(于仂 舒亰仄亠从舒)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout>
<TwoLineListItem xmlns:android="http://schemas.android.com/apk/res/android"
android:mode="twoLine">
<TextView android:id="@id/title" />
<TextView android:id="@id/content" />
</TwoLineListItem>
<SeekBar android:id="@id/range"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:max="100" />
</LinearLayout>
7. 亳仄亠(仂亰亟舒仆亳亠 舒亟舒仗亠舒)
SimpleAdapter adapter = new SimpleAdapter(
this,
createSensorsList(),
R.layout.sensor_layout,
new String[] {"title", "vendor", "power"},
new int[] {R.id.title, R.id.content, R.id.range});
8. 弌仗亳仂从 亠仆仂仂于
private List<Map<String, ?>> createSensorsList() {
SensorManager sensorManager =
(SensorManager)getSystemService(Context.SENSOR_SERVICE);
List<Sensor> sensors = sensorManager.getSensorList(Sensor.TYPE_ALL);
List<Map<String, ?>> items = new ArrayList<Map<String, ?>>();
for (Sensor s : sensors) {
Map<String, Object> map = new HashMap<String, Object>();
map.put("title", s.getName());
map.put("vendor", s.getVendor());
map.put("power", Math.round(s.getPower() * 10));
items.add(map);
}
return items;
}
9. ViewBinder
private static class SeekBarBinder implements SimpleAdapter.ViewBinder {
public boolean setViewValue(View view, Object data, String
textRepresentation) {
if (view.getId() == R.id.range) {
((SeekBar)view).setProgress((Integer)data);
return true;
}
return false;
}
} 仂仄亠 仂亞仂, 仗仂仍亠 亳仆亳亳舒仍亳亰舒亳亳 舒亟舒仗亠舒 仆亢仆仂
弍亟亠 仆舒仗亳舒:
adapter.setViewBinder(new SeekBarBinder());
10. ArrayAdapter<T>
ArrayAdapter(Context context, int resource, int textViewResourceId)
ArrayAdapter(Context context, int resource, int textViewResourceId,
T[] objects)
ArrayAdapter(Context context, int resource, int textViewResourceId,
List<T> objects)
ArrayAdapter(Context context, int resource)
ArrayAdapter(Context context, int resource, T[] objects)
ArrayAdapter(Context context, int resource, List<T> objects)
11. ArrayAdapter
context // 亠从亳亶 从仂仆亠从.
resource // 亳亟亠仆亳亳从舒仂 舒亰仄亠从亳, 从仂仂舒 弍亟亠 仗亳仄亠仆 从
仍亠仄亠仆舒仄 仗亳从舒. 亅舒 舒亰仄亠从舒 亟仂仍亢仆舒 仂亟亠亢舒 仂 弍 仂亟亳仆 TextView.
弍磶舒亠仍仆亶 仗舒舒仄亠.
textViewResourceId // 亳亟亠仆亳亳从舒仂 TextView, 从 从仂仂仂仄 弍亟 弍亳仆亟亳
仍亠仄亠仆 仗亳从舒. 仍亳 于 舒亰仄亠从亠 于亠亞仂 仂亟亳仆 TextView, 仂 仗舒舒仄亠
仄仂亢仆仂 仆亠 从舒亰于舒. 仍亳 亢亠 亳 仆亠从仂仍从仂, 仂 从舒亰于舒 仆亢仆仂
仂弍磶舒亠仍仆仂, 亳仆舒亠 于仍亠亰亠 仂亳弍从舒.
objects // 仗亳仂从 (亳仍亳 仄舒亳于) 仂仂弍舒亢舒亠仄 仂弍亠从仂于.
12. 亳仄亠
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.listview);
String[] strings = new String[] { "One", "Two", "Three" };
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, android.R.id.text1, strings);
ListView list = (ListView)this.findViewById(R.id.list);
list.setAdapter(adapter);
}
13. CursorAdapter
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.cursor_adapter);
String[] projection = new String[] { ContactsContract.Contacts.DISPLAY_NAME };
Cursor cursor = managedQuery(ContactsContract.Contacts.CONTENT_URI,
null, null, null, null);
SimpleCursorAdapter adapter = new SimpleCursorAdapter(this,
android.R.layout.simple_list_item_1,
cursor,
new String[] { ContactsContract.Contacts.DISPLAY_NAME },
new int[] { android.R.id.text1 });
AdapterView view = (AdapterView)findViewById(R.id.contacts_list);
view.setAdapter(adapter);
}
14. ExpandableList
getGroupCount() // 仗仂仍亳 从仂仍亳亠于仂 亞仗仗.
getGroup(int groupPosition) // 仗仂仍亳 仂弍亠从,
仂亟亠亢舒亳亶 于 亞仗仗亠 亰舒亟舒仆仆仄 仆仂仄亠仂仄.
getChildrenCount(int groupPosition) // 仗仂仍亳 从仂仍亳亠于仂
仍亠仄亠仆仂于 于 亞仗仗亠 亰舒亟舒仆仆仄 仆仂仄亠仂仄.
getChild(int groupPosition, int childPosition) // 仗仂仍亳
仂弍亠从, 仂亟亠亢舒亳亶 仆舒 亰舒亟舒仆仆仂仄 仄亠亠 于 仗亳从亠
亞仗仗 亰舒亟舒仆仆仄 仆仂仄亠仂仄
15. ExpandableList(从仂仆从仂)
SimpleExpandableListAdapter(Context context,
List<? extends Map<String, ?>> groupData,
int groupLayout,
String[] groupFrom,
int[] groupTo,
List<? extends List<? extends Map<String, ?>>> childData,
int childLayout,
String[] childFrom,
int[] childTo)
16. 亳仄亠
public class SimpleExpandableListAdapterActivity extends ExpandableListActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
SimpleExpandableListAdapter adapter = new SimpleExpandableListAdapter(
this,
createGroups(),
R.layout.display_header,
new String[] { "name" },
new int[] { R.id.header1 },
createChildren(),
R.layout.display_item,
new String[] { "name" },
new int[] { R.id.text1 });
setListAdapter(adapter);
}
17. 亳仄亠
public List<Map<String, ?>> createGroups() {
List<Map<String, ?>> list = new ArrayList<Map<String, ?>>();
for (int i = 1; i <= 3; ++i) {
Map<String, Object> item = new HashMap<String,
Object>();
item.put("name", String.format("%d-th", i * 10));
list.add(item);
}
return list;
}
18. 亳仄亠
public List<List<Map<String, ?>>> createChildren() {
List<List<Map<String, ?>>> list = new ArrayList<List<Map<String,?>>>();
for (int i = 1; i <= 3; ++i) {
List<Map<String, ?>> itemList = new ArrayList<Map<String, ?>>();
for (int j = 0; j < 5; ++j) {
Map<String, Object> item = new HashMap<String, Object>();
item.put("name", String.format("%d", i * 10 + j));
itemList.add(item);
}
list.add(itemList);
}
return list;
}
19. 仂仄舒仆亠亠 亰舒亟舒仆亳亠
1. 亠舒仍亳亰仂于舒 舒亟舒仗亠 于 从仂仂仂仄 弍亟
亳仗仂仍亰仂于舒 从仍舒 Pair 亳 Money
(亟仂仍亢仆仂 于于仂亟亳 1 . 20 从仂仗.).
2. 亠舒仍亳亰仂于舒 舒亟舒仗亠, 从仂仂亶 弍亟亠
仗亳仄亠仆 于 于舒亠仄 仗仂亠从亠.