Link to home
Start Free TrialLog in
Avatar of Zolf
ZolfFlag for United Arab Emirates

asked on

ListView editing data issue

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

Avatar of alexey_gusev
alexey_gusev
Flag of United Kingdom of Great Britain and Northern Ireland image

what is "listModel" ? I'm not sure you update your adapter
Avatar of Zolf

ASKER

private ArrayList<HashMap<String, String>> listModel;
I meant if it related somehow to tableAdapter and it's items?
Avatar of Zolf

ASKER

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
Avatar of Zolf

ASKER

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?
Avatar of Zolf

ASKER

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}
Avatar of Zolf

ASKER

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

Avatar of Zolf

ASKER

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
Avatar of Zolf

ASKER

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?
Avatar of Zolf

ASKER

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
ASKER CERTIFIED SOLUTION
Avatar of alexey_gusev
alexey_gusev
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Zolf

ASKER

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
Avatar of Zolf

ASKER

thanks mate!!