Note that there are some explanatory texts on larger screens.

plurals
  1. POOverriding SimpleCursorAdapter incorrect data and position returned
    primarykey
    data
    text
    <p>I'm currently attempting to override a simplecursor adapter to allow me to use a imagbutton for each listitem.</p> <p>Code so far: </p> <pre><code>public class MyCursorAdapter extends SimpleCursorAdapter { Cursor mCursor; final int tIndex; final class ViewHolder{ public TextView textView; public ImageButton imageButton; } public MyCursorAdapter(Context context, int layout, Cursor c, String[] from, int[] to) { super(context, layout, c, from, to); mCursor = c; tIndex = mCursor.getColumnIndex(DBAdapter.TASK); } @Override public View getView(int position, View convertView, ViewGroup parent) { if (mCursor.moveToPosition(position)){ ViewHolder viewHolder; final int i = position; if (convertView == null){ LayoutInflater inflater = MainActivity.this.getLayoutInflater(); viewHolder = new ViewHolder(); convertView = inflater.inflate(R.layout.listview,parent,false); viewHolder.textView = (TextView)convertView.findViewById(R.id.taskName); viewHolder.imageButton = (ImageButton)convertView.findViewById(R.id.edit_task); viewHolder.imageButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { Log.d("LOG_TAG", "It Works: " + i); } }); convertView.setTag(viewHolder); } else { viewHolder = (ViewHolder)convertView.getTag(); } String task = mCursor.getString(tIndex); viewHolder.textView.setText(task); } return convertView; } } </code></pre> <p>I use my parent activity to display the contents of the database, and use an alternate activity to add data to the database. I call finish() on the alternate activity to ensure the user is returned to the parent activity, in the parent activity in the onResume() method I use: </p> <pre><code>@Override protected void onResume() { super.onResume(); cursor.moveToFirst(); myCursorAdapter.swapCursor(cursor = db.getAllRows()); list.startLayoutAnimation(); amountTasks(); } </code></pre> <p>When the user is returned to the parent activity, the data is incorrectly showed, the text of the listview will be a duplicate of the last entry, and the position will be 0.</p> <p>I have found that if i change the return statement to:</p> <pre><code>return super.getView(position,convertView,parent) </code></pre> <p>The text is correctly shown, but the position still returns 0.</p> <p>Any idea why this is happening?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload