Object reference changes value completely at random

Discussion in 'Application Development' started by jonhoo, Jul 14, 2010.

  1. jonhoo

    jonhoo New Member

    Jul 14, 2010
    Hey hey!

    I've just started developing my second Android application. It is pretty straightforward, but useful nonetheless.
    All it does is allow the user to record key-value pairs for later reference (for things you can't be bothered to remember such as bank account numbers, PGP fingerprints, etc..)

    While doing this, however, I have been completely stumped by a bug..:
    I'll try to explain as best I can.
    My main, and only, activity extends ListActivity, and each element in the list is a LinearLayout containing two TextViews, one with the key and the other with the value.
    The main class (Tuples) holds a pointer called activeElement, which points to the latest element to have been either clicked or long clicked.
    Code (Text):
    2. lv.setOnItemClickListener(new OnItemClickListener(){
    3.     public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
    4.         Tuples.this.activeElement = (LinearLayout) view;
    5.         // More code...
    Furthermore, I have three dialogs, one which shows details for a list item when clicked, one which opens a simple select list of "Edit" or "Delete" on long click, and finally, an edit dialog (which also serves as a new element dialog).
    At this point, the logic for editing things is very simple: When an element is edited, its old row is deleted, and a new one is created with the new values.

    The code reads as follows (this is on the AlertDialog.Builder I use to build the dialog in onCreateDialog):
    Code (Text):
    2. .setPositiveButton(R.string.save, new DialogInterface.OnClickListener() {
    3.     public void onClick(DialogInterface dialog, int whichButton) {
    4.         // Save!
    5.         AlertDialog ad = (AlertDialog) dialog;
    6.         EditText tk = (EditText) ad.findViewById(R.id.tuple_key);
    7.         EditText tv = (EditText) ad.findViewById(R.id.tuple_value);
    9.         // Delete old one
    10.         Tuples.this.deleteElement ( Tuples.this.activeElement );
    12.         // Add new one
    13.         Tuples.this.add ( tk.getText().toString(), tv.getText().toString() );
    14.     }
    15. })
    Here comes the really weird part though - when I run the program, everything works fine, except that on edit, it is not the element I long click on that is deleted, but the last element in list?
    I have put debugging statements all over the place, and it seems as though it changes value just before the onClick on the dialog OK button is clicked.
    The variable is not changed anywhere in my code (I have put debugging statements on all activeElement = statements), and at the end of onPrepareDialog, activeElement points to the correct element.
    Can anyone think of what might be causing this? I'll provide more code if needed.

    Hope for a helpful answer =)

  2. jonhoo

    jonhoo New Member

    Jul 14, 2010
    Turns out, Android reuses list items in ListView, so storing a pointer to a given ListView element is not at all guaranteed to always refer to the same element later on.
    Rewrote the code to use the position and ID parameters passed into onItemClick instead, and now everything works =)


Share This Page