android-如何在列表视图中编号项目?
作者:互联网
我想知道如何为我的listview项目设置一个从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);
}
}
}
解决方法:
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);
}
}
}
标签:sqlite,listview,simplecursoradapter,android 来源: https://codeday.me/bug/20191031/1977747.html