如何自动安排Android上的ListView?

时间:2014-06-07 19:55:46

标签: android listview

我非常非常非常熟悉Android,我正在为教师,学生和家长制作一个安排应用程序。我一直在观看有关如何制作ListView的教程,我已经完成了所有配置等等等等等等。我只是需要一些帮助来动态安排作业。

此处是您输入作业信息的屏幕:

这是列表:

如您所见,作业2 将于6月1日到期,而作业1 将于6月7日到期。但是,作业1 列在作业1 之后。有没有办法按顺序排列?我知道您可以简单地将月份数字,天数字和年份数字相乘,而较小的值将首先增加,但是如何使用Android的API组织它?

这是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);
}

这是我的作业类

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);
}

谢谢!

2 个答案:

答案 0 :(得分:1)

我会做什么来实现Compaterable对你的Assignment对象。

在将ArrayAdapter设置到列表之前,根据您的比较对它进行排序。

这是一个很好的教程,介绍如何将Comparable实现为自定义对象:

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

答案 1 :(得分:0)

我同意使用可比对象来组织数组adpater中的列表视图项,您可以使用以下内容刷新列表:

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

    //Preferably, insert re -organize list items code here      
}