I have a list view with two edit and delete buttons. When I click these buttons, I either update or delete the items in the list view from my user adapter. But the problem is that I will add a new item to my list; previous changes are discarded and old values are displayed.
Below is the code for my 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;
}
}
Below is the code inside the operation 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 building a 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 an 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 list of arrays is updated or deleted inside the adapter class, but there are still old values in the json array inside the activity. I would like to know how can I update the activity from the adapter so that the values are updated in the json array?
. .