android getPosition和排序

时间:2012-12-25 01:01:56

标签: android sqlite

是否可以按一个字段排序并查看另一个字段的行位置。 例如,在第一个字段中使用名称,在第二个字段中使用age按名称排序,在第三个字段中查看从最小到最旧的位置。

Ann 25years 2 // 2nd position in the age.
Joe 30years 3 // 3rd position in the age.
Ron 20years 1 // 1st position

我想从“专栏时代”获得定位,是否可能?

我的代码

Show_Activity

///ON RESUME    
    @Override
    protected void onResume() {
        // TODO Auto-generated method stub
        super.onResume();

    datasource.open();
        Cursor cursor = datasource.Query(filter);

        String[] columns = new String[] { "swimm_pos", "swimm_date","swimm_lap", "swimm_stroke", "swimm_time", "swimm_media", "swimm_efficiency", "swimm_note" };
        int[] to = new int[] { R.id.swimm_pos, R.id.swimm_date, R.id.swimm_lap, R.id.swimm_stroke, R.id.swimm_time, R.id.swimm_medialap, R.id.swimm_efficiency, R.id.swimm_note};

        SimpleCursorAdapter adapter = new SimpleCursorAdapter(
            this, 
            R.layout.list_layout, 
            cursor, 
            columns, 
            to);

    adapter.setViewBinder(new SimpleCursorAdapter.ViewBinder() {
            public boolean setViewValue(View view, Cursor cursor, int columnIndex) {
              if (view.getId() == R.id.swimm_pos)
                { 
                  rowcounter = cursor.getPosition()+1;
                  String s = String.valueOf(rowcounter);
                  TextView tv = (TextView)view;
                  tv.setBackgroundColor(0xFF558866);
                  tv.setText(s);
                  return true;
            }
              return false;}
        });

    this.setListAdapter(adapter);
    datasource.close();
}

将对DBAdapter

public Cursor Query(String filter) {
            Cursor cursor = database.rawQuery
                    ("select _id, swimm_pos,swimm_date, swimm_lap,swimm_stroke,swimm_time,swimm_media,swimm_efficiency,swimm_note from swimm_table order by cast("+filter+" as integer) asc", null);
            return cursor;
        }
.........

我有按钮来改变排序

....
    switch (v.getId()) {
      case R.id.btn_sort_date:
            filter = "swimm_date";
            datasource.open();
                cursor = datasource.Query(filter);        
                adapter = new SimpleCursorAdapter(
                    this, 
                    R.layout.list_layout, 
                    cursor, 
                    columns, 
                    to); 

1 个答案:

答案 0 :(得分:0)

你可以用不同的方式做到这一点。您可以通过编写代码然后在表中插入来对年龄进行排序,也可以在不同的表中使用年龄,并使其成为ORDER BY(默认为升序)年龄。将年龄表标记为一列作为主键自动增量。然后使用包含人名的表进行内连接。这样你就得到了上面提到的表格。