Created
August 28, 2015 01:35
-
-
Save AdamMc331/bf59cc99eb7296e917e6 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.adam.cashcaretaker.fragments; | |
import android.database.Cursor; | |
import android.os.Bundle; | |
import android.support.annotation.Nullable; | |
import android.support.v4.app.DialogFragment; | |
import android.support.v4.app.LoaderManager; | |
import android.support.v4.content.CursorLoader; | |
import android.support.v4.content.Loader; | |
import android.view.LayoutInflater; | |
import android.view.View; | |
import android.view.ViewGroup; | |
import android.view.Window; | |
import android.widget.AdapterView; | |
import android.widget.ListView; | |
/** | |
* Created by adam on 6/6/15. | |
*/ | |
public class CategoryDialog extends DialogFragment implements LoaderManager.LoaderCallbacks<Cursor>{ | |
private CategoryAdapter mAdapter; | |
private static final int CATEGORY_LOADER = 0; | |
@Nullable | |
@Override | |
public View onCreateView(LayoutInflater inflater, ViewGroup container, | |
Bundle savedInstanceState) { | |
View view = inflater.inflate(R.layout.dialog_category, container, false); | |
// Setup listview and adapter | |
ListView categoryListView = (ListView) view.findViewById(R.id.category_list_view); | |
mAdapter = new CategoryAdapter(getActivity()); | |
categoryListView.setAdapter(mAdapter); | |
// Set click listener | |
categoryListView.setOnItemClickListener(new AdapterView.OnItemClickListener() { | |
@Override | |
public void onItemClick(AdapterView<?> parent, View view, int position, long id) { | |
Category c = new Category((Cursor)mAdapter.getItem(position)); | |
((OnCategorySelectedListener)getActivity()).onCategorySelected(c); | |
dismiss(); | |
} | |
}); | |
// No need to show title for this dialog. | |
getDialog().getWindow().requestFeature(Window.FEATURE_NO_TITLE); | |
return view; | |
} | |
@Override | |
public void onActivityCreated(Bundle savedInstanceState) { | |
super.onActivityCreated(savedInstanceState); | |
getLoaderManager().initLoader(CATEGORY_LOADER, null, this); | |
} | |
@Override | |
public Loader<Cursor> onCreateLoader(int id, Bundle args) { | |
switch(id){ | |
case CATEGORY_LOADER: | |
return new CursorLoader( | |
getActivity(), | |
CCContract.CategoryEntry.CONTENT_URI, | |
null, | |
null, | |
null, | |
CCContract.CategoryEntry.COLUMN_NAME | |
); | |
default: | |
throw new UnsupportedOperationException("Unknown loader id: " + id); | |
} | |
} | |
@Override | |
public void onLoadFinished(Loader<Cursor> loader, Cursor data) { | |
switch(loader.getId()){ | |
case CATEGORY_LOADER: | |
mAdapter.swapCursor(data); | |
break; | |
default: | |
throw new UnsupportedOperationException("Unknown loader id: " + loader.getId()); | |
} | |
} | |
@Override | |
public void onLoaderReset(Loader<Cursor> loader) { | |
switch(loader.getId()){ | |
case CATEGORY_LOADER: | |
mAdapter.swapCursor(null); | |
break; | |
default: | |
throw new UnsupportedOperationException("Unknown loader id: " + loader.getId()); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="utf-8"?> | |
<ListView | |
xmlns:android="http://schemas.android.com/apk/res/android" | |
android:id="@+id/category_list_view" | |
android:layout_width="match_parent" | |
android:layout_height="match_parent" | |
android:divider="@android:color/black" | |
android:dividerHeight="1px"/> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// This is your activity that opens the dialog | |
public class Activity extends AppCompatActivity implements OnCategorySelectedListener{ | |
/** | |
* Shows the DialogFragment allowing the user to select a Category for the account. | |
*/ | |
private void showCategoryDialog(){ | |
DialogFragment dialogFragment = new CategoryDialog(); | |
dialogFragment.show(getSupportFragmentManager(), "category"); | |
} | |
@Override | |
public void onCategorySelected(Category category) { | |
// Handle what to do with the category that was selected. | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment