在Adapter中删除textview后删除listview中的项目

时间:2014-12-20 20:33:03

标签: android listview android-arrayadapter

我对android很新。对于我的生活,我无法弄清楚如何从我的适配器中删除ListView中的项目。列表视图中的每一行都有一个编辑和删除选项。我填写一个对话框,询问用户确认删除,确认后,我希望ListView删除该项目。任何帮助将不胜感激。

Contacts.java

public class Contacts extends ActionBarActivity {

    private Button mButton;
    private ContactsAdapter mAdapter;
    private ListView mListView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.emergency_contacts);

        mAdapter = new ContactsAdapter(Contacts.this,new ArrayList<SingleContactInfo>());
        mListView = (ListView) findViewById(R.id.listView);
        mListView.setAdapter(mAdapter);
        mButton = (Button) findViewById(R.id.addContactButton);


        updateData();

        mButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent goToAddContacts = new Intent(getApplicationContext(), AddContactInfo.class);
                startActivity(goToAddContacts);
            }
        });

    }

    public void updateData(){
        ParseQuery<SingleContactInfo> query = ParseQuery.getQuery(SingleContactInfo.class);
        query.whereEqualTo("user", ParseUser.getCurrentUser());
        query.setCachePolicy(ParseQuery.CachePolicy.CACHE_THEN_NETWORK);
        query.findInBackground(new FindCallback<SingleContactInfo>() {
            @Override
            public void done(List<SingleContactInfo> contacts, ParseException error) {
                if(contacts != null){
                    mAdapter.clear();
                    for (int i = 0; i < contacts.size(); i++) {
                        mAdapter.add(contacts.get(i));
                        mAdapter.notifyDataSetChanged();
                    }
                }
            }
        });
    }

ContactsAdapter.java

public class ContactsAdapter extends  ArrayAdapter<SingleContactInfo> {
    private final Context mContext;
    private List<SingleContactInfo> mContacts;
    public TextView mContactName;
    public TextView mNumber;
    public TextView mEdit;
    public TextView mDelete;

    public ContactsAdapter(Context context, List<SingleContactInfo> objects) {
        super(context, R.layout.add_emergency_contacts_two, objects);
        this.mContext = context;
        this.mContacts = objects;
    }


    public View getView(final int position, View convertView, final ViewGroup parent) {
        if (convertView == null) {
            LayoutInflater mLayoutInflater = LayoutInflater.from(mContext);
            convertView = mLayoutInflater.inflate(R.layout.add_emergency_contacts_two, null);
        }

        final SingleContactInfo contact = mContacts.get(position);

        mContactName = (TextView) convertView.findViewById(R.id.emergency_contact_name);
        mNumber = (TextView) convertView.findViewById(R.id.emergency_contact_number);
        mEdit = (TextView) convertView.findViewById(R.id.edit_textView);
        mDelete = (TextView) convertView.findViewById(R.id.delete_textView);



        mDelete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                deleteContactDialog(contact, mContext, position);
                Toast.makeText(getContext(), "Deleted", Toast.LENGTH_LONG).show();
            }
        });

        mContactName.setText(contact.getName());
        mNumber.setText(contact.getNumber());



        return convertView;
    }
//TODO Look below here
    public void deleteContactDialog(final SingleContactInfo contact, Context context, final int position) {
        AlertDialog.Builder confirmDelete = new AlertDialog.Builder(context);
        confirmDelete.setMessage("Are You Sure You Want to Delete Contact Person")
                .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        try {
                        } catch (ParseException e) {
                            e.printStackTrace();
                        }
                    }
                })
                .setNegativeButton("No", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {

                    }
                });

        AlertDialog dialog = confirmDelete.create();

        dialog.show();

    }



}

2 个答案:

答案 0 :(得分:1)

您只需要从联系人的ArrayList中删除所需位置的项目,然后通知数据集:

public void onClick(DialogInterface dialog, int which) {
    try {
        mContacts.remove(position);
        notifyDataSetChanged();
    } 
    catch (ParseException e) {
        e.printStackTrace();
    }
}

此外,从mAdapter.notifyDataSetChanged();周期之外拨打for也足够了。

答案 1 :(得分:1)

 public void deleteContactDialog(final SingleContactInfo contact, Context context, final int position) {
    AlertDialog.Builder confirmDelete = new AlertDialog.Builder(context);
    confirmDelete.setMessage("Are You Sure You Want to Delete Contact Person")
            .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {

                   //Just add these two lines and you should be good.

                   mContacts.remove(position);
                   ContactsAdapter.this.notifyDataSetChanged();
                }
            })
            .setNegativeButton("No", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {

                }
            });

    AlertDialog dialog = confirmDelete.create();

    dialog.show();

}