在ListView中删除自定义行

时间:2015-09-22 22:49:02

标签: android

我有两个与我合作的课程。联系人和CustomAdapter。在我的Contacts类中,我有一个onActivityResult()方法,它从不同的活动中获取数据,并使用我的CustomAdapter类将它放在Custom ListView中。数据得到了很好的补充。每行包含名称,电子邮件,电话号码和按钮小部件。我的问题是,我希望能够按下此按钮并删除该特定行。我尝试了很多不同的东西,但似乎没有任何工作。

我把代码放在下面。如果有人对最佳方法有任何建议,我将不胜感激。谢谢。

联系人类中的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) {
                    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()
                }
            });

        }
    }
}

自定义适配器整个类:

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 个答案:

答案 0 :(得分:1)

基本上你需要一个List<>来统治你的名单(我相信你的是names。为此,你的getItem(int position)必须返回names.size()要删除特定行,您只需要删除所需的names索引,然后在适配器中调用notifyDataSetChanged()