传递给OnClickListener

传递给OnClickListener

我的活动适配器可以处理我的主要物品清单。当我单击删除时,我想提示用户,如果确认,请从列表中删除该项目并刷新视图。

我有删除项目的逻辑,但是似乎没有办法将参数向下传递给OnClickListener。如果单击列表上的按钮,如何捕获单击的位置,以及如何将其传递给OnClickListener函数,以便可以删除正确的元素?

任何帮助,将不胜感激,

谢谢,
拉德克

 @Override
   public View getView(final int _position, View convertView, ViewGroup parent) {

    try {
        if(convertView == null){
            convertView = layoutInflater.inflate(com.mylist.www.R.layout.activity_main_row,parent,false);
        }

        TextView textView = (TextView) convertView.findViewById(com.mylist.www.R.id.rowText);
        textView.setText(listItems.getJSONObject(_position).getString("description"));

        // set the checkbox stuff
        CheckBox chkEnabled = (CheckBox) convertView.findViewById(com.mylist.www.R.id.chkEnabled);

        textView.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                try{
                    if (listItems != null){
                        JSONArray clickedItems = listItems.getJSONObject(_position).getJSONArray("listItems"); // position
                        Intent intent = new Intent(context, ListActivity.class);

                        intent.putExtra("listItems", clickedItems.toString());
                        context.startActivity(intent);
                    }
                } catch (Exception e){
                    e.printStackTrace();
                }

            }

        });

        Button btnNavigate = (Button) convertView.findViewById(com.mylist.www.R.id.btnDelete);
        btnNavigate.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                try{
                    if (listItems != null){

                        AlertDialog.Builder builder = new AlertDialog.Builder(context);
                        builder.setMessage("Are you sure?").setPositiveButton("Yes", dialogClickListener)
                                .setNegativeButton("No", dialogClickListener).show();
                    }
                } catch (Exception e){
                    e.printStackTrace();
                }

            }

        });


        return convertView;
    } catch (Exception e) {
        e.printStackTrace();
        return null;
    }
}

DialogInterface.OnClickListener dialogClickListener = new DialogInterface.OnClickListener() {
    @Override
    public void onClick(DialogInterface dialog, int which) {

        try {
            switch (which){
                case DialogInterface.BUTTON_POSITIVE:
                    //Yes button clicked
                    listItems.remove(position); //????? how do I pass it

                    JSONObject jObj = new JSONObject();
                    jObj.put("Items", listItems);

                    editor.putString("json",jObj.toString());
                    editor.commit();

                    ((MainActivity)context).refreshView();

                    break;

                case DialogInterface.BUTTON_NEGATIVE:
                    //No button clicked
                    break;
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }
};

最佳答案

为什么不将头寸存储在另一个变量中,以后再使用

int currentPosition;


用户单击按钮时将当前视图位置存储在getView()

currentPosition = position;

AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setMessage("Are you sure?").setPositiveButton("Yes", dialogClickListener)
                            .setNegativeButton("No", dialogClickListener).show();


然后在听众中使用它

listItems.remove(currentPosition);

10-08 17:53