ListView editing data issue

zolf
zolf used Ask the Experts™
on
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;
	}

Open in new window

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
what is "listModel" ? I'm not sure you update your adapter

Author

Commented:
private ArrayList<HashMap<String, String>> listModel;
I meant if it related somehow to tableAdapter and it's items?
Learn Ruby Fundamentals

This course will introduce you to Ruby, as well as teach you about classes, methods, variables, data structures, loops, enumerable methods, and finishing touches.

Author

Commented:
yes.

tableAdapter = new ListViewAdapter(this, listModel);
visibleTable.setAdapter(tableAdapter);

Open in new window


please help.i dont understand what i am missing.my delete menu is working perfect

Author

Commented:
any help!!
how do you send your new data?

and could you quickly check after editing and "tableAdapter.notifyDataSetChanged();" call - what's in the adapter's items? ie tableAdapter.getItem(info.position - 1) etc?

Author

Commented:
after tableAdapter.notifyDataSetChanged();

i run this

System.out.println("AFTER refresh "+tableAdapter.getItem(info.position - 1).toString()) ;

and it prints out the new entered qty i.e. 54

 AFTER refresh {Qty=54, Name=Sales, Sr.No.=1}

Author

Commented:
i send the data to the db like this

@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
			case R.id.send_menu:
				System.out.println("The content of arraylist is: " + listModel);
				getConnection().saveOrder(orderList);
				Toast.makeText(this, "Order sent", Toast.LENGTH_LONG).show();
				break;
		}
		return true;
	}

Open in new window


@Override
	public void saveOrder(List<Order> orders)
	{
		// TODO Auto-generated method stub
		System.out.println("INSIDE SAVEORDER");
		List<NameValuePair> params = new ArrayList<NameValuePair>(orders.size());
		
		params.add(new BasicNameValuePair("action", "save_order"));
		
		for(int i=0;i<orders.size();i++)
		{
			Order order = orders.get( i ) ; 
			
			params.add(new BasicNameValuePair("amount_"+i, Integer.toString(order.getQty())));
			params.add(new BasicNameValuePair("prod_id_"+i, Integer.toString(order.getProductID())));
		}
		
		
		try {
			String response = executeHttpPost(params);
			String[] result = response.split(DELIMITER);
			for(int i = 0; i<result.length;i++)
			{
				System.out.println(result[i]);
			}
			if (result.length == 2 && "100".equals(result[0])) {
				
				Log.d("---", "Order Sent to Server : " + result.length);
				Log.d("---", "Order Sent to Server : " + result[0]);
				Log.d("---", "Order Sent to Server : " + result[1]);
		
			}
			else {
				Log.d(" ---- ", "Incorrect login or password.");
				sessionCookie = null;
			}
		}
		catch (Exception exc) {
			Log.e("----", exc.getMessage(), exc);
			throw new ConnectionException(exc.getMessage());
		}

		
	}

Open in new window

Author

Commented:
tell m esomething how can i only type digit in the arlert dialog which i have shown in my previous code
- firstly: I see this line

getConnection().saveOrder(orderList);

and I ask myself - what is orderList and is it updated?

- re. input - you can set inputType to be numbers only, just open your layout in Eclipse and you'll see it immediately => android:inputType="number". Alternatively, you could set the same from the code - just read the docs

Author

Commented:
I pass the list data to the db.i dont see anything wrong in here
well, if it doesn't work then surely there is something wrong somewhere?

> I pass the list data to the db
and "list data" being what? besides, what do you see in orderList?

Author

Commented:
You see in the list view i see the updated values,but when i send the data to save in the db it saves the old value not the updated value.it seems the updated data sits on top of the old data in the view but old data data is sent to the db
we're moving in circles :)

so, in one piece of code you have tableAdapter & listMode, in the other piece of code where you're saying you send the order you have

case R.id.send_menu:
				
                                System.out.println("The content of arraylist is: " + listModel);
				getConnection().saveOrder(orderList);
				Toast.makeText(this, "Order sent", Toast.LENGTH_LONG).show();
				break;

Open in new window


So what I'm trying to ask you is this: listModel model is probably updated, but what do you have in orderList ? Could you print it as well to see if you have all the data in there? Maybe I'm missing some relations in your data - for me it seems like you don't update orderList.

Author

Commented:
you were correct the orderList contained the old value.when i send the data to the db.any help what i can do to update this value

Author

Commented:
thanks mate!!

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial