0

I'm very, VERY new to android and I'm making a scheduling app for teachers, students, and parents. I've been watching tutorials on how to make a ListView and I have that all configured blah blah blah. I just needed some help with arranging assignments dynamically.

Here's the screen where you input information of the assignment:

Here's the list:

As you can see, Assignment 2 was due June 1st, while Assignment 1 was due June 7th. However, Assignment 2 is listed after Assignment 1. Is there a way I can arrange it in order? I know you can simply multiply the months digit, days digit, and year digit together and the lesser value would go first, however how do I organize it using Android's API?

This is TeachersActivity.java

List<Assignment> a = new ArrayList<Assignment>();
ListView aListView;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_teachers);

    TextView teachers = (TextView) findViewById(R.id.teachers);
    TextView create = (TextView) findViewById(R.id.create);
    TextView assignments = (TextView) findViewById(R.id.assignments);
    final EditText assignmentname = (EditText) findViewById(R.id.assignmentname);
    final DatePicker due = (DatePicker) findViewById(R.id.due);
    final EditText time = (EditText) findViewById(R.id.time);
    TabHost tabHost = (TabHost) findViewById(R.id.tabs);
    final Button createassignment = (Button) findViewById(R.id.createassignment);
    aListView = (ListView) findViewById(R.id.listView);

    tabHost.setup();
    TabHost.TabSpec tabSpec = tabHost.newTabSpec("add");
    tabSpec.setContent(R.id.tabAdder);
    tabSpec.setIndicator("Add");
    tabHost.addTab(tabSpec);

    tabSpec = tabHost.newTabSpec("assignments");
    tabSpec.setContent(R.id.tabAssignments);
    tabSpec.setIndicator("Assignments");
    tabHost.addTab(tabSpec);

    Typeface arvo = Typeface.createFromAsset(getAssets(),
            "fonts/arvo.otf");
    Typeface bebas = Typeface.createFromAsset(getAssets(),
            "fonts/bebas.otf");

    teachers.setTypeface(arvo);
    create.setTypeface(bebas);
    assignmentname.setTypeface(bebas);
    time.setTypeface(bebas);
    createassignment.setTypeface(bebas);
    assignments.setTypeface(bebas);

    createassignment.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            GregorianCalendar g = new GregorianCalendar( due.getYear(), due.getMonth(), due.getDayOfMonth() );
            int t = Integer.parseInt(time.getText().toString());

            addAssignments(assignmentname.getText().toString(), g, t);
            populateList();
            Toast.makeText(getApplicationContext(), "Assignment created!", Toast.LENGTH_SHORT).show();
        }
    });

    assignmentname.addTextChangedListener(new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {

        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            createassignment.setEnabled(!assignmentname.getText().toString().trim().isEmpty());
        }

        @Override
        public void afterTextChanged(Editable s) {

        }
    });

    teachers.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent i = new Intent(TeachersActivity.this, MainActivity.class);  // http://stackoverflow.com/a/13194141/2869358
            startActivity(i);  // http://stackoverflow.com/a/13194141/2869358
        }
    });
}

private void populateList()
{
    ArrayAdapter<Assignment> adapter = new AssignmentListAdapter();
    aListView.setAdapter(adapter);
}

private void addAssignments( String name, GregorianCalendar dateDue, int time )
{
    a.add( new Assignment( name, dateDue, time ));
}

private class AssignmentListAdapter extends ArrayAdapter<Assignment>
{

    Typeface arvo = Typeface.createFromAsset(getAssets(),
            "fonts/arvo.otf");
    Typeface bebas = Typeface.createFromAsset(getAssets(),
            "fonts/bebas.otf");

    public AssignmentListAdapter()
    {
        super ( TeachersActivity.this, R.layout.listview_assignments, a );
    }

    @Override
    public View getView( int position, View view, ViewGroup parent )
    {
        if ( view == null )
        {
            view = getLayoutInflater().inflate( R.layout.listview_assignments, parent, false );
        }

        Assignment currentAssignment = a.get( position );

        TextView name = (TextView) view.findViewById(R.id.name);
        TextView due = (TextView) view.findViewById(R.id.due);
        TextView time = (TextView) view.findViewById(R.id.time);
        TextView assigned = (TextView) view.findViewById(R.id.assigned);
        TextView completed = (TextView) view.findViewById(R.id.completed);

        name.setTypeface(bebas);
        due.setTypeface(bebas);
        time.setTypeface(bebas);
        assigned.setTypeface(bebas);
        completed.setTypeface(bebas);

        name.setText(currentAssignment.getName());
        due.setText("Due: " + currentAssignment.getDue());
        time.setText("Estimated Time: " + currentAssignment.getTimeString());
        assigned.setText("Assigned: " + currentAssignment.getAssigned());
        completed.setText("Completed: " + currentAssignment.getCompletedString());

        return view;
    }
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.teachers, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();
    if (id == R.id.action_settings) {
        return true;
    }
    return super.onOptionsItemSelected(item);
}

Here is my Assignments class

String name;
int time;
public Calendar assigned;
public Calendar due;
SimpleDateFormat dateFormat = new SimpleDateFormat("MM.dd.yyyy");

public Assignment( String name, GregorianCalendar dateDue, int time )
{
    this.name = name;
    assigned = Calendar.getInstance();
    due = dateDue;
    this.time = time;
}

public String getName()
{
    return name;
}

public String getTimeString()
{
    int h = time / 60;
    int m = time % 60;
    return h + " hour(s), " + m + " minute(s)";
}

public int getTime()
{
    return time;
}

public String getAssigned()
{
    return dateFormat.format(assigned.getTime());
}

public String getDue()
{
    return dateFormat.format(due.getTime());
}

@Override
public int compareTo(Assignment other)
{
    return due.compareTo(other.due);
}

Thanks!

Anshul Desai
  • 57
  • 1
  • 1
  • 4

2 Answers2

1

What I would do its to implement Comparable to your Assignment object.

And them before you set the ArrayAdapter to your list you sort it based on your comparassion.

here is a good tutorial on how to implement Comparable to a custom object:

http://www.mkyong.com/java/java-object-sorting-example-comparable-and-comparator/

Marcus Gabilheri
  • 1,259
  • 1
  • 14
  • 26
  • Okay, so I've made my compareTo method: `@Override public int compareTo(Assignment other) { return due.compareTo(other.due); }` However what do I do now? How do I have my listview use this method to change the order dynamically? – Anshul Desai Jun 07 '14 at 20:34
  • What you have to do is use `Collections.sort` in your `ArrayList` before you set it to the adapter of the `ListView` – Marcus Gabilheri Jun 08 '14 at 16:19
0

I agree with using the comparable object to organize the list view items in your array adpater , you could refresh the list using the following:

public void onRestoreInstanceState(Bundle savedInstanceState) 
{
    super.onRestoreInstanceState(savedInstanceState);

    //Preferably, insert re -organize list items code here      
}
Rahil Wazir
  • 10,007
  • 11
  • 42
  • 64