簡體   English   中英

從Android中的自定義列表適配器調用活動方法?

[英]Calling a activity method from a custom list adapter in android?

在按鈕上單擊,我正在嘗試使用Google的凌空庫將一些數據發送到我的PHP腳本。 發送數據的方法在Activity中,我編寫了一個接口,以從自定義列表適配器調用該方法。

以下是我的適配器代碼:

public class RoleList extends ArrayAdapter<String> {

    public static final String DELETEUSERINFO = VirtualMachineIp.VMIP_ADDRESS.trim() + "goldmine/getuserinfo.php";
    public static final String KEY_NAME = "name";
    public static final String KEY_ROLE = "role";
    private ArrayList<String> name;
    private ArrayList<String> username;
    private ArrayList<String> password;
    private ArrayList<String> role;
    private Activity context;

    public int getPos() {
        return pos;
    }

    public void setPos(int pos) {
        this.pos = pos;
    }

    int pos;

    public boolean isDelPressed() {
        return delPressed;
    }

    public void setDelPressed(boolean delPressed) {
        this.delPressed = delPressed;
    }

    boolean delPressed;

    private onDelCallback listener;

    public RoleList(Activity context, ArrayList<String> name, ArrayList<String> username, ArrayList<String> password, ArrayList<String> role, onDelCallback listener) {
        super(context, R.layout.role_list, name);
        this.context = context;
        this.name = name;
        this.username = username;
        this.password = password;
        this.role = role;
        this.listener = listener;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        LayoutInflater inflater = context.getLayoutInflater();
        final View listViewItem = inflater.inflate(R.layout.role_list, null, true);
        final TextView textViewName = (TextView) listViewItem.findViewById(R.id.tv_empname);
        final TextView textViewusername = (TextView) listViewItem.findViewById(R.id.tv_empusername);
        final TextView textViewPass = (TextView) listViewItem.findViewById(R.id.tv_emppassword);
        final TextView textViewRole = (TextView) listViewItem.findViewById(R.id.tv_emprole);
        Button edit = (Button) listViewItem.findViewById(R.id.btn_editRole);
        Button delete = (Button) listViewItem.findViewById(R.id.btn_delRole);


        delete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                setDelPressed(true);
                name.remove(position);
                username.remove(position);
                password.remove(position);
                role.remove(position);
                listener.deleteuser(position);
                notifyDataSetChanged();
                setPos(position);

            }
        });
        edit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Log.d("Emp Info", name.get(position) + " " + username.get(position) + " " + password.get(position) + " " + role.get(position));

                final Dialog dialog = new Dialog(getContext());
                dialog.setContentView(R.layout.userreg);
                dialog.setTitle("Edit Employee " + name.get(position) + " details");
                final String[] arraySpinner = new String[]{"Manager", "Stockist", "Cashier", "Accountant"};
                dialog.setCancelable(false);
                dialog.setCanceledOnTouchOutside(false);
                final EditText emp_name = (EditText) dialog.findViewById(R.id.editTextName);
                final EditText emp_uname = (EditText) dialog.findViewById(R.id.editTextUserName);
                final EditText emp_pw = (EditText) dialog.findViewById(R.id.editTextPassword);
                final Spinner emp_role = (Spinner) dialog.findViewById(R.id.spinner_role);
                final TextView textRole = (TextView) dialog.findViewById(R.id.tv_selected_role);
                ArrayAdapter<String> adapter = new ArrayAdapter<String>(getContext(), android.R.layout.simple_spinner_item, arraySpinner);
                emp_role.setAdapter(adapter);
                emp_role.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
                    @Override
                    public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                        Toast.makeText(getContext(), "Role Selected is " + arraySpinner[position], Toast.LENGTH_SHORT).show();

                        String employee_role = arraySpinner[position];
                        textRole.setText(employee_role);

                    }

                    @Override
                    public void onNothingSelected(AdapterView<?> parent) {

                    }
                });


                emp_name.setText(name.get(position));
                emp_uname.setText(username.get(position));
                emp_pw.setText(password.get(position));
                emp_role.setSelection(position);

                Button buttoncancel = (Button) dialog.findViewById(R.id.buttonCancel);
                buttoncancel.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        dialog.dismiss();
                    }
                });

                Button buttonChange = (Button) dialog.findViewById(R.id.buttonRegister);
                buttonChange.setText("Change");
                buttonChange.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        textViewName.setText(emp_name.getText().toString());
                        textViewusername.setText(emp_uname.getText().toString());
                        textViewPass.setText(emp_pw.getText().toString());
                        textViewRole.setText(textRole.getText());
                        dialog.dismiss();

                    }
                });


                dialog.show();
            }
        });


        textViewName.setText(name.get(position));
        textViewusername.setText(username.get(position));
        textViewPass.setText(password.get(position));
        textViewRole.setText(role.get(position));

        return listViewItem;
    }

    public interface onDelCallback {
        void deleteuser(int pos);
    }



}

理想情況下,按下列表內的按鈕時,應調用以下代碼:

@Override
    public void deleteuser(final int pos)
    {
        StringRequest stringRequest = new StringRequest(Request.Method.POST, DELETEUSERINFO, new Response.Listener<String>() {
            @Override
            public void onResponse(String response)
            {
                String resp = response.toString().trim();
                if (resp.equals("UNABLE TO DELETE USER"))
                {
                    Log.d("UNABLE TO DEL",resp);
                }else if (resp.equals("USER DELETED SUCCESSFULLY"))
                {
                    Toast.makeText(UserRegistration.this, "USER DELETED SUCCESSFULLY", Toast.LENGTH_SHORT).show();
                }

            }
        },
                new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {

                        Toast.makeText(UserRegistration.this, "Please check your internet connection", Toast.LENGTH_LONG).show();
                    }
                }){

            @Override
            protected Map<String, String> getParams() throws AuthFailureError {
                Map<String, String> params = new HashMap<>();
                params.put(KEY_NAME, employee_name.get(pos));
                params.put(KEY_ROLE,employee_role.get(pos));
                return params;
            }
        };
        RequestQueue rq = Volley.newRequestQueue(this);
        rq.add(stringRequest);
    }

我設置內部parseUserinfo(),它運行在的響應適配器getUserinfo()其余內調用onCreate().getUserinfo()是用來從所述遠程服務器請求數據。

這是我設置適配器的方式:

 RoleList roleList = new RoleList(UserRegistration.this, employee_name, emp_username, emp_password, employee_role,UserRegistration.this);
 userList.setAdapter(roleList);

我想知道我是否在正確的地方調用適配器。 我需要再打一次嗎?

任何幫助或建議,表示贊賞。 謝謝。

您可以通過某些方式從適配器調用您的活動方法:

  1. 當您當時創建適配器的構造函數時,可以在其活動對象中傳遞其活動對象,然后在適配器中使用該對象。

  2. 如果在適配器中創建接口,則必須在各自的活動中實現該接口,然后可以通過適配器構造函數傳遞活動對象,並可以使用所需的方法。

公共RoleList(活動上下文,ArrayList名稱,ArrayList用戶名,ArrayList密碼,ArrayList角色,onDelCallback偵聽器){}在上面的構造函數中,您必須傳遞各自的Activity對象,例如MyActvity mActivity,而不是傳遞通用的Activity對象。 我希望您的方法在MyActvity類中可用,並希望您已經在MyActvity類上實現了接口。

您可以像這樣在適配器中執行活動的刪除任務

delete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                UserRegistration act = (UserRegistration) context;
                act.deleteuser(position);
            }
        });

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM