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

AndroidJavaSmartphone Programming

Avatar of undefined
Last Comment
Zolf

8/22/2022 - Mon
alexey_gusev

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

ASKER
private ArrayList<HashMap<String, String>> listModel;
alexey_gusev

I meant if it related somehow to tableAdapter and it's items?
I started with Experts Exchange in 2004 and it's been a mainstay of my professional computing life since. It helped me launch a career as a programmer / Oracle data analyst
William Peck
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
Zolf

ASKER
any help!!
alexey_gusev

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?
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
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}
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

Zolf

ASKER
tell m esomething how can i only type digit in the arlert dialog which i have shown in my previous code
Your help has saved me hundreds of hours of internet surfing.
fblack61
alexey_gusev

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

ASKER
I pass the list data to the db.i dont see anything wrong in here
alexey_gusev

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?
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
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
alexey_gusev

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
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
Zolf

ASKER
thanks mate!!
Experts Exchange is like having an extremely knowledgeable team sitting and waiting for your call. Couldn't do my job half as well as I do without it!
James Murphy