Back to project page ProjectStudio.
The source code is released under:
Apache License
If you think the Android project ProjectStudio listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
package fragments; /*ww w. j a v a 2 s . com*/ 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.Loader; import android.support.v4.widget.SimpleCursorAdapter; import android.text.TextUtils; import android.util.Log; import android.view.Menu; import android.view.MenuInflater; import android.view.MenuItem; import android.view.View; import android.widget.ListView; import android.widget.SearchView; import com.example.uniutilproject.R; import java.sql.SQLException; import DB_Provider.CourseDataSource; import DB_Provider.DB_ABSTRACTS; /** * Created by desmond on 18/03/14. */ public class CursorLoaderListFragment extends ListFragment implements SearchView.OnQueryTextListener, LoaderManager.LoaderCallbacks<Cursor> { // This is the Adapter being used to display the list's data. SimpleCursorAdapter mAdapter; // If non-null, this is the current filter the user has provided. String mCurFilter; private CourseDataSource dataSource; private Cursor cursor; @Override public void onActivityCreated(Bundle savedInstanceState) { super.onActivityCreated(savedInstanceState); //OPEN DATABASE dataSource = new CourseDataSource(getActivity()); try { dataSource.open(); } catch (SQLException e) { e.printStackTrace(); } // Give some text to display if there is no data. In a real // application this would come from a resource. setEmptyText("No results found :("); // We have a menu item to show in action bar. setHasOptionsMenu(true); //Get the cursor cursor = dataSource.getAllCourses(); String[] from = {DB_ABSTRACTS.DBCourse.NAME_COLUMN}; int[] to = {android.R.id.text1}; //Create an empty adapter we will use to display the loaded data mAdapter = new SimpleCursorAdapter(getActivity(), android.R.layout.simple_list_item_2, null, from, to , 0); setListAdapter(mAdapter); //Start out with a progress indicator setListShown(false); //Prepare the loader. Either re-connect with an existing one, // or start a new one. getLoaderManager().initLoader(0, null, this); } @Override public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) { super.onCreateOptionsMenu(menu, inflater); //Place an actionBar item for searching MenuItem item = menu.add("Search"); item.setIcon(R.drawable.ic_search); item.setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM); SearchView sv = new SearchView(getActivity()); sv.setOnQueryTextListener(this); item.setActionView(sv); } @Override public Loader<Cursor> onCreateLoader(int id, Bundle args) { return null; } @Override public void onLoadFinished(Loader<Cursor> loader, Cursor data) { } @Override public void onLoaderReset(Loader<Cursor> loader) { } @Override public boolean onQueryTextSubmit(String s) { // Don't care about this. return true; } @Override public void onListItemClick(ListView l, View v, int position, long id) { // Insert desired behavior here. Log.i("FragmentComplexList", "Item clicked: " + id); } @Override public boolean onQueryTextChange(String newText) { // Called when the action bar search text has changed. Update // the search filter, and restart the loader to do a new query // with this filter. mCurFilter = !TextUtils.isEmpty(newText) ? newText : null; getLoaderManager().restartLoader(0, null, this); return true; } }