Repository navigation
Expand file tree
/
Copy pathBindDynamicDataLayout
More file actions
143 lines (110 loc) · 4.97 KB
/
Copy pathBindDynamicDataLayout
File metadata and controls
143 lines (110 loc) · 4.97 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
//very helpful src of how adapter is refreshed:
http://stackoverflow.com/questions/12229817/android-how-does-notifydatasetchanged-method-and-listviews-work
//src: http://developer.android.com/guide/topics/ui/declaring-layout.html#AdapterViews
Use if: content for layout is dynamic or not pre-determined.
Want to populate the layout with views at runtime.
Need a layout that subclasses the AdapterView class
>> Use an Adapter to bind data to its layout.
Layout examples:
ListView, GridView
To populate:
bind AdapterView instance to an Adapter (which retrieves ata from external source. creates a View for each data entry)
Popular Adapters:
ArrayAdapter (works for Strings only)
for array data sources.
view is created by callign toString() on each.
>> contents placed into a TextView
Override options:
[1] IF want object appearance to change, override MyObject.toString()
[2] To create a view other than a TextView (maybe want ImageView), extend ArrayDapter class and override getView() to retun the type of view you want for each item
SimpleCursorAdapter (works if data comes form a Cursor)
+ Must specify layout to use for each row in Cursor
+ Must specify which columns in Cursor can be inserted into which views of the layout
eg) create list of people's name + phone
String[] fromColumns = {ContactsContract.Data.DISPLAY_NAME,
ContactsContract.CommonDataKinds.Phone.NUMBER};
int[] toViews = {R.id.display_name, R.id.phone_number};
When instantiating SimpleCursorAdapter, choose what layout to use for each result, what Cursor to draw results from, and the from[] and to[] arrays
SimpleCursorAdapter adapter = new SimpleCursorAdapter(this,
R.layout.person_name_and_number, cursor, fromColumns, toViews, 0);
ListView listView = getListView();
listView.setAdapter(adapter);
eg) ListView for string array
/**
* @param context (this in this case)
* @param layout that contains a TextView for each MyObject in array
* @param MyObjectClassArray
*/
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, myStringArray);
ListView listView = (ListView) findViewById(R.id.listview);
listView.setAdapter(adapter);
If data pool changed, call notifyDataSetChanged()
>> notifies view that data changed
>> view should refresh itself
Handling Click Events for each item in AdapterView
+ Must implement AdapterView.OnItemClickListener interface
// Create a message handling object as an anonymous class.
private OnItemClickListener mMessageClickedHandler = new OnItemClickListener() {
public void onItemClick(AdapterView parent, View v, int position, long id) {
// Do something in response to the click
}
};
listView.setOnItemClickListener(mMessageClickedHandler);
eg) Big example involving ListFragment and its adapter
public class BDArrayAdapter extends ArrayAdapter<BluetoothDevice> {
private final Context context;
private List<BluetoothDevice> items;
public BDArrayAdapter(Context context, List<BluetoothDevice> objects){
super(context, R.layout.rows_peers, objects);
this.context = context;
this.items = objects;
}
public View getView(int position, View convertView, ViewGroup parent){
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View rowView = inflater.inflate(R.layout.rows_peers, parent, false);
//do stuff to rowView's views
TextView device_info = (TextView) rowView.findViewById(R.id.people_name_address);
BluetoothDevice device = items.get(position);
device_info.setText(device.getName() + "\n" + device.getAddress());
return rowView;
}
}
public class ListPeers extends ListFragment {
List<BluetoothDevice> pairedDevices = new ArrayList<BluetoothDevice>();
public ListPeers() {
// Required empty public constructor
}
@Override
public void onActivityCreated(Bundle savedInstanceState){
super.onActivityCreated(savedInstanceState);
this.setListAdapter(new BDArrayAdapter(getActivity(), pairedDevices));
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_list_peers, container, false);
}
}
public class BlueToothActivity extends Activity {
...
Set<BluetoothDevice> pairedDevices;
public void onDiscoverOtherDevices(){
if (mReceiver == null){
mReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
//If found device
if (BluetoothDevice.ACTION_FOUND.equals(action)){
//Add device info
BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
if (pairedDevices.add(device)){
};
}
}
};
}
registerReceiver(mReceiver, filter);
}
}