繁体   English   中英

notifyDataSetChanged不会更新我的列表适配器

[英]notifyDataSetChanged does not update my List adapter

我正在尝试使用警报对话框将项目动态添加到列表适配器。 该项目将添加到用于显示的列表中,但是添加新项目时,列表适配器不会更改。 这是我的一些代码:

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;
    }
}

}

任何帮助,将不胜感激谢谢。

首先在创建时间设置

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

之后,您没有将此联系人分配给contactNumbersList

此contactNumbersList已设置到您的适配器中,因此只要将其更新(例如添加/删除),您都可以收到通知

您需要创建此contactNumbersList声明,使其像接触对象一样位于onCreate方法之外,以便您在联系时可以将接触值重新分配给该对象,之后再添加

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM