Object reference changes value completely at random


  1. jonhoo

    jonhoo New Member

    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:
    lv.setOnItemClickListener(new OnItemClickListener(){
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            Tuples.this.activeElement = (LinearLayout) view;
            // 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:
    .setPositiveButton(R.string.save, new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int whichButton) {
            // Save!
            AlertDialog ad = (AlertDialog) dialog;
            EditText tk = (EditText) ad.findViewById(R.id.tuple_key);
            EditText tv = (EditText) ad.findViewById(R.id.tuple_value);
            
            // Delete old one
            Tuples.this.deleteElement ( Tuples.this.activeElement );
            
            // Add new one
            Tuples.this.add ( tk.getText().toString(), tv.getText().toString() );
        }
    })
    
    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 =)
    Jon

    Advertisement
  2. jonhoo

    jonhoo New Member

    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 =)

    Jon

Share This Page