删除自定义列表中的点击行

时间:2015-09-23 16:15:49

标签: android

我需要帮助弄清楚为什么以下代码不会删除自定义ListView中的单击行。

基本上,我有两个与我合作的课程。联系人和CustomAdapter。在我的Contacts类中,我有一个onActivityResult()方法,它从不同的活动中获取数据,并使用我的CustomAdapter类将它放在Custom ListView中。数据得到了很好的补充。我有一个onItemClickListener方法,在我的contactList设置CustomAdapter之后设置。理想情况下,该方法应删除按下的特定行。我尝试了很多不同的东西,但似乎没有任何效果。我正在使用下面的代码删除要删除的行但是它只删除了最后一行,而不是我点击的行。

我把代码放在下面。如果有人有任何建议我会非常感激。谢谢。

联系人类中的onActivityResult:

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

                }
            });
        }
    }
}

自定义适配器整个类:

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

2 个答案:

答案 0 :(得分:2)

创建一个如下所示的类:

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

}

声明一个联系人数组,并在你的onActivityResult中添加:

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

将监听器更改为此。

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

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

                customAdapter.notifyDataSetChanged();

            }
        });

将适配器类更改为:

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

我认为它应该可以解决你的问题。

答案 1 :(得分:0)

使用你的适配器删除它并刷新你的视图。我认为这是更合适的方式。