简体   繁体   English

从onClickListener中的自定义适配器创建的ListView中删除行

[英]Delete row from ListView created by Custom Adapter in onClickListener

Hi i get my data from a server and pass them in my Custom Adapter to populate my ListView. 嗨,我从服务器获取数据,并将其传递到“自定义适配器”中以填充ListView。 Below is my custom Adapter code: 以下是我的自定义适配器代码:

public class AppointmentAdapter extends BaseAdapter {

private AppointmentAdapter adapter;
private Activity activity;
private ArrayList<HashMap<String, String>> data;
private static LayoutInflater inflater=null;
HashMap<String, String> todo = new HashMap<String, String>();
 // String confirmation;
RelativeLayout confirm_corner;


public AppointmentAdapter(Activity a, ArrayList<HashMap<String,        String>> d) {
    activity = a;
    data=d;
    inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    //imageLoader=new ImageLoader(activity.getApplicationContext());
}

public int getCount() {
    return data.size();
}

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

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



public View getView(final int position, View convertView, ViewGroup parent) {
    //View vi=convertView;
    //if(convertView==null)
       final View  vi = inflater.inflate(R.layout.appointments_list_item, null);
    adapter = new AppointmentAdapter(activity,data);
    TextView aid = (TextView)vi.findViewById(R.id.aid); // id
    TextView apptitle = (TextView)vi.findViewById(R.id.apptitle); // title
    TextView starts_date = (TextView)vi.findViewById(R.id.starts_date); // created_at
    TextView starts_time = (TextView)vi.findViewById(R.id.starts_time);
    TextView contact = (TextView)vi.findViewById(R.id.contact);
    TextView confirmation = (TextView)vi.findViewById(R.id.confirmation);
    confirm_corner = (RelativeLayout)vi.findViewById(R.id.confirm_corner);

    //CheckBox check = (CheckBox)vi.findViewById(R.id.tododone); // checkbox


    todo = data.get(position);

    // Setting all values in listview
    aid.setText(todo.get(AppointmentsFragment.TAG_AID));
    apptitle.setText(todo.get(AppointmentsFragment.TAG_APPTITLE));
    starts_date.setText(todo.get(AppointmentsFragment.TAG_STARTDATE));
    starts_time.setText(todo.get(AppointmentsFragment.TAG_STARTTIME));
    contact.setText(todo.get(AppointmentsFragment.TAG_CONTACT));
    confirmation.setText(todo.get(AppointmentsFragment.TAG_CONFIRMATION));
    String test = todo.get(AppointmentsFragment.TAG_USER_EMAIL);
    //Handle buttons and add onClickListeners
    ImageView accept = (ImageView)vi.findViewById(R.id.accept);
    ImageView deny = (ImageView)vi.findViewById(R.id.deny);
    //String test = confirmation.getText().toString();
    //&& (!test.equals(MainActivity.user_email))
    Log.d("CONFIRMATION: ", MainActivity.user_email);
    if (confirmation.getText().toString().equals("pending")  ){
        Log.d("PASS CONFIRMATION: ", confirmation.getText().toString());
        confirm_corner.setVisibility(View.VISIBLE);
    }

    accept.setOnClickListener(new View.OnClickListener(){
        @Override
        public void onClick(View v) { 
            //do something
            confirm_corner = (RelativeLayout)vi.findViewById(R.id.confirm_corner);
            Log.d("Button accept: ", MainActivity.user_email);
            new Confirm_appointment().execute();
            //vi.setBackgroundColor(Color.RED);
            confirm_corner.setVisibility(View.GONE);


        }
    });
    deny.setOnClickListener(new View.OnClickListener(){
        @Override
        public void onClick(View v) { 
            //do something
            Log.d("Button deny: ", MainActivity.user_email);
            new Deny_appointment().execute();
            confirm_corner.setVisibility(View.INVISIBLE);
            //adapter = new AppointmentAdapter(this,data);
            data.remove(position);
            data.clear();
            data.addAll(data); 
            adapter.notifyDataSetChanged();
        }
    });


    return vi;
}

I want when i click on the deny button that row to be removed. 我想在单击“拒绝”按钮时将其删除。 What should i change in my code to get this fixed? 我应该在代码中进行哪些更改以解决此问题?

What is the purpose of the following two calls?: 以下两个调用的目的是什么?

data.clear();
data.addAll(data); 

To remove an element from your ListView simply call data.remove(position) and then update the Adapter ( adapter.notifyDataSetChanged() ). 要从ListView删除元素,只需调用data.remove(position) ,然后更新Adapteradapter.notifyDataSetChanged() )。 See if that works. 看看是否可行。

Don't create a new AppointmentAdapter inside the AppointmentAdapter . 不要在AppointmentAdapter内部创建新的AppointmentAdapter When you try to notifyDataSetChanged() after you removed a row you then notify the wrong adapter. 当您在删除一行后尝试使用notifyDataSetChanged() ,您将通知错误的适配器。 You notify the newly constructed adapter which is not the one that has changed data. 您将通知新构造的适配器,该适配器不是已更改数据的适配器。 (The newly constructed ones are not attached to the listview either so they couldn't make sure the notifications reach that anyway.) (新构建的通知也未附加到列表视图,因此它们也无法确保通知到达该列表。)

You should remember that only a single adapter is needed to work together with the a ListView which has multiple item Views. 您应该记住,只需一个适配器即可与具有多个项目视图的ListView一起使用。 You construct one adapter per item view but that wont work. 您为每个项目视图构造一个适配器,但这将无法工作。 The adapter is supposed to cooperate with the ListView and not with the item views. 该适配器应该与ListView配合,而不与项目视图配合。

There are many things I would do different with your code and I think you should implement the View Holder pattern. 我将对您的代码做很多事情,我认为您应该实现View Holder模式。 You can read about it here . 你可以在这里阅读。

update 更新

Instead of calling adapter.notifyDataSetChanged() you have to call it on this . 不必调用adapter.notifyDataSetChanged() ,而必须在this上调用它。 Where this should be the adapter where you just removed the row. this应该是刚刚删除该行的适配器。 Because you are using anonymous (non-static) inner classes for the View.OnClickListener you can reach the outer class by using AppointmentAdapter.this , which is the adapter that you want. 因为您正在为View.OnClickListener使用匿名(非静态)内部类,所以可以使用AppointmentAdapter.this到达外部类, AppointmentAdapter.this是所需的适配器。

Try this: 尝试这个:

deny.setOnClickListener(new View.OnClickListener(){
    @Override
    public void onClick(View v) { 
        //do something
        Log.d("Button deny: ", MainActivity.user_email);
        new Deny_appointment().execute();
        confirm_corner.setVisibility(View.INVISIBLE);
        AppointmentAdapter.this.data.remove(position);
        AppointmentAdapter.this.notifyDataSetChanged();
    }
});

By the way, I agree with Willis that the lines where you clear out your data and reload it seems just wrong. 顺便说一句,我同意Willis的观点,即清除数据并重新加载数据的行似乎是错误的。 So I threw them out too. 所以我也把它们扔了出去。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 删除自定义适配器中的ListView行 - Delete ListView row in Custom Adapter Android ListView onClickListener自定义适配器 - Android ListView onClickListener Custom Adapter Android删除自定义数组适配器中的listview行 - Android delete listview row in custom array adapter Android ListView:如何使用自定义适配器上的活动onClickListener? - Android ListView: How to use the activity onClickListener from a custom Adapter? 如何从自定义列表视图数组适配器类中的列表视图中删除行,并刷新列表视图中的其余项? - How to delete the row from a listview in custom listview array adapter class and refresh the remaining items inside listview? 如何使用自定义适配器从listView删除行 - How to delete a row from listView with customised adapter 自定义适配器不刷新android中onclicklistener的listview - custom adapter do not refresh the listview for onclicklistener in android 使用自定义适配器从ListView删除项目 - Delete items from ListView with a custom adapter ListView行按钮:如何创建将View.OnClickListener连接到ListView每行上的按钮的自定义适配器? - ListView row buttons: How do I create a custom Adapter that connects a View.OnClickListener to a button on each row of a ListView? 如何删除android中基本适配器中自定义列表视图中的行 - how to delete row in custom listview in base adapter in android
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM