From: Marius Gavrilescu Date: Wed, 14 Aug 2013 06:26:37 +0000 (+0300) Subject: Clean up EventListActivity X-Git-Url: http://git.ieval.ro/?a=commitdiff_plain;h=6f8cabdd198b7e9c9f65d5ec83fc5fd55e774c0d;p=unical.git Clean up EventListActivity --- diff --git a/src/ro/ieval/unical/EventAdapter.java b/src/ro/ieval/unical/EventAdapter.java deleted file mode 100644 index a9e0181..0000000 --- a/src/ro/ieval/unical/EventAdapter.java +++ /dev/null @@ -1,38 +0,0 @@ -package ro.ieval.unical; -import java.util.Date; - -import android.content.Context; -import android.view.LayoutInflater; -import android.view.View; -import android.view.ViewGroup; -import android.widget.ArrayAdapter; -import android.widget.TextView; - - -public class EventAdapter extends ArrayAdapter { - private final Event[] values; - private final Context context; - - public EventAdapter(final Context context, final Event[] values) {//NOPMD - super(context,R.layout.event_row_layout,values); - this.context=context; - this.values=values; - } - - @Override - public View getView(final int position, final View convertView, final ViewGroup parent) { - final LayoutInflater inflater = (LayoutInflater) context - .getSystemService(Context.LAYOUT_INFLATER_SERVICE); - final View rowView = inflater.inflate(R.layout.event_row_layout, parent, false); - final TextView title = (TextView) rowView.findViewById(R.id.smalleventtitle); - final TextView dstart = (TextView) rowView.findViewById(R.id.smalldatestart); - final TextView dend = (TextView) rowView.findViewById(R.id.smalldateend); - final View header = rowView.findViewById(R.id.rowHeader); - header.setBackgroundColor(values[position].eventColour); - title.setText(values[position].title); - dstart.setText(new Date(values[position].dtstart).toString()); - dend.setText(new Date(values[position].dtend).toString()); - - return rowView; - } -} diff --git a/src/ro/ieval/unical/EventListActivity.java b/src/ro/ieval/unical/EventListActivity.java index 27eaef5..642b77f 100644 --- a/src/ro/ieval/unical/EventListActivity.java +++ b/src/ro/ieval/unical/EventListActivity.java @@ -1,49 +1,25 @@ package ro.ieval.unical; -import java.util.Arrays; -import java.util.Comparator; -import java.util.Date; -import android.app.Activity; +import android.app.ListActivity; +import android.content.Context; import android.content.Intent; import android.os.Bundle; +import android.view.LayoutInflater; import android.view.Menu; import android.view.MenuInflater; import android.view.MenuItem; import android.view.View; -import android.widget.AdapterView; +import android.view.ViewGroup; import android.widget.AdapterView.OnItemClickListener; +import android.widget.AdapterView; +import android.widget.ArrayAdapter; import android.widget.ListView; +import android.widget.TextView; +import java.util.Arrays; +import java.util.Comparator; +import java.util.Date; - -public class EventListActivity extends Activity { - - //Used to refresh events if the user wants to display only certain events - //Sry for bad english - //Muie la comisie - private void displayEvents() { - final Event events[]=Event.getAllEvents(getBaseContext()); - final ListView lv = (ListView) findViewById(R.id.eventlist); - - Arrays.sort(events,new EventComparator()); - int idx=-1;//index of the first event to be displayed - for(int i=0; inew Date().getTime()) idx=i; - if(idx==-1) idx=0; - - final EventAdapter adapter = new EventAdapter(this, events); - lv.setAdapter(adapter); - lv.setSelectionFromTop(idx, 0); - lv.setOnItemClickListener(new OnItemClickListener() { - - @Override - public void onItemClick(final AdapterView arg0, final View arg1, final int arg2, - final long arg3) { - DisplayEventActivity.displayEvent(EventListActivity.this,events[arg2]); - } - - }); - } - +public final class EventListActivity extends ListActivity { private static class EventComparator implements Comparator { @Override public int compare(final Event lhs, final Event rhs) { @@ -56,11 +32,51 @@ public class EventListActivity extends Activity { } + private class EventAdapter extends ArrayAdapter { + public EventAdapter() {//NOPMD + super(EventListActivity.this, R.layout.event_row_layout); + } + + @Override + public View getView(final int position, final View convertView, final ViewGroup parent) { + final LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE); + final View rowView = inflater.inflate(R.layout.event_row_layout, parent, false); + final TextView title = (TextView) rowView.findViewById(R.id.smalleventtitle); + final TextView dstart = (TextView) rowView.findViewById(R.id.smalldatestart); + final TextView dend = (TextView) rowView.findViewById(R.id.smalldateend); + final View header = rowView.findViewById(R.id.rowHeader); + header.setBackgroundColor(events[position].eventColour); + title.setText(events[position].title); + dstart.setText(new Date(events[position].dtstart).toString()); + dend.setText(new Date(events[position].dtend).toString()); + + return rowView; + } + } + + private Event[] events; + + @Override + public void onListItemClick(final ListView view, final View v, final int pos, final long id) { + DisplayEventActivity.displayEvent(this, events[pos]); + } + + private void displayEvents() { + events = Event.getAllEvents(this); + + Arrays.sort(events, new EventComparator()); + int idx=-1;//index of the first event to be displayed + for(int i=0; inew Date().getTime()) idx=i; + if(idx==-1) idx=0; + + setListAdapter(new EventAdapter()); + getListView().setSelectionFromTop(idx, 0); + } + @Override protected void onCreate(final Bundle savedInstanceState) { super.onCreate(savedInstanceState); - setContentView(R.layout.event_list); - displayEvents(); } @@ -75,8 +91,7 @@ public class EventListActivity extends Activity { public boolean onOptionsItemSelected(final MenuItem item) { switch (item.getItemId()) { case R.id.add_event: - final Intent i=new Intent(this,AddEventActivity.class); - startActivity(i); + startActivity(new Intent(this,AddEventActivity.class)); return true; case R.id.refresh: displayEvents();