我想知道如何为我的列表视图项目设置一个从 1 到 n 的数字。该列不包含在我的 sqllite 数据库中。
我想使用一个自定义的简单光标适配器,它有一个名为 labelNumber 的文本视图,并在第一次调用 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);
}
}
}