删除和编辑android中的列表项?

时间:2016-04-23 07:53:56

标签: android arrays json android-arrayadapter

我有一个列表视图,其中有两个按钮编辑和删除。点击这些按钮我可以从我的自定义适配器更新或删除列表视图中的项目。但问题是我是否将新项目添加到列表中之前的更改将被丢弃,并显示旧值。

  

以下是我的适配器代码。

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

以下是Handler类活动中的代码。

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


            }
        });
    }
  

构造json数组的代码。

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

解析数组的代码。

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

    }
  

问题是数组列表在内部更新或删除   适配器类,但活动内的json数组仍旧   我想知道如何从中更新活动   适配器,以便json数组具有更新的值?

感谢任何帮助或建议。谢谢。

0 个答案:

没有答案