Java tutorial
/* * Copyright (C) 2011 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 com.schedule; import android.content.ContentValues; import android.database.Cursor; import android.os.Bundle; import android.support.v4.app.ListFragment; import android.support.v4.app.LoaderManager; import android.support.v4.content.CursorLoader; import android.support.v4.content.Loader; import android.view.View; import android.view.animation.AnimationUtils; import android.widget.ListView; import android.widget.SimpleCursorAdapter; import android.widget.Toast; import com.schedule.db.DataProviderContract.MessageEntry; import com.schedule.db.DataProviderContract.TargetEntry; /** * Fragment that displays the news headlines for a particular news category. * * This Fragment displays a list with the news headlines for a particular news * category. When an item is selected, it notifies the configured listener that * a headlines was selected. */ public class TargetsFragment extends ListFragment implements LoaderManager.LoaderCallbacks<Cursor> { // The list adapter for the list we are displaying SimpleCursorAdapter mListAdapter; // Identifies a particular Loader being used in this component private static final int TARGET_LOADER = 0; @Override public Loader<Cursor> onCreateLoader(int arg0, Bundle arg1) { // TODO Auto-generated method stub switch (arg0) { case TARGET_LOADER: // Returns a new CursorLoader return new CursorLoader(getActivity(), TargetEntry.TARGET_TABLE_CONTENTURI, new String[] { TargetEntry._ID, TargetEntry.COLUMN_NAME }, null, null, null); default: // An invalid id was passed in return null; } } @Override public void onLoadFinished(Loader<Cursor> arg0, Cursor arg1) { mListAdapter.changeCursor(arg1); } @Override public void onLoaderReset(Loader<Cursor> arg0) { mListAdapter.changeCursor(null); } // The listener we are to notify when a headline is selected OnHeadlineSelectedListener mHeadlineSelectedListener = null; /** * Represents a listener that will be notified of headline selections. */ public interface OnHeadlineSelectedListener { /** * Called when a given headline is selected. * * @param index * the index of the selected headline. */ public void onHeadlineSelected(long index); } /** * Default constructor required by framework. */ public TargetsFragment() { super(); } @Override public void onStart() { super.onStart(); setListAdapter(mListAdapter); } void initData() { ContentValues values[] = new ContentValues[3]; String names[] = new String[] { "Simth", "Bob", "Jack" }; for (int i = 0; i < 3; i++) { values[i] = new ContentValues(); values[i].put(TargetEntry.COLUMN_NAME, names[i]); values[i].put(TargetEntry.COLUMN_AGE, 18); values[i].put(TargetEntry.COLUMN_PHONE, 110); getActivity().getContentResolver().insert(TargetEntry.TARGET_TABLE_CONTENTURI, values[i]); ContentValues value = new ContentValues(); value.put(MessageEntry.COLUMN_TARGET_ID, i + 1); value.put(MessageEntry.COLUMN_CONTENT, "test content test content test content test content" + i); getActivity().getContentResolver().insert(MessageEntry.MESSAGE_TABLE_CONTENTURI, value); } } @Override public void onViewCreated(View view, Bundle savedInstanceState) { // TODO Auto-generated method stub super.onViewCreated(view, savedInstanceState); //initData(); String[] from = { TargetEntry.COLUMN_NAME }; int[] to = { android.R.id.text1 }; mListAdapter = new SimpleCursorAdapter(getActivity(), R.layout.headline_item, null, from, to); getLoaderManager().initLoader(TARGET_LOADER, null, this); getListView().setLayoutAnimation( AnimationUtils.loadLayoutAnimation(getActivity(), R.anim.layout_bottom_to_top_slide)); // // String[]mStrings=new String[]{"a","b","c","d"}; // // getListView().setAdapter(new ArrayAdapter<String>(this.getActivity(), // android.R.layout.simple_list_item_activated_1, mStrings)); // getListView().setTextFilterEnabled(true); // Tell the list view to show one checked/activated item at a time. // getListView().setChoiceMode(ListView.CHOICE_MODE_SINGLE); // Start with first item activated. // Make the newly clicked item the currently selected one. getListView().setItemChecked(0, true); } @Override public void onListItemClick(ListView l, View v, int position, long id) { // TODO Auto-generated method stub // super.onListItemClick(l, v, position, id); if (null != mHeadlineSelectedListener) { mHeadlineSelectedListener.onHeadlineSelected(id); // Toast.makeText(getActivity(), "id:"+id, 1).show(); } Toast.makeText(getActivity(), "onlistitemclick", 1).show(); getListView().setItemChecked(position, true); } /** * Sets the listener that should be notified of headline selection events. * * @param listener * the listener to notify. */ public void setOnHeadlineSelectedListener(OnHeadlineSelectedListener listener) { mHeadlineSelectedListener = listener; } /** * Handles a click on a headline. * * This causes the configured listener to be notified that a headline was * selected. */ // @Override // public void onItemClick(AdapterView<?> parent, View view, int position, long id) { // if (null != mHeadlineSelectedListener) { // mHeadlineSelectedListener.onHeadlineSelected(id); // // // Toast.makeText(getActivity(), "id:"+id, 1).show(); // } // } /** * Sets choice mode for the list * * @param selectable * whether list is to be selectable. */ public void setSelectable(boolean selectable) { if (selectable) { getListView().setChoiceMode(ListView.CHOICE_MODE_SINGLE); Toast.makeText(getActivity(), "CHOICE_MODE_SINGLE", 1).show(); } else { getListView().setChoiceMode(ListView.CHOICE_MODE_NONE); } } }