如何将数据从列表适配器发送到android中的活动?

时间:2016-04-23 08:10:08

标签: java android listadapter

我有一个自定义列表适配器我想将此列表适配器中的值传递给我设置适配器的活动。 这是我的适配器代码。

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);
}
   }

记录空指针异常。

  

04-23 14:59:16.462 24919-24919 / org.bordetuts.com.goldmine   E / AndroidRuntime:致命异常:主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 / 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:   在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:   在dalvik.system.NativeStart.main(本地方法)

     

我想通知活动内部发生了一些变化   适配器。例如。按下删除某个位置。任何   建议,帮助或指示会有所帮助。谢谢。

1 个答案:

答案 0 :(得分:1)

您的活动:

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);
}

此外,你可以使用事件总线,奥托,但我认为对于你的情况只是一个自定义回调就足够了。这取决于。