1

I have two classes that I'm working with. Contacts and CustomAdapter. In my Contacts Class I have an onActivityResult() method, which gets data from a different activity and places it in a Custom ListView using my CustomAdapter Class. The data gets added fine. Each row consists of a name, email, phone number AND a Button Widget. My question is, I would like to be able to press this Button and have that specific row be deleted. I've tried a number of different things but nothing seems to be working.

I placed the code below. If anyone has any suggestions on the best way to do this, I would greatly appreciate it. Thank you.

onActivityResult in Contacts Class:

    @Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if(requestCode == REQUEST_CODE){
        if(resultCode == RESULT_OK){
            String name = data.getStringExtra("name");
            String phone = data.getStringExtra("phone");
            final String email = data.getStringExtra("email");
            //These are array lists declared earlier
            phoneNums.add(phone);
            names.add(name);
            emails.add(email);

            customAdapter = new CustomAdapter(Contacts.this,names,phoneNums,emails);
            contactList.setAdapter(customAdapter);

 contactList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                    names.remove(position);
                    phoneNums.remove(position);
                    phoneNums.remove(position);

                    //This method is still not being recognized
                    contactList.getAdapter().notifyDataSetChanged()

                   //This one is but the app is crashing when I click on any of the rows

                    contactList.getAdapter().notify()
                }
            });

        }
    }
}

Custom Adapter Entire Class:

public class CustomAdapter extends BaseAdapter implements View.OnClickListener {
private Context context;
private ArrayList<String>phoneNumbers;
private ArrayList<String>names;
private ArrayList<String>emails;
private static LayoutInflater inflater = null;


public CustomAdapter(Context c,ArrayList<String>n,ArrayList<String>nums,ArrayList<String>e){
    context = c;
    phoneNumbers = nums;
    names = n;
    emails = e;
    inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

}
@Override
public int getCount() {
    return names.size();
}

@Override
public Object getItem(int position) {
    return position;
}

@Override
public long getItemId(int position) {
    return (long)position;
}

@Override
public View getView(final int position, final View convertView, ViewGroup parent) {
    View view = convertView;

    if (view == null){
        view = inflater.inflate(R.layout.contacts_custom_row,null);
        Button deleteBtn = (Button)view.findViewById(R.id.customRowDeleteButton);
        TextView name = (TextView)view.findViewById(R.id.customRowContactName);
        TextView phone = (TextView)view.findViewById(R.id.customRowContactNumber);
        TextView email = (TextView)view.findViewById(R.id.customRowContactEmail);

        name.setText(names.get(position));
        phone.setText(phoneNumbers.get(position));
        email.setText(emails.get(position));

        deleteBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
             //SHOULD I PLACE CODE TO DELETE THE ROW IN HERE?

            }
        });


    }
    return view;
}

1 Answer 1

1

Basically what you need is one List<>to rule the size of you List (i believe yours is the names. For that, your getItem(int position) has to return names.size(). To delete a specific row, you just need to delete the index of names that you want, and call notifyDataSetChanged() in your adapter after that.

Sign up to request clarification or add additional context in comments.

11 Comments

So my getItem method looks like this now....public Object getItem(int position) {return names.size();} Do you know how and where I would utilize this now? Back in the Contacts class or within the onClick in the getView method above?Thank you.
you dont need to use this method, it's used automatically by the adapter.
When you say "delete the index of names that you want", where would I place this code?
You can put this code on the click listener that you already defined. Delete the index, and call this.notifyDataSetChanged();
I tried this in my onClick() names.remove(position);notifyDataSetChanged(); This is only deleting my last row in the ListView, instead of the specific one that I click. What should I pass in the names.remove parameter instead? The position variable isn't quite working.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.