listview在从数据库中删除行后再次加载值

时间:2013-09-20 07:45:33

标签: android listview

我有一个自定义适配器,用于填充我的联系人列表视图。我正在使用db.delete从该视图中删除特定联系人。删除联系人后,视图不会更新。说我有A B C D联系人。当我删除A时,当我返回活动时,我的列表视图会被B C D B C D填充。我试过了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;

    }

}

1 个答案:

答案 0 :(得分:0)

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

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

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

adaptor.updateResults(newArrayListHere);