How to number items in a list view?

别来无恙 提交于 2020-01-24 09:49:07

问题


I was wondering how to set a number starting from 1 to n for my listview items. The column is not included in my sqllite database.

I would like to use a custom simple cursor adapter that has a textview called labelNumber and set the number to 1 on the first call to newView and bindview and increment the value after each call.

Is this the right approach or is there a better way. Obviously this is to help with the navigation of the list view to know your current position?

Update- code is working check comments below

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

}

}


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

}
}



回答2:


Try to override 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;
}



回答3:


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

}

This is full simple cursor adapter..,.



来源:https://stackoverflow.com/questions/13179876/how-to-number-items-in-a-list-view

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!