-
Notifications
You must be signed in to change notification settings - Fork 310
/
CursorAdapterListFragment.java
86 lines (75 loc) �� 2.93 KB
/
CursorAdapterListFragment.java
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
/*
* Copyright (C) 2014 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package androidx.test.ui.app;
import android.database.MatrixCursor;
import android.database.MergeCursor;
import android.os.Bundle;
import android.view.View;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.TextView;
import androidx.cursoradapter.widget.CursorAdapter;
import androidx.cursoradapter.widget.SimpleCursorAdapter;
import androidx.fragment.app.ListFragment;
import com.google.common.annotations.VisibleForTesting;
/** A {@link ListFragment} which uses a {@link CursorAdapter} as a data source. */
@SuppressWarnings("SetTextI18n")
public class CursorAdapterListFragment extends ListFragment {
private static final String COLUMN_ID = "_id";
@VisibleForTesting
public static final String COLUMN_STR = "column_str";
@VisibleForTesting
public static final String COLUMN_LEN = "column_length";
private static final String[] COLUMN_NAMES = new String[] {
COLUMN_ID, COLUMN_STR, COLUMN_LEN
};
private MatrixCursor listItemCursor;
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
listItemCursor = new MatrixCursor(COLUMN_NAMES);
populateData();
// Throw in a row with different column names... for the sake of science (testing).
MatrixCursor surprise =
new MatrixCursor(new String[] {"surprise!", "columns", "are different"});
surprise.addRow(new Object[] {1, 2, 3});
MergeCursor mergeCursor = new MergeCursor(new MatrixCursor[] {listItemCursor, surprise});
final ListAdapter cursorAdapter = new SimpleCursorAdapter(getActivity(),
android.R.layout.simple_list_item_2, mergeCursor,
new String[] {
COLUMN_STR, COLUMN_LEN
},
new int[] {
android.R.id.text1, android.R.id.text2
}, 0);
setListAdapter(cursorAdapter);
}
@Override
public void onListItemClick(ListView listView, View view, int position, long id) {
TextView selectedItemValue = (TextView) getActivity().findViewById(R.id.selected_item_value);
selectedItemValue.setText("item: " + position);
}
private static Object[] makeItem(int forRow) {
return new Object[] {
forRow, "item: " + forRow, ("item: " + forRow).length()
};
}
private void populateData() {
for (int i = 0; i < 20; i++) {
listItemCursor.addRow(makeItem(i));
}
}
}