3

我想知道如何为我的列表视图项目设置一个从 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);
    }

}

}

4

3 回答 3

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

}
}
于 2012-11-01T16:17:17.347 回答
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;
}
于 2012-11-01T15:30:41.437 回答
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;
}

}

这是完全简单的光标适配器..,。

于 2012-11-01T16:01:16.047 回答