onListItemClickListener: passing the long id value - android

UPDATE
The original question i asked was about my long id value but because you guys were right in the way u said i had the correct id i removed my error. Thanks for the help. read my answer for more detail.
1) My app uses the local android SQLiteDatabase and has three tables. I have no problems for two of the tables but turns out my third one is presenting some issues because of my column declarations are public static final string COLUMN_NAME = "name"; ,etc.
My Activities are not extending the ListActivity so that I can have custom lists and listviews for each activity.
I am getting my listview by listview = (ListView) findViewById(R.id.myList); and adding a listener to the listview by listview.setOnItemClickListener(ListListener); Then here is my method for the list listener:
OnItemClickListener ListListener = new OnItemClickListener(){
public void onItemClick(AdapterView<?> arg0, View v, int position,
final long id)
{
AlertDialog.Builder dialog = new AlertDialog.Builder(ExerciseList.this)
.setIcon(R.drawable.edit)
.setTitle("Update Selected Exercise")
.setMessage("Would you like to update the current Exercise? Click continue to proceed.")
.setPositiveButton("Continue", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface arg0, int arg1) {
final Intent i = new Intent(getBaseContext(), AddExercise.class);
i.putExtra(ExerciseDbAdapter.KEY_ROW_ID, id);
startActivityForResult(i, EDIT_EXERCISE);
}
})
.setNegativeButton("Back", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
dialog.show();
}
};
This above method is just a working on list item click listener!

Intent.putExtra("Key",value) is right way to put the data in intent so
i.putExtra("INSERT THE KEY HERE",ExerciseDbAdapter.KEY_ROW_ID, id);

Okay guys so i found the issue with my application and you were all right. I was getting the correct row id from the application.
I however was passing another data member through my intent causing the setRowIdFromIntent() method to change the id from null to 0. or from not null to 0.
Basically no matter what the value i was passing it was being set to 0 from my setRowIdFromIntent() method because of the data member i passed through. Therefore the above code is almost irrelevant to my problem.
So if you want a working on click list listener the one above will definitely help you pass the correct id to your new activity. Sorry again for this confusion I had on my side. Thanks again for all other postings!

Related

setOnItemClickListener not updating with notifyDataSetChanged

I'm using notifyDataSetChanged with my custom GridViewAdapter which updates the listView however when I click on something with my updated listView, the old links are still there. I have the following ClickListener in my onCreate method.
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
Intent intent = new Intent(getBaseContext(), SpiceList.class);
intent.putExtra("INGREDIENTS_CALL", true);
intent.putExtra("INGREDIENTS_SELECTED", position + 1);
if (recipeCall) {
intent.putExtra("RECIPE_CALL", true);
}
startActivity(intent);
}
});
I've tried duplicating this inside the buttons that update the listView but this does not work. Any ideas for how I can best update my listener to reflect my changed listView?
Thanks! :)
Not sure if i understand your question but let me see if i can try. You are changing the listviews views when you click on one?
Are you deleting any element in the array or List you are using for your adapter? So you remove an item and then call notifyDataSetChange()? or you add an item to the array and call the notify method?
Turns out the problem was in the way I was retrieving my SQL queries. I was making the position of my listener equal to the ID of the SQL entry I was retrieving. Therefore, even though my list changed, item 1 was still pulling SQL items with an ID of 1.
When I designed my queries in Android, I originally did not expect that I would run into this problem. However, now I've linked my SQL queries to the actual item ID and not the listener position. My new listener is now:
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
Intent intent = new Intent(getBaseContext(), SpiceList.class);
intent.putExtra("INGREDIENTS_CALL", true);
intent.putExtra("INGREDIENTS_SELECTED", Integer.parseInt(resultsID.get(position)));
if (recipeCall) {
intent.putExtra("RECIPE_CALL", true);
}
startActivity(intent);
}
});
Thanks for your help!

Refreshing Listview's ArrayAdapter with the remove method throws UnsupportedOperationException [duplicate]

This question already has an answer here:
Unable to modify ArrayAdapter in ListView: UnsupportedOperationException
(1 answer)
Closed 9 years ago.
I want to remove data from a ListView. For that on a long press event I've used the code below:
lstGame.setOnItemLongClickListener(new OnItemLongClickListener() {
#Override
public boolean onItemLongClick(AdapterView<?> arg0, View arg1, final int arg2, long arg3) {
AlertDialog.Builder builder = new AlertDialog.Builder(FavouriteActivity.this);
builder.setMessage("Remove from Favourite?").setCancelable(false)
.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
Const.favourite(FavouriteActivity.this, (args[arg2]));
Toast.makeText(FavouriteActivity.this, "Selected Item Removed from Favourite.", Toast.LENGTH_LONG).show();
// Here I get the UnsupportedException---->
// adapter.remove(args[arg2]);
lstGame.setAdapter(adapter);
lstGame.invalidate();
}
}).setNegativeButton("No", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
});
Dialog alert = builder.create();
alert.show();
return false;
}
});
Why do I get that exception?
If the adapter reference points to a default ArrayAdapter instance then you most likely instantiate the ArrayAdapter using an array of objects as its source of data. If this is the case then, under the hood, the ArrayAdapter will transform that array into a special ArrayList(not the normal java one). This special ArrayList doesn't implement the methods that change its size(so using methods like add or remove(which modify that list) on the ArrayAdapter will throw the UnsupportedOperationException), it will only allow you to modify the values in it.
If you want to use that remove method then put the data from the array that you currently use in the ArrayAdapter in an ArrayList and then pass that list to the ArrayAdapter constructor.

Identifying the group that has been clicked in an expandableListView

I'm trying to identify a view that has been clicked in an expandableListView. When I set an OnItemLongClickListener I get an argument that shows me the position of the clicked view inside the list. However, it also counts child views. I'd like it to count only groups, so when a group was clicked I can determine which one it was. Is there a way to do that?
No, the long parameter is not the packed value, this is the ID generated by your adapter (getCombinedChildId()). Attempting to interpret an ID, even if you generate it in a certain way would be a bad idea. Id is an id.
I believe the right way is to use ExpandableListView.getExpandableListPosition(flatPos) method. Your "pos" argument passed to the listener is, in fact, flat list position. getExpandableListPosition() method returns the packed position which can be then decoded into separate group and child positions using the static methods of ExpandableListView.
I have hit this problem myself today so I am describing the solution I have found working for me.
The long id parameter passed by the onItemLongLongClick method is a packed value.
You can retrieve group position with ExpandableListView.getPackedPositionGroup(id)
Child position is obtained with ExpandableListView.getPackedPositionChild(id).
If Child == -1 then the long click was on the group item.
Below is an example listener class demonstrating unpacking of id.
private class expandableListLongClickListener implements AdapterView.OnItemLongClickListener {
public boolean onItemLongClick (AdapterView<?> p, View v, int pos, long id) {
AlertDialog.Builder builder = new AlertDialog.Builder(ctx);
builder.setTitle("Long Click Info");
String msg = "pos="+pos+" id="+id;
msg += "\ngroup=" + ExpandableListView.getPackedPositionGroup(id);
msg += "\nchild=" + ExpandableListView.getPackedPositionChild(id);
builder.setMessage(msg);
builder.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) { }
} );
AlertDialog alert = builder.create();
alert.show();
return true;
}
}

Remove an item from a ListView and from a file

So, i'm storing my datas in an IO-file! My datas are displayed and i want to delete an item from the listview, i maked this code, and i'm stucking!
L.setOnItemLongClickListener(new OnItemLongClickListener() {
#Override
public boolean onItemLongClick(AdapterView<?> arg0, View arg1,
final int arg2, long arg3) {
AlertDialog alert_reset;
AlertDialog.Builder builder = new AlertDialog.Builder(activity);
builder.setMessage("Supprimer cette donnée ?")
.setCancelable(false)
.setPositiveButton("Oui",
new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog,
int id) {
.............
updatelv(activity);
}
})
.setNegativeButton("Non",
new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog,
int id) {
dialog.cancel();
}
});
alert_reset = builder.create();
alert_reset.show();
return true;
}
Have i to use List.remove(arg2)?
And for deleting the data from a file, how can i do this ?
Thank you.
To remove an item from a ListView (which is just a display of some data) you need to remove the item from the data that backs the ListAdapter.
A common example is an Adapter that contains a list. To remove an item from the list and update the ListView you would do something like this.
myList.remove(arg2); // remove the item
myAdapter.notifyDataSetChanged(); // let the adapter know to update
IMHO the easiest way is to start by deleting the entry in the file, and then restart the "buildList" process. As the old entry is no more in the file, the new list won't show it any more.
About deleting in the file, it's more a java based question than Android, and it depends also on the store format you use (xml, json, custom ?). You should consider using a database, which is more flexible and easy to update.

Open a Custom Dialog when clicking on a listview entry

I would like to open a custom dialog when someone clicks on a listview entry. That dialog will need to know the text that was clicked on in order to display additional information on that particular entry. Can anyone point me in the right direction on how to accomplish that?
Thanks!
On the ListActivity, override the onListItemClick method. There, you will get the position of the item that was clicked. As you said you want to know the text that is on the item that was clicked, I suppose you have a simple List. In that case, I guess you have, for instance, an array with strings to populate the list.
public void onListItemClick(ListView parent, View v, int position,
long id) {
String itemText = items[position]);
}
So, in this case I'm supposing you have an array of Strings called items. The next step would be to create a Dialog, which can be done this way:
public void onListItemClick(ListView parent, View v, int position,
long id) {
String itemText = items[position]);
new AlertDialog.Builder(this)
.setTitle("Title for " + itemText)
.setMessage("Custom message for "+itemText)
.setNeutralButton("Close", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dlg, int sumthin) {
// do whatever you want to do
}
}).show();
}
By the way... if you want to receive nice answers here, make sure you provide nice questions. By "nice question" I mean something with a little bit of your code, so that we can get a better idea of how to help you ;)

Categories

Resources