如何将数据从列表适配器发送到Android中的活动? [英] How to send data from list adapter to activity in android?

查看:89
本文介绍了如何将数据从列表适配器发送到Android中的活动?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个自定义列表适配器,我想将值从该列表适配器传递到设置适配器的活动中. 这是我的适配器代码.

I have a custom list adapter I would like to pass values from this list adapter to an activity where I set the adapter. This is my Adapter code.

public class RoleList extends ArrayAdapter<String>
{
    private ArrayList<String> name;
    private ArrayList<String> username;
    private ArrayList<String>  password;
    private ArrayList<String>  role;
    private Activity context;

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

    @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)
            {
                name.remove(position);
                username.remove(position);
                password.remove(position);
                role.remove(position);
                callback.onActionPerformed(position);
                notifyDataSetChanged();
            }
        });
        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 MyActionCallback{
 void onActionPerformed(int position);
}
   }

记录Null指针异常.

Log for Null pointer exception.

04-23 14:59:16.462 24919-24919/org.bordetuts.com.goldmine E/Android运行时:致命异常:主要04-23 14:59:16.462 24919-24919/org.bordetuts.com.goldmine E/AndroidRuntime:流程: org.bordetuts.com.goldmine,PID:24919 04-23 14:59:16.462 24919-24919/org.bordetuts.com.goldmine E/Android运行时: java.lang.NullPointerException 04-23 14:59:16.462 24919-24919/org.bordetuts.com.goldmine E/AndroidRuntime:at org.bordetuts.com.goldmine.adapter.RoleList $ 1.onClick(RoleList.java:67) 04-23 14:59:16.462 24919-24919/org.bordetuts.com.goldmine E/AndroidRuntime:位于 android.view.View.performClick(View.java:4444)04-23 14:59:16.462 24919-24919/org.bordetuts.com.goldmine E/AndroidRuntime:at android.view.View $ PerformClick.run(View.java:18440)04-23 14:59:16.462 24919-24919/org.bordetuts.com.goldmine E/AndroidRuntime:at android.os.Handler.handleCallback(Handler.java:733)04-23 14:59:16.462 24919-24919/org.bordetuts.com.goldmine E/AndroidRuntime:at android.os.Handler.dispatchMessage(Handler.java:95)04-23 14:59:16.462 24919-24919/org.bordetuts.com.goldmine E/AndroidRuntime:at android.os.Looper.loop(Looper.java:136)04-23 14:59:16.462 24919-24919/org.bordetuts.com.goldmine E/AndroidRuntime:at android.app.ActivityThread.main(ActivityThread.java:5028)04-23 14:59:16.462 24919-24919/org.bordetuts.com.goldmine E/Android运行时: 在java.lang.reflect.Method.invokeNative(本地方法)04-23 14:59:16.462 24919-24919/org.bordetuts.com.goldmine E/Android运行时: 在java.lang.reflect.Method.invoke(Method.java:515)04-23 14:59:16.462 24919-24919/org.bordetuts.com.goldmine E/AndroidRuntime:at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:788) 04-23 14:59:16.462 24919-24919/org.bordetuts.com.goldmine E/AndroidRuntime:位于 com.android.internal.os.ZygoteInit.main(ZygoteInit.java:604)04-23 14:59:16.462 24919-24919/org.bordetuts.com.goldmine E/Android运行时: 在dalvik.system.NativeStart.main(本机方法)

04-23 14:59:16.462 24919-24919/org.bordetuts.com.goldmine E/AndroidRuntime: FATAL EXCEPTION: main 04-23 14:59:16.462 24919-24919/org.bordetuts.com.goldmine E/AndroidRuntime: Process: org.bordetuts.com.goldmine, PID: 24919 04-23 14:59:16.462 24919-24919/org.bordetuts.com.goldmine E/AndroidRuntime: java.lang.NullPointerException 04-23 14:59:16.462 24919-24919/org.bordetuts.com.goldmine E/AndroidRuntime: at org.bordetuts.com.goldmine.adapter.RoleList$1.onClick(RoleList.java:67) 04-23 14:59:16.462 24919-24919/org.bordetuts.com.goldmine E/AndroidRuntime: at android.view.View.performClick(View.java:4444) 04-23 14:59:16.462 24919-24919/org.bordetuts.com.goldmine E/AndroidRuntime: at android.view.View$PerformClick.run(View.java:18440) 04-23 14:59:16.462 24919-24919/org.bordetuts.com.goldmine E/AndroidRuntime: at android.os.Handler.handleCallback(Handler.java:733) 04-23 14:59:16.462 24919-24919/org.bordetuts.com.goldmine E/AndroidRuntime: at android.os.Handler.dispatchMessage(Handler.java:95) 04-23 14:59:16.462 24919-24919/org.bordetuts.com.goldmine E/AndroidRuntime: at android.os.Looper.loop(Looper.java:136) 04-23 14:59:16.462 24919-24919/org.bordetuts.com.goldmine E/AndroidRuntime: at android.app.ActivityThread.main(ActivityThread.java:5028) 04-23 14:59:16.462 24919-24919/org.bordetuts.com.goldmine E/AndroidRuntime: at java.lang.reflect.Method.invokeNative(Native Method) 04-23 14:59:16.462 24919-24919/org.bordetuts.com.goldmine E/AndroidRuntime: at java.lang.reflect.Method.invoke(Method.java:515) 04-23 14:59:16.462 24919-24919/org.bordetuts.com.goldmine E/AndroidRuntime: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:788) 04-23 14:59:16.462 24919-24919/org.bordetuts.com.goldmine E/AndroidRuntime: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:604) 04-23 14:59:16.462 24919-24919/org.bordetuts.com.goldmine E/AndroidRuntime: at dalvik.system.NativeStart.main(Native Method)

我想通知活动, 适配器.例如在特定位置按下了删除键.任何 建议,帮助或指示会有所帮助.谢谢.

I would like to notify the activity that something changed inside the adapter.For eg. Delete was pressed for a certain position. Any suggestions,help or pointers will help.Thank you.

推荐答案

您的活动:

public class MainActivity extends AppCompatActivity implements YourCustomAdapter.MyActionCallback {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    // or something like that
    ListView listView = findViewById(R.id.my_list_view);
    listView.setAdapter(new YourCustomAdapter(this));
}



@Override
public void onActionPerformed(int position) {
    //do here something with your data

}

您的适配器:

public class YourCustomAdapter extends BaseAdapter {

public YourCustomAdapter(
        MyActionCallback actionCallback) {
    mActionCallback = actionCallback;
}

private MyActionCallback mActionCallback;

@Override
public int getCount() {
    return 0;
}

@Override
public Object getItem(int position) {
    return null;
}

@Override
public long getItemId(int position) {
    return 0;
}

@Override
public View getView(final int position, View convertView, ViewGroup parent) {
    // your case specific code
    // don't forget to implement view re-using properly,
    // don't inflate view each time
    delete.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v)
        {
            name.remove(position);
            username.remove(position);
            password.remove(position);
            role.remove(position);
            //pass position of deleted object or any other thing you want
            mActionCallback.onActionPerformed(position);
            notifyDataSetChanged();
        }
    });
    return null;
}

public interface MyActionCallback{
    void onActionPerformed(int position);
}

您还可以使用事件总线(例如Otto),但是我认为对于您的情况而言,只需一个自定义回调就足够了.这取决于.

Also you can use the Event Bus, Otto for example, but I think for your case just a custom callback will be enough. It depends.

这篇关于如何将数据从列表适配器发送到Android中的活动?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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