Java tutorial
/* * 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 android.support.test.testapp; import com.google.common.annotations.VisibleForTesting; import android.database.MatrixCursor; import android.os.Bundle; import android.support.v4.app.ListFragment; import android.support.v4.widget.CursorAdapter; import android.support.v4.widget.SimpleCursorAdapter; import android.view.View; import android.widget.ListAdapter; import android.widget.ListView; import android.widget.TextView; /** * A {@link ListFragment} which uses a {@link CursorAdapter} as a data source. */ 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(); final ListAdapter cursorAdapter = new SimpleCursorAdapter(getActivity(), android.R.layout.simple_list_item_2, listItemCursor, 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)); } } }