android get position of clicked item in gridview - android

as we know using android grid view, we can do the following and get notified when item is clicked:
gridview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
Toast.makeText(PopularCapitActivity.this, "" + position, Toast.LENGTH_SHORT).show();
}
});
we also know that, if the cell in the grid contains a clickable item, say a button, the above won't get fired.
so currently i have a grid view, each cell has its own button, so now when user clicks on the button, it will have its own action based on the cell the button resides in, my question is, how can i access the cell position in the button handler?
thanks

Assuming you are using a custom adapter for the GridVIew, in the getView method you can simply add a tag to the Button object that contains the position passed into getView:
button.setTag(new Integer(position));
Then, in the onClickListener method, with the view that is passed in (the button) you can do:
Integer position = (Integer)view.getTag();
And then handle the position value from there.
EDIT:
It appears the best practice would be to do:
button.setTag(Integer.valueOf(position));
rather than using the Integer constructor.

gridView.setOnItemClickListener(new AdapterView.OnItemClickListener()
{
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id)
{
String a = String.valueOf(position);
Toast.makeText(getApplicationContext(), a, Toast.LENGTH_SHORT).show();
}
});

Related

Highlighting the list item of a list view in android for a specif time

In my android application i'm having a list view and some items in that,now my requirement is when I select a particular item from that list the list item should be highlighted with a red color, and that selection should not be disappear because in my application i should choose an item from the list view and i should click a button(submit).
So in order to know to the user that he has selected an item from the list view it should be highlighted until he clicks submit button.
I have used choice Mode attribute and set the value to single choice and i have changed the color for highlighting everything works fine but i need it not to be disappeared until user clicks the submit button
The variable val consists of some names which i retrieved from database.
Please help me to solve this Thanks in advance.
ArrayAdapter<String> adptr= new ArrayAdapter<String>(this,R.layout.row,R.id.member_name,array);
lv.setAdapter(adptr);
lv.setOnItemClickListener(new OnItemClickListener(){
#Override
public void onItemClick(AdapterView<?> parent, View v, int pos,
long id) {
// TODO Auto-generated method stub
val=lv.getItemAtPosition(pos).toString();
//Toast.makeText(getApplicationContext(), val, 5000).show();
}
});
use OnItemClickListener
ListView lview = getListView();
int pos =0;
lview.setOnItemClickListener(new OnItemClickListener()
{
#Override
public void onItemClick(AdapterView<?> adapter, View v, int position,
long arg3)
{
// set the background of v here
if (position == pos)
// reset the background color
// and pos = position;
else
get the previous listitem view and reset it's background to original
}
});
Actually I had the same problem sometime back. I was working on non touch andorid ui application for google tv.
The solution is like this.
1) create a custom ArrayAdapter (extending ArrayAdapter)
2) onItemClick get the position of the item.
3) send that position to your adapter by some public method say setCurrentPosition(int pos)
4) in getView() of the adapter check for the position and set the background to red for that view.
Hope this will work as it worked for me.

Get value of item on OnItemClick Listview

I try to get the value of a selected Item within a custom adapter on a listview. I try this with following code:
public void onItemClick(AdapterView<?> parent, View v,
int position, long id) {
View curr = parent.getChildAt((int) id);
TextView c = (TextView)curr.findViewById(R.id.tvPopUpItem);
String playerChanged = c.getText().toString();
Toast.makeText(Settings.this,playerChanged, Toast.LENGTH_SHORT).show();
}
At the beginning, if I click, the values are good, but once I scrolled and I click on another Item, I get the wrong value of that clicked item... Any idea what is causing this?
The parameter v is the current row. so use:
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
TextView c = (TextView) v.findViewById(R.id.tvPopUpItem);
String playerChanged = c.getText().toString();
Toast.makeText(Settings.this,playerChanged, Toast.LENGTH_SHORT).show();
}
(Or you could use getChildAt(position) but this would be slower.)
Understand you might be able to simplify this more depending on your layout.
Just Small Change is Required
TextView c = (TextView) v.findViewById(R.id.tvPopUpItem);
Since you are using custom view you need to pass the View argument in your OnItemClickListener then you need to use that value to get the Details of TextViews present in that
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Intent i=new Intent(ListOfAstrologers.this,AstroProForUser.class);
i.putExtra("hello",adapter.getItem(position).getUsername() );
startActivity(i);
}
});
Here username is a string field declared in a class and you must over ride getItem() method in adapter class to get the details of inflated row when you click.
I dont have much experience but I think if you want to pass a variable from an OnClick (which is an anonimous class) to the outside of the onClick and to the other activity, you want to pass it via Intent intent.putExtra... (it's quite easy)
Otherwise you might end up using "public static" variable, which might be a memory leak...

Getting list row id in list view

If I have a listview being populated with webservices and the elements added in each row have a unique id stored in the array list which utilizes a hashmap to store data,thn what would be the simplest way to obtain the id of the data,that was clicked in the row..
I am using a base adapter on my list,
Any help would be greatly appreciated.
set on item click listener on listview. it will return position of the clicked item in the adapter (equivalent to position in the array list). you can fetch the id using that.
lv.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(),String.valueOf(position), Toast.LENGTH_SHORT).show();
}
});
Third parameter is Position of Item in the list.
use this method listView.getItemAtPosition(position). It got a position you can use
listView.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> a, View v, int position, long id) {
// Get item at position like this:
// listView.getItemAtPosition(position));
}
});
if you use the adapter.getItem(position) function you'll be able to get the item stored in the ListView in that specific position.
update: this is of-course in the setOnItemClickListener

Nothing happens when clicking on image GridView

I have this code to launch the desired activity when an image from a gridview is clicked:
gridview.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
switch (v.getId()) {
case R.drawable.stock:
Intent i = new Intent(acceuil.this,carburant.class);
startActivity(i);
}
}
});
Unfortunately, when i tap on the image nothing happens. What is the problem of this code ? Thank you very much.
The treatement must be done with manipulating the position of the element and not the name of the image, here is the correct code:
gridview.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
switch (**position**) {
case **0**:
Intent i = new Intent(acceuil.this,carburant.class);
startActivity(i);
}
}
});
Yes, since the view ID and the drawable ID are not the same this won't work.
If you've implemented GridView like they show in the example here then you should be able to use the position parameter in onItemClick() to retrieve the drawable id from the array and base the switch block on that. Of course you need to make that value final in order to use switch, but that's not directly relevant.

How to get a particular row in listview to perform its own operation?

I am new to android programming. I need some help here. I have used this site example on creating a listview. What I want to achieve is when the user clicks a particular row, the row clicked will perform its respective action. (Eg. When clicked row 1 will show a toast. When clicked row 2 will direct the user to another new view, etc.)
I have set a OnItemClickListener to the listview but am lost on how to do it. Any help will be appreciated. Thanks!
Below is my code:
.......
final ListView list = new ListView(this);
list.setOnItemClickListener(new OnItemClickListener(){
#Override
public void onItemClick(Adapter<?> arg0, View v, int i, long l){
// At implementation
}
});
.......
It looks like you're on the right track, everything should be clearer with onItemClick's parameter names :
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
Toast.makeText(getApplicationContext(), "Clicked row " + position,
Toast.LENGTH_SHORT).show();
}
So you actually get the position of the item clicked, and can have a different action for each item.
To get the index of the selected item:
int selected_item = myListView.getPositionForView(view);
or to get the string:
String chosen_item = (myListView.getItemAtPosition(selected_item).toString());
If your class is extending ListActivity replace "myListView" with "this".

Categories

Resources