0

我有一个自定义适配器填充我的联系人列表视图。我正在使用db.delete从该视图中删除特定联系人。一旦我的联系人被删除,视图就不会更新。假设我有 ABCD 联系人。当我删除 A 时,当我返回活动时,我的列表视图会填充 BCDBCD。我试过了adaptor.clear。帮助 !

            This is my AddContacts.java

                adaptor = new CustomContactAdapter(this);  // Adaptor which populates th listview                
                final ListView favContacts = (ListView) findViewById(R.id.list);
                favContacts.setAdapter(adaptor);    
                queryForContacts();



                    favContacts.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() 

                    {

                                @Override
                                public boolean onItemLongClick(AdapterView<?> parent, View view,int      position, long id) {
                                    // TODO Auto-generated method stub

                                    Log.d(LOG_TAG,"List View Clicked : id ="+id);
                                    ContactDetail myObject = CustomContactAdapter.contacts.get((int) id);
                                    nickname = myObject.nickname;
                                    Log.d(LOG_TAG,"Extracting the nickname ="+nickname);
                                    showDialog(DELETE_CONTACT);
                                    contact_position=position;
                                    Log.d(LOG_TAG, "this is the contact position "+contact_position);
                                    adaptor.clear();
                                    return false;
                                }
                            }); 
                }
        ----------------------------------------------

        /**
             * Ths method is called to fill UI with contacts fetched from database
             * @param mCursor
             */
            public void fillChat(Cursor mCursor) {
                Log.v(LOG_TAG, "fillChat : " + mCursor.getCount());
                boolean hasUpdated = false;
                int count = 0;
                if (mCursor != null && mCursor.getCount() != 0) {
                    adaptor.clear();
                    mCursor.moveToFirst();
                    while (!mCursor.isAfterLast()) {
                        Log.d(LOG_TAG, "mCursor " + count++);
                        if(!mCursor.getString(mCursor
                                        .getColumnIndex(ContactTable.AUTH_NAME)).equals(Config.getAuthUserName()))
                        {
                            ContactDetail detail = new ContactDetail(
                                    mCursor.getString(mCursor
                                            .getColumnIndex(ContactTable.AUTH_NAME)),
                                    mCursor.getString(mCursor
                                            .getColumnIndex(ContactTable.NICK_NAME)),
                                            mCursor.getInt(mCursor
                                                    .getColumnIndex(ContactTable.CONTACT_STATUS))       ,
                                    mCursor.getString(mCursor
                                            .getColumnIndex(ContactTable.PHOTO_PATH)));
                            adaptor.add(detail);

                        }

                        mCursor.moveToNext();
                    }
                    adaptor.notifyDataSetChanged();
                    mCursor.close();

                }
            }

    ----------
    //Dialog for 'delete contact'

        protected Dialog onCreateDialog(int id) {
                switch (id) {
        case DELETE_CONTACT: {
                    AlertDialog.Builder builder = new AlertDialog.Builder(this);
                    builder.setItems(R.array.delete_contact,
                            new DialogInterface.OnClickListener() {
                                public void onClick(DialogInterface dialog, int which) {
                                DatabaseUtils.deleteContactFromDB(nickname, mContext);
                                Log.d(LOG_TAG,"Nickname sent :" +nickname);
                                CustomContactAdapter.contacts.remove(contact_position);
                                adaptor.notifyDataSetChanged();
                                return;
                                }
                            });
                    return builder.create();


                }
                default:
                    return null;
                }
            }

--------------------------
DatabaseUtils.java
public static void deleteContactFromDB(String name, Context context) {
        MyDatabaseHelper dbHelper = new MyDatabaseHelper(context);
        SQLiteDatabase db = dbHelper.getWritableDatabase();
        db.setLocale(Locale.getDefault());
        db.setLockingEnabled(true);
        //String[] args = {name};
        String where = ContactTable.NICK_NAME + "='" + name + "'";
        Log.d(LOG_TAG,"printing where :"+where);
        int rowsAffected = db.delete(ContactTable.TABLE_NAME,where, null);
        Log.d(LOG_TAG, "deleteContact rows affected in contactdb logcat  table ="
                + rowsAffected);


        db.setLockingEnabled(false);

        if (db != null) {
            db.close();
        }
    }

此类是在添加联系人屏幕中填充联系人列表视图的适配器类:CustomContactAdaptor.java

public class CustomContactAdapter extends ArrayAdapter<ContactDetail> {

    private LayoutInflater inflater;
    private static final String LOG_TAG = "CustomContactAdapter";
    private Context mContext;
    static ArrayList<ContactDetail> contacts = new ArrayList<ContactDetail>();
//  private CreateBitmap createBitmap;

    /**
     * Adds the specified object at the end of the array.
     */
    @Override
    public void add(ContactDetail object) {
        super.add(object);
        contacts.add(object);
    }

    /**
     * Update photo in list view if changed by user
     * @param name
     * @param nickname
     * @param photoLoc
     */
    public void updatePhoto(String name,String nickname,String photoLoc){
        Iterator<ContactDetail> itr = contacts.iterator();
        while(itr.hasNext()){
            ContactDetail contact = itr.next();
            if(name.equals(contact.name)){
                if(photoLoc!=null){

                    contact.photo = photoLoc;
                }
                if(nickname!=null){
                    contact.nickname = nickname;
                }
            }
        }
    }
    /**
     * Constructor
     * @param context
     */
    public CustomContactAdapter(Context context) {
        super(context, R.layout.add_contact_list_item);
        // Cache the LayoutInflate to avoid asking for a new one each time.
        ArrayList<ContactDetail> contacts = new ArrayList<ContactDetail>();
        inflater = LayoutInflater.from(context);
        mContext = context;

    }

    /**
     * returns number of contacts in list view
     */
    public int getCount() {
        return this.contacts.size();
    }

    /**
     * returns list item at particular index
     */
    public ContactDetail getItem(int index) {
        return contacts.get(index);
    }



    /**
     * returns view for each list view element
     */
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        TextView name;
        ImageView photo;
        TextView nickname;

        ContactDetail contact = (ContactDetail) getItem(position);
        Log.d(LOG_TAG,"getView name "+contact.name + " photo "+contact.photo);
//      if (convertView == null) {
            convertView = inflater.inflate(
                    R.layout.add_contact_list_item, null);
            // Find the child views.rss
            name = (TextView) convertView.findViewById(R.id.name);
            nickname = (TextView) convertView.findViewById(R.id.nickname);

            photo = (ImageView) convertView.findViewById(R.id.photo);

            // Optimization: Tag the row with it's child views, so we don't have
            // to
            // call findViewById() later when we reuse the row.
            convertView.setTag(new CustomContactViewHolder(name, nickname,photo));

        name.setText(contact.name);
        nickname.setText(contact.nickname);

        if (contact.photo != null && photo != null&& new File(contact.photo).exists()) {

//          createBitmap.createRoundedImage(contact.photo,photo,64,63,4, CreateBitmap.ROUNDIMAGE);


            Bitmap roundedImage = ImageUtils.getRoundedImage(contact.photo,64,63,4);
            photo.setImageBitmap(roundedImage);
//          System.gc();
        }

        return convertView;

    }

}
4

1 回答 1

0

在您的CustomContactAdapter中,创建一个执行此操作的方法:

public void updateResults(ArrayList< ContactDetail > results) {
    contacts = results;
    //Triggers the list update
    notifyDataSetChanged();
}

然后您可以创建一个 CustomContactAdapter 的实例并调用该函数,例如在您的 AddContacts.java

adaptor.updateResults(newArrayListHere);

于 2013-09-20T07:48:56.813 回答