troubleshooting Question

ListView editing data issue

Avatar of zolf
zolfFlag for United Arab Emirates asked on
JavaSmartphone ProgrammingAndroid
16 Comments1 Solution936 ViewsLast Modified:
Hello there,

I have a listview on which I have contextmenu delete and edit.the delete is working as expected.but when i select edit menu,a alertdialog box opens with the value i want to change in it. then i change the value and it shows that new value in the listview row. but when i send the data to save in the db.the old data is saved not the new value which i assigned.what is wrong.

public boolean onContextItemSelected(MenuItem item)	{
		final AdapterView.AdapterContextMenuInfo info;
		try {
			info = (AdapterView.AdapterContextMenuInfo) item.getMenuInfo();
		}
		catch (ClassCastException e) {
			Log.e("", "bad menuInfo", e);
			return false;
		}

		switch (item.getItemId()) {
			case R.id.edit_menu:
				
				String qty = listModel.get(info.position - 1).get( "Qty" ) ;

				AlertDialog.Builder alert = new AlertDialog.Builder(this);
				alert.setTitle("Edit Quantity");
				
				final EditText input = new EditText(this);
				alert.setView(input);
				input.setText(qty);
				alert.setPositiveButton("OK", new DialogInterface.OnClickListener() {
				public void onClick(DialogInterface dialog, int whichButton) {
				  String value = input.getText().toString();
				  listModel.get(info.position - 1 ).put( "Qty", value); 
				  tableAdapter.notifyDataSetChanged();
				  Toast.makeText(getBaseContext(), "Edit 1 " + value , Toast.LENGTH_SHORT).show();
				  }
				});

				alert.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
				  public void onClick(DialogInterface dialog, int whichButton) {
				    // Canceled.
				  }
				});

				alert.show();
				return true;
				
			case R.id.delete_menu:
				listModel.remove(tableAdapter.getItem(info.position - 1));
				tableAdapter.notifyDataSetChanged();
				Toast.makeText(this, "Delete " + info.id + "  " + info.position, Toast.LENGTH_SHORT).show();
				return true;
		}
		return true;
	}
ASKER CERTIFIED SOLUTION
alexey_gusev

Our community of experts have been thoroughly vetted for their expertise and industry experience.

Join our community to see this answer!
Unlock 1 Answer and 16 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 16 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros