通过自定义适配器中创建ListView中删除行onClickListener [英] Delete row from ListView created by Custom Adapter in onClickListener

查看:328
本文介绍了通过自定义适配器中创建ListView中删除行onClickListener的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好我从服务器获取我的数据,并通过他们在我的自定义适配器来填充我的ListView。下面是我的自定义适配器code:

Hi i get my data from a server and pass them in my Custom Adapter to populate my 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;
}

我想,当我的拒绝按钮被删除的行单击。我应该在code改怎么得到这个固定的?

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?

推荐答案

不要创建一个新的 AppointmentAdapter 中的 AppointmentAdapter 。当您尝试 notifyDataSetChanged()您删除了行后,你再通知错误的适配器。你通知新建成的适配器,它是不是有改变的数据之一。 (新构建的是没有连接到ListView要么使他们不能确保通知到达该反正。)

Don't create a new AppointmentAdapter inside the AppointmentAdapter. When you try to notifyDataSetChanged() after you removed a row you then notify the wrong adapter. 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.)

您应该记住,只有一个适配器需要与有多个项目视图的一个ListView共同努力。您构建每个项目视图中的一个适配器,但是这不会工作。该适配器被认为与ListView和不与该项目的观点进行合作。

You should remember that only a single adapter is needed to work together with the a ListView which has multiple item Views. 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.

有很多事情我会做不同的与code和我想你应该实现视图持有人格局。你可以阅读一下这里

There are many things I would do different with your code and I think you should implement the View Holder pattern. You can read about it here.

更新

而不是调用的 adapter.notifyDataSetChanged()你要调用它这个。其中,这个应该是你刚才删除的行适配器。因为你是使用匿名(非静态)内部类的 View.OnClickListener 您可以通过使用达到外部类 AppointmentAdapter.this ,这是你想要的适配器。

Instead of calling adapter.notifyDataSetChanged() you have to call it on this. Where this should be the adapter where you just removed the row. 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.

试试这个:

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. So I threw them out too.

这篇关于通过自定义适配器中创建ListView中删除行onClickListener的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆