Search code examples
androidlistviewcustom-adapter

Position and ID are always returning same value in ListView


I have a little problem with my ListView, which does not give me neither the correct position nor id, when being in OnItemLongClickListener.

The ListView displays all entries properly, but on long item click it returns me the sum of entries as the position (doesn't matter on what item I click) and the highest id of all entries. As this is the matter, I cannot get the correct id of the entry (which I do have in my custom adapter). What am I doing wrong?

mListView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
        @Override
        public boolean onItemLongClick(AdapterView<?> adapterView, View view, int position, long id) {
            //Here I want to delete the selected entry..
            //both position and id are returning the same value:
            // when there are three items in the list, the position would be three for all entries, 
            // while the id would be the value of the latest entry.
            showDeleteSingleEntryDialog(id);
            return true;
        }
    });

I populate the list view with using a AsyncTask as follows (by calling the AsyncTask in my OnCreateView inside the fragment)

private class DisplayEntriesAsyncTask extends AsyncTask<Void,Void,Void>{

    Cursor data;

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
    }

    @Override
    protected Void doInBackground(Void... voids) {
        data = mDatabaseHelper.getDiaryEntriesCurrentUser(userID);
        return null;
    }

    @Override
    protected void onProgressUpdate(Void... values) {
        super.onProgressUpdate(values);

    }

    @Override
    protected void onPostExecute(Void aVoid) {
        super.onPostExecute(aVoid);

        listData = new ArrayList<>();
        if (data == null || data.getCount() < 1) {
            mTextView.setText("Keine Einträge vorhanden!");
        } else {
            try {
                while (data.moveToNext()){
                    listData.add(data.getString(1));
                }
            } catch (CursorIndexOutOfBoundsException e){
                //...
            }
        }
        adapter = new DiaryCursorAdapter(getContext(), data);
        mListView.setAdapter(adapter);
    }
}

Finally, here's my custom adapter

public class DiaryCursorAdapter extends CursorAdapter {

Context context;
private long ident;

public DiaryCursorAdapter(Context context, Cursor c) {
    super(context, c, 0);
    this.context = context;
}

@Override
public void bindView(View view, Context context, Cursor cursor) {
    ident = cursor.getLong(0);
    TextView title = (TextView) view.findViewById(R.id.listitem_title);
    title.setText(cursor.getString(1));
    TextView location = (TextView) view.findViewById(R.id.listitem_location);
    location.setText(cursor.getString(3));
    TextView date = (TextView) view.findViewById(R.id.listitem_date);
    date.setText(cursor.getString(4));
    TextView content = (TextView) view.findViewById(R.id.listitem_content);
    content.setText(cursor.getString(2));
}
@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
    LayoutInflater layoutInflater = LayoutInflater.from(context);
    return layoutInflater.inflate(R.layout.listview_diary_entries, parent, false);
    //return view;
}

@Override
public long getItemId(int position) {
    return ident;
}}

I tried to populate the list view without the AsyncTask. By the way, the ListView's parent in the corresponding layout file is a LinearLayout (not a scrollview, as I discovered was a possible issue).


Solution

  • To make @pskink and @karora comments an answer:

    I've falsely overwritten getItemID, which caused the behaviour I described above.

    So, removing of the override method did fix my problem indeed.