4

I have a list view with two buttons edit and delete.On click of these buttons I either update or delete the items in the list view from my custom adapter.But the problem is if I add a new item to the list my previous changes are discarded and old values are displayed.

The following is my code for the adapter.

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;

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

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

The following is the code inside the activity for the Handler class.

public void userRolehandler()
    {
        final Handler handler = new Handler();
        handler.post(new Runnable() {
            @Override
            public void run()
            {
                RoleList roleList = new RoleList(UserRegistration.this,employee_name,emp_username,emp_password,employee_role);
                Log.d("ARRAY SIZE", String.valueOf(employee_name.size()));

                userList.setAdapter(roleList);
                roleList.notifyDataSetChanged();


            }
        });
    }

Code for constructing the json array.

  public void userRoleArray() throws JSONException {
        name = editTextName.getText().toString();
        username = editTextUsername.getText().toString();
        password = editTextPassword.getText().toString();
        emp_role = textRole.getText().toString();

        JSONObject jsonobj = new JSONObject();
        jsonobj.put("name",name);
        jsonobj.put("username",username);
        jsonobj.put("password",password);
        jsonobj.put("emp_role",emp_role);

        rolejson.put(counter,jsonobj);
        counter++;
    }

Code for Parsing the array.

  public void travRoleArray() throws JSONException {
        response = "{\"result\":" + rolejson.toString() + "}";
        Log.d("RESPONSE",response);
        JSONObject jsonOb = new JSONObject(response);
        JSONArray result = jsonOb.getJSONArray(JSON_ARRAY);
        try
        {
            employee_name.clear();
            emp_username.clear();
            emp_password.clear();
            employee_role.clear();
            for (int i=0; i< result.length();i++)
            {
                JSONObject jObj = result.getJSONObject(i);
                employee_name.add(i,jObj.getString(KEY_NAME));
                emp_username.add(i, jObj.getString(KEY_UNAME));
                emp_password.add(i, jObj.getString(KEY_PASS));
                employee_role.add(i,jObj.getString(KEY_ROLE));
            }

            for (String name:employee_name)
            {
                Log.i("Name : ",name);

            }
        }catch (ArrayIndexOutOfBoundsException e)
        {
            Toast.makeText(UserRegistration.this, "Array Index out of bound exception", Toast.LENGTH_LONG).show();
        }

    }

The problem is that the array list gets updated or deleted inside the adapter class but the json array inside the activity still has the old values.I would like to know how can I update the activity from the adapter so that the json array has the updated values?

Any help or suggestion is appreciated.Thank you.

AndroidNewBee
  • 744
  • 3
  • 12
  • 36
  • 1
    Please have a look at this answer: http://stackoverflow.com/questions/36808319/how-to-send-data-from-list-adapter-to-activity-in-android/36808531#36808531 – Yurii Tsap Apr 23 '16 at 08:44

0 Answers0