Nothing happens when clicking on image GridView - android

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.

Related

onTouch is registered right after LongTouch

I have a ListView that when I hold (long touch) on an item, the item is removed from the list.
It's registering a touch right after, while the long touch is still happening.
Here is my code -
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
Intent intent = new Intent(getApplicationContext(), MapsActivity.class);
intent.putExtra("placeNumber", i);
intent.putExtra("Type", "Fav");
startActivity(intent);
}
});
listView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
#Override
public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
Log.i("Removing", "" + favouriteLocations.get(position));
favouriteLocations.remove(position);
Favourites.arrayAdapter.notifyDataSetChanged();
return false;
}
});
After letting go of long touch a 'phantom' touch appears on the item where the long touch had been (a new item has moved up in the list) and the activity loads as if it has been touched.
It worked fine at one point, I'm sure I didn't change the code. Any ideas?
Thanks.
you need to return true from the long click listner, you must have this in the xml too android:longClickable="true" but list view is long clickable by default so no need to update xml at all.

ListView example program which switches to new activity

Could anyone possibly give me an entire example for demonstrating a ListView.
Each item in listview needs to fire a new activity.
For ex, if I use 'Subjects' as content of ListView then after pressing particular a subject from listview , a new screen with its title and some content of that subject gets displayed.
I know this is too much to ask, but i have been looking for ListView Ex. since quite long and nothing has helped me so far. PLease Help
Thank you
yourlistview.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
}
});
then use bundle to send some content to your next activity.
then getActionBar.setTitle(sometext); this text is come from your bundle.
This contains:
http://www.androidbegin.com/tutorial/android-search-listview-using-filter/
you can refer following link for listview demo code.
http://www.androidhive.info/2011/10/android-listview-tutorial/
and for opening new activity on selecting any item, you can use,
listCoupon.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
// TODO Auto-generated method stub
// here you can compare selected item by its position and then start a new activity
// arg2 is selected item position in your arraylist
}
});

Show/Hide button in listview android,

Can anyone knows how can i make something like this in ListView ?
When a item in ListView is Clicked the imageButton get Visible.
http://img850.imageshack.us/img850/2377/is4t.png
Then you click another item and the previus item that you clicked get invisible , and the actual item get the image button visible.
http://img14.imageshack.us/img14/1920/lnpy.png
I have searched about this , but i haven't found something to work exactly i want. And i have tried to make the button visible onItemClick for that view , but , when i click another button , the previus item still visible. I have tried to do notifyDataBaseChanged but still there. Thanks for help , and sorry for my bad english.
¿How can i get access on the previous Item View to set button to View.GONE ?
Code :
listView.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View view, int position,
long arg3) {
// TODO Auto-generated method stub
MPlayer.playSong(position);
Button bPlaying = (Buttton)view.findViewById(R.id.button1);
bPlaying.setVisibility(View.VISIBLE);
ca.notifyDataSetChanged();
}
});
Not the best solution but hope this might help you solving you the problem
private Button previousButton = null;
listView.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View view, int position, long arg3) {
MPlayer.playSong(position);
if(previousButton != null){
previousButton.setVisibility(View.GONE);
}
Button bPlaying = (Buttton)view.findViewById(R.id.button1);
bPlaying.setVisibility(View.VISIBLE);
previousButton = bPlaying;
ca.notifyDataSetChanged();
}
});

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

android get position of clicked item in gridview

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();
}
});

Categories

Resources