如何在列表视图中对项目进行编号?

时间:2012-11-01 15:04:46

标签: android sqlite listview simplecursoradapter

我想知道如何为我的listview项目设置从1到n的数字。该列不包含在我的sqllite数据库中。

我想使用一个自定义的简单游标适配器,它具有一个名为labelNumber的textview,并在第一次调用newView和bindview时将数字设置为1,并在每次调用后递增该值。

这是正确的方法还是有更好的方法。显然这是为了帮助导航列表视图以了解您当前的位置?

更新代码正在检查以下评论

public class ProgressAdapter extends SimpleCursorAdapter {
private Context context;
private int layout;
private ViewHolder viewHolder;
    // create an integer to use as a number holder
private int n;


static class ViewHolder {
    public TextView number;
    public TextView setsText;
    public TextView repsText;
    public TextView weightText;
    public TextView completeText;
    public ImageView mgImage;
}

public ProgressAdapter(Activity context, int layout, Cursor c,
        String[] from, int[] to) {
    super(context, layout, c, from, to);
    this.context = context;
    this.layout = layout;

}


@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
    Cursor c = getCursor();
    viewHolder = new ViewHolder();
    final LayoutInflater inflater = LayoutInflater.from(context);
    View v = inflater.inflate(layout, parent, false);
    // get the number using the cursor position
            // add 1 to the int to start at 1 instead of 0
    n = c.getPosition()+ 1;
            String lineNumber = Integer.toString(n);

    int setsColumn = c.getColumnIndex(ExerciseDbAdapter.KEY_SETS);
    String sets = c.getString(setsColumn);

    int repsColumn = c.getColumnIndex(ExerciseDbAdapter.KEY_REPS);
    String reps = c.getString(repsColumn);

    int weightColumn = c.getColumnIndex(ExerciseDbAdapter.KEY_WEIGHT);
    String weight = c.getString(weightColumn);


    viewHolder.number = (TextView) v.findViewById(R.id.labelNumber);
    viewHolder.setsText = (TextView) v.findViewById(R.id.labelSets);
    viewHolder.repsText = (TextView) v.findViewById(R.id.labelReps);
    viewHolder.weightText = (TextView) v.findViewById(R.id.labelWeight);

    if ( viewHolder.setsText != null && viewHolder.repsText != null
            && viewHolder.weightText != null
            ) {
        viewHolder.number.setText(lineNumber);
        viewHolder.setsText.setText(sets);
        viewHolder.repsText.setText(reps);
        viewHolder.weightText.setText(weight);


    }
    return v;
}

@Override
public void bindView(View v, Context context, Cursor c) {
    viewHolder = new ViewHolder();
            // use cursor to get the integer and add 1 to position
    c = getCursor();
    n = c.getPosition()+ 1;
            String lineNumber = Integer.toString(n);
    int setsColumn = c.getColumnIndex(ExerciseDbAdapter.KEY_SETS);
    String sets = c.getString(setsColumn);

    int repsColumn = c.getColumnIndex(ExerciseDbAdapter.KEY_REPS);
    String reps = c.getString(repsColumn);

    int weightColumn = c.getColumnIndex(ExerciseDbAdapter.KEY_WEIGHT);
    String weight = c.getString(weightColumn);

    // set the fields for the view
    viewHolder.number = (TextView) v.findViewById(R.id.labelNumber);
    viewHolder.setsText = (TextView) v.findViewById(R.id.labelSets);
    viewHolder.repsText = (TextView) v.findViewById(R.id.labelReps);
    viewHolder.weightText = (TextView) v.findViewById(R.id.labelWeight);


    if (viewHolder.setsText != null && viewHolder.repsText != null
            && viewHolder.weightText != null
            ) {
        viewHolder.number.setText(lineNumber);
        viewHolder.setsText.setText(sets);
        viewHolder.repsText.setText(reps);
        viewHolder.weightText.setText(weight);
    }

}

}

3 个答案:

答案 0 :(得分:2)

public class ProgressAdapter extends SimpleCursorAdapter {
private Context context;
private int layout;
private ViewHolder viewHolder;
// create an integer to use as a number holder
private int n;


static class ViewHolder {
public TextView number;
public TextView setsText;
public TextView repsText;
public TextView weightText;
public TextView completeText;
public ImageView mgImage;
}

public ProgressAdapter(Activity context, int layout, Cursor c,
    String[] from, int[] to) {
super(context, layout, c, from, to);
this.context = context;
this.layout = layout;

}


@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
Cursor c = getCursor();
viewHolder = new ViewHolder();
final LayoutInflater inflater = LayoutInflater.from(context);
View v = inflater.inflate(layout, parent, false);
// get the number using the cursor position
n = c.getPosition();
        // add 1 to the int to start at 1 instead of 0
n = n+1;
int setsColumn = c.getColumnIndex(ExerciseDbAdapter.KEY_SETS);
String sets = c.getString(setsColumn);

int repsColumn = c.getColumnIndex(ExerciseDbAdapter.KEY_REPS);
String reps = c.getString(repsColumn);

int weightColumn = c.getColumnIndex(ExerciseDbAdapter.KEY_WEIGHT);
String weight = c.getString(weightColumn);


viewHolder.number = (TextView) v.findViewById(R.id.labelNumber);
viewHolder.setsText = (TextView) v.findViewById(R.id.labelSets);
viewHolder.repsText = (TextView) v.findViewById(R.id.labelReps);
viewHolder.weightText = (TextView) v.findViewById(R.id.labelWeight);

if ( viewHolder.setsText != null && viewHolder.repsText != null
        && viewHolder.weightText != null
        ) {
    viewHolder.number.setText(n+"");
    viewHolder.setsText.setText(sets);
    viewHolder.repsText.setText(reps);
    viewHolder.weightText.setText(weight);


}
return v;
}

@Override
public void bindView(View v, Context context, Cursor c) {
viewHolder = new ViewHolder();
        // use cursor to get the integer
c = getCursor();
n = c.getPosition();
        // add one to the value to start at 1 instead of 0
n = n+1;
int setsColumn = c.getColumnIndex(ExerciseDbAdapter.KEY_SETS);
String sets = c.getString(setsColumn);

int repsColumn = c.getColumnIndex(ExerciseDbAdapter.KEY_REPS);
String reps = c.getString(repsColumn);

int weightColumn = c.getColumnIndex(ExerciseDbAdapter.KEY_WEIGHT);
String weight = c.getString(weightColumn);

// set the fields for the view
viewHolder.number = (TextView) v.findViewById(R.id.labelNumber);
viewHolder.setsText = (TextView) v.findViewById(R.id.labelSets);
viewHolder.repsText = (TextView) v.findViewById(R.id.labelReps);
viewHolder.weightText = (TextView) v.findViewById(R.id.labelWeight);


if (viewHolder.setsText != null && viewHolder.repsText != null
        && viewHolder.weightText != null
        ) {
    viewHolder.number.setText(n+"");
    viewHolder.setsText.setText(sets);
    viewHolder.repsText.setText(reps);
    viewHolder.weightText.setText(weight);
}

}
}

答案 1 :(得分:1)

尝试覆盖getview .. ,.

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    LayoutInflater inflater=(LayoutInflater)appContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    convertView=inflater.inflate(layout, null, true);

    EditText txt=(EditText)convertView.findViewById(R.id.labelNumber);

    txt.settext(position + 1);   //position starts from 0 thats why add 1

    return convertView;
}

答案 2 :(得分:1)

public class myAdapter extends SimpleCursorAdapter {

//    globle variable

public myAdapter() {
    //constructor
}

@Override
public int getCount() {
    return 0;
}

@Override
public Object getItem(int arg0) {
    return null;
}
@Override
public long getItemId(int arg0) {
    return 0;
}

    @Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater=(LayoutInflater)appContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView=inflater.inflate(layout, null, true);

EditText txt=(EditText)convertView.findViewById(R.id.labelNumber);

txt.settext(position + 1);   //position starts from 0 thats why add 1

return convertView;
}

}

这是完整的简单游标适配器..,。