Hugs
Hugs

Reputation: 935

notifyDataSetChanged does not update my List adapter

I am trying to dynamically add items to a list adapter using an alert dialog. The item is getting added to the list being used to display, but the list adapter does not change when the new item is added. Here is some of my code:

public class ContactEdit extends Activity implements View.OnClickListener, OnItemSelectedListener

{

private Button btnAdd = null;

/**
 * Used as a flag to indicate if the Activity is being used to create a new
 * contact (0) or update an old contact (1).
 */
public static boolean isEdit = false;

/**
 * The adapter that holds the contacts numbers
 */
public NumberListAdapter numberListAdapter;

/**
 * A Contact containing all the details of the contact to be edited by this
 * Activity
 */
private Contact contact = null;

/**
 * This method creates the NewContact activity.
 * 
 * @param i
 *            This Method gets passed the bundle to be created.
 * @see android.app.Activity#onCreate(android.os.Bundle)
 */
@Override
public final void onCreate(final Bundle i)
{
    // Used to temporarily store the nameID passed to this Activity when it
    // is called to
    // edit a contact.

    isEdit = false;

    try
    {
        super.onCreate(i);

        this.setContentView(R.layout.contactedit);

        // Add number button
        this.btnAdd = (Button) findViewById(R.id.form_button);
        this.btnAdd.setOnClickListener(ContactEdit.this);

        final Bundle extras = this.getIntent().getExtras();

        if (extras != null)
        {
            // sets value
            contact = extras.getParcelable(BlackbookUtils.CONTACT);

            final List<Number> contactNumbersList = contact.getNumbers();
            ListView numberListView = (ListView) this.findViewById(R.id.numberListView);
            numberListAdapter = new NumberListAdapter(this, R.layout.row, contactNumbersList);
            numberListView.setAdapter(numberListAdapter);

            isEdit = true;

        }

        Log.d(TAG, "new contact oncreate successful");
    }
    catch (Exception e)
    {
        Log.d(TAG, "newcontact oncreate failed");
        e.printStackTrace();
    }
}

/**
 * This method sets the program to watch for any button or textbox to be
 * pressed.
 * 
 * @param view
 *            This is the current focused item, in this case a textbox.
 * @see android.view.View.OnClickListener#onClick(android.view.View)
 */
public final void onClick(final View view) // change this to use new delim
// methods
{
    try
    {
        if (view == btnAdd)
        {
            //TODO: Guard to check if it is a new contact
            //Add number from the edit text to the list

            if (!isEdit)
            {
                contact = new Contact();
                numberListAdapter = new NumberListAdapter(this, R.layout.row, contact.getNumbers());
            }

            AlertDialog.Builder alert = new AlertDialog.Builder(this);

            alert.setTitle("Contact edit");
            alert.setMessage("Add a new number");

            // Set an EditText view to get user input 
            final EditText input = new EditText(this);
            alert.setView(input);

            alert.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int whichButton) {
                    Editable value = input.getText();
                    //TODO: This will notify there is a change in the list and reload it

                    Number tempNumber = new Number();
                    tempNumber.setphoneNumber(value.toString());
                    contact.getNumbers().add(tempNumber);

                    numberListAdapter.notifyDataSetChanged();
                }
            });

            alert.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int whichButton) {
                    // Canceled.
                }
            });

            alert.show();
        }


            }
        }
    }
    catch (Exception e)
    {
        Log.d(TAG, "Names, Numbers and Emails failed to set ");
        e.printStackTrace();
    }
}



private static class NumberListAdapter extends ArrayAdapter<Number>
{

    /**
     * Used to create the initial views that populate the list.
     */
    private LayoutInflater mInflater = null;

    /**
     * List of stations after the filter has been applied.
     */
    public List<Number> filteredItems;

    /**
     * List of all stations.
     */
    public ArrayList<Number> originalItems = new ArrayList<Number>();

    /**
     * Used to search the stations and update the list appropriately.
     */
    private Filter filter;

    public NumberListAdapter(Context context, int textViewResourceId,
            List<Number> contactNumbersList) {
        super(context, textViewResourceId, contactNumbersList);

        // Cache the LayoutInflate to avoid asking for a new one each time.
        mInflater = LayoutInflater.from(context);
        // keep track of the list of provided Station objects and a copy for
        // applying filters
        this.filteredItems = contactNumbersList;
        cloneItems(contactNumbersList);

    }

    protected void cloneItems(List<Number> contactNumbersList) {
        for (Iterator<Number> iterator = contactNumbersList.iterator(); iterator
        .hasNext();) {
            Number s = (Number) iterator.next();
            originalItems.add(s);
        }
    }

    static class ViewHolder
    {
        TextView number;
        //TODO: Will need to possibly assign function of this
        Button deleteBTN;
    }

    /**
     * Populates each row of the custom list with data
     */
    /*
     * (non-Javadoc)
     * @see android.widget.ArrayAdapter#getView(int, android.view.View,
     * android.view.ViewGroup)
     */
    @Override
    public View getView(int position, View convertView, ViewGroup parent)
    {
        // this will hold onto the widgets associated with a particular list
        // entry
        ViewHolder holder;

        if (convertView == null)
        {
            // if there's no views to recycle, inflate a new one
            convertView = mInflater.inflate(R.layout.row, null);

            // store the widgets in the holder
            holder = new ViewHolder();
            //                holder.number = (TextView) convertView.findViewById(R.id.rowTextView);
            //                holder. = (Button) convertView.findViewById(R.id.rowButton);
            holder.number = (TextView) convertView.findViewById(R.id.rowTextView);
            holder.deleteBTN = (Button) convertView.findViewById(R.id.rowButton);
            // tag the holder to the view so that they can be retrieved next
            // time
            convertView.setTag(holder);
        }
        else
        {
            // retrieved the tagged ViewHolder
            holder = (ViewHolder) convertView.getTag();
        }

        // retrieve the current Station object
        Number temp = filteredItems.get(position);

        if (temp != null)
        {
            try
            {   
                holder.number.setText(temp.getnumber());

            }
            catch (Exception e)
            {
                Log.d(TAG, "getView - failed");
                e.printStackTrace();
            }
        }
        return convertView;
    }
}

}

Any help would be appreciated thanks.

Upvotes: 0

Views: 2161

Answers (1)

Pratik
Pratik

Reputation: 30855

first onCreate time you set the

final List<Number> contactNumbersList = contact.getNumbers();

and after that you didn't assign this contact to the contactNumbersList

this contactNumbersList was set into your adapter so whenever this will be updated like add/remove the you can get notified

you need to create this contactNumbersList declare as outside of the onCreate method like contact object so you can re-assign the contact value to this object when you contact add this like after that

contactNumbersList = contact.getNumbers();
numberListView.setAdapter(numberListAdapter);

Upvotes: 1

Related Questions