Mark F
Mark F

Reputation: 1543

DELETING CLICKED ROW IN CUSTOM LISTVIEW

I need help figuring out why the following code will not delete a clicked row in my Custom ListView.

Basically, 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. I have an onItemClickListener method that is set after my contactList sets its CustomAdapter. Ideally the method should delete the specific row that is pressed. I've tried a number of different things but nothing seems to be working. I;m getting the rows to delete with the code below BUT IT"S ONLY DELETING THE LAST ROW ADDED vs one that I click.

I placed the code below. If anyone has any suggestions 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) {
                    //IS THIS CODE CORRECT?
                    names.remove(position);
                    phoneNums.remove(position);
                    emails.remove(position);

                    customAdapter.notifyDataSetChanged();

                }
            });
        }
    }
}

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

        TextView deleteText = (TextView)view.findViewById(R.id.customRowDeleteText);
        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));

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

            }
        });


    }
    return view;
}

Upvotes: 0

Views: 58

Answers (2)

user5156016
user5156016

Reputation:

Create a class that looks like this:

public class Contact {

private String name;
private String phone;
private String email;

public Contact(String name, String phone, String email){
    this.setName(name);
    this.setPhone(phone);
    this.setEmail(email);

}

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

public String getPhone() {
    return phone;
}

public void setPhone(String phone) {
    this.phone = phone;
}

public String getEmail() {
    return email;
}

public void setEmail(String email) {
    this.email = email;
}

}

Declare an array of Contacts and In your onActivityResult add:

contacts.add(new Contact(name,phone,email);

Change the listener to this.

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

               contacts.remove(customAdapter.getItem(position));

                customAdapter.notifyDataSetChanged();

            }
        });

Change your adapter class to this:

public class CustomAdapter extends BaseAdapter{
    private Context context;
    private ArrayList<Contact> contacts;
    private static LayoutInflater inflater = null;


public CustomAdapter(Context context,ArrayList<Contact> contacts){
    this.context = context;
   this.contacts = contacts;
    inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

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

@Override
public Object getItem(int position) {
    return contacts.get(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;
    Contact contact = getItem(position);

    if (view == null){
        view = inflater.inflate(R.layout.contacts_custom_row,null);
    }

    TextView deleteText =  (TextView)view.findViewById(R.id.customRowDeleteText);
    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(contact.getName());
    phone.setText(contact.getPhone());
    email.setText(contact.getEmail());

    return view;
}

I think it should resolve your problems.

Upvotes: 2

Satyavrat
Satyavrat

Reputation: 469

Delete it by using your adapter and refresh your view.i think this is more suitable way.

Upvotes: 0

Related Questions