diff --git a/res/layout/choose_category_dialog.xml b/res/layout/choose_category_dialog.xml index 23be899..5d43843 100644 --- a/res/layout/choose_category_dialog.xml +++ b/res/layout/choose_category_dialog.xml @@ -1,10 +1,11 @@ - - + android:layout_height="wrap_content"> + - \ No newline at end of file + \ No newline at end of file diff --git a/res/layout/choose_category_list_row.xml b/res/layout/choose_category_list_row.xml new file mode 100644 index 0000000..3be511a --- /dev/null +++ b/res/layout/choose_category_list_row.xml @@ -0,0 +1,12 @@ + + + + + diff --git a/src/com/studentersamfundet/app/ui/ChooseCategoryDialog.java b/src/com/studentersamfundet/app/ui/ChooseCategoryDialog.java new file mode 100644 index 0000000..2a22a0e --- /dev/null +++ b/src/com/studentersamfundet/app/ui/ChooseCategoryDialog.java @@ -0,0 +1,61 @@ +package com.studentersamfundet.app.ui; + +import java.security.InvalidParameterException; + +import com.studentersamfundet.app.Event; +import com.studentersamfundet.app.R; + +import android.app.Dialog; +import android.content.Context; +import android.content.DialogInterface; +import android.view.View; +import android.widget.AdapterView; +import android.widget.ArrayAdapter; +import android.widget.ListView; +import android.widget.AdapterView.OnItemClickListener; + +public class ChooseCategoryDialog extends Dialog { + private final Callback callback; + private String chosenCategory = Event.ALL; + + public interface Callback { + public void run(String ... message); + } + + private class OnDismiss implements OnDismissListener { + public void onDismiss(DialogInterface dialog) { + callback.run(chosenCategory); + } + } + + public ChooseCategoryDialog(Context context, Callback callback) { + super(context); + + if (callback == null) + throw new InvalidParameterException("callback is not supposed to be null!"); + + this.callback = callback; + + setContentView(R.layout.choose_category_dialog); + setTitle("Choose category"); + setOnDismissListener(new OnDismiss()); + populateList(context); + } + + private void populateList(Context context) { + final String[] categories = new String[] { + Event.ALL, + "film" + }; + + ListView list = (ListView)findViewById(R.id.choose_category_list); + list.setAdapter(new ArrayAdapter(context, R.layout.choose_category_list_row, R.id.choose_category_list_row_text, categories)); + list.setOnItemClickListener(new OnItemClickListener() { + public void onItemClick(AdapterView parent, View view, int position, + long id) { + ChooseCategoryDialog.this.chosenCategory = categories[position]; + ChooseCategoryDialog.this.dismiss(); + } + }); + } +} diff --git a/src/com/studentersamfundet/app/ui/ProgramListActivity.java b/src/com/studentersamfundet/app/ui/ProgramListActivity.java index cb1d6df..fd58cdc 100644 --- a/src/com/studentersamfundet/app/ui/ProgramListActivity.java +++ b/src/com/studentersamfundet/app/ui/ProgramListActivity.java @@ -26,6 +26,7 @@ import com.studentersamfundet.app.Event; import com.studentersamfundet.app.FeedFetcher; import com.studentersamfundet.app.R; import com.studentersamfundet.app.XmlParser; +import com.studentersamfundet.app.ui.ChooseCategoryDialog.Callback; public class ProgramListActivity extends BaseDnsActivity { public static final String feedURL = "http://studentersamfundet.no/rss/lars_program_feed.php"; @@ -61,8 +62,7 @@ public class ProgramListActivity extends BaseDnsActivity { return true; case R.id.menu_categories: - String category = chooseCategory(); - this.createList(category, false); + createCategoryDialog(); return true; default: @@ -142,12 +142,16 @@ public class ProgramListActivity extends BaseDnsActivity { } - protected String chooseCategory() { - Dialog dialog = new Dialog(this); - dialog.setContentView(R.layout.choose_category_dialog); + protected void createCategoryDialog() { + Dialog dialog = new ChooseCategoryDialog(this, new Callback() { + public void run(String... message) { + if (message.length < 1) + message = new String[] { Event.ALL }; + + ProgramListActivity.this.createList(message[0], false); + } + }); - dialog.setTitle("Choose category"); dialog.show(); - return Event.ALL; } }