How to get another item then click on item in gridview? - android

I want to know how to get different item from selected item in gridview. I have gridview game of matching pictures. When i click on one image then that will flip and then when click on second image first will flip back. How to perform this animation? I want to get imageview of first selected image.

You can do that by saving the last view in a variable
View lastView = null;
// .....
gv.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View view,
int position, long arg3) {
if(lastView != null) {
// do something with the last view
}
// the rest of your code
lastView = view;
}
});
Not tested but should work.

Related

Get id / position of ListView row

I have an Android ListView with a bunch of rows. Each row contains an image and some text. If I click on the image of a row, I want to perform action A, if I click on any other place in the row, I want to perform action B. Both of these actions need the id of the ListView item to work.
So far, for action B, I simply used listview's onItemClick, where I got the id from the parameter. However, as far as I know, the only way to find out on which view in the row itself the user clicked, is to add an onClick event handler to the ImageView that triggers action A. In that case, I only get the View that was clicked, though - not the id of the row that the ImageView is in.
My question is - how do I go from a View object to getting the id of the parent row within the ListView?
The onItemClickListener method has an onClick interface already with the view and position passed into it: You can get check which view has been clicked by doing a switch statement and performing a dedicated action when the specific view has been clicked.
E.g.
listView.setOnItemClickListener(new android.widget.AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view,int position, long id) {
String item = listView.getItemAtPosition(position);
Toast.makeText(this,"You selected : " + item,Toast.LENGTH_SHORT).show();
}
});
as shown in How to create listview onItemclicklistener
You'll see that in the onItemClick interface it has the View view as a parameter and int position as a parameter, perform the action inside the listener using both of those parameters. Like i mentioned before you can use a switch statement.
listView.setOnItemClickListener(new android.widget.AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view,int position, long id) {
String item = listView.getItemAtPosition(position);
switch(view.getId()) {
case R.id.imageView:
//perform action for imageView using the data from Item (if you have used an MVC pattern)
break;
// you can do this for any widgets you wish to listen for click actions
}
}
});
Update: All credit for this goes to wwfloyd in How to know which view inside a specific ListView item that was clicked
#Override
public View getView(final int position, View convertView, final ViewGroup parent) {
// Check if an existing view is being reused, otherwise inflate the view
if (convertView == null) {
convertView = LayoutInflater.from(getContext()).inflate(R.layout.one_line, parent, false);
}
// This chunk added to get textview click to register in Fragment's onItemClick()
// Had to make position and parent 'final' in method definition
convertView.findViewById(R.id.someName).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
((ListView) parent).performItemClick(v, position, 0);
}
});
// do stuff...
}
in your adapter apply a click listener to the element and then trigger the interface method, so that you can identify the registered widget that has been clicked and then in your onItemClick do:
public void onItemClick(AdapterView adapterView, View view, int position, long id) {
long viewId = view.getId();
switch (viewId) {
case R.id.someName:
//widget has been clicked
break;
default:
//row has been clicked
break;
}
}
if you aren't using tag, then you can
int itemId=...;
OnClickListener customOnClick=...;
imageView.setTag(itemId);
imageView.setOnClickListener(customOnClick);
and inside your custom OnClickListener
int itemId = (Integer) view.getTag();
note that you can set "any" Object as tag, so you may even set your whole custom object, not only id
#Override
public void onItemClick(AdapterView<?> arg0, View arg1,
int position, long id)
in this method View arg1 gives you Item view and you can find the item content by this arg1.findViewById(R.id.content);. Here position gives you the item position of list view
You can set click listeners for each view in a row from the getView() method in the adapter.
You can impliment it in getView() method of adapter of listview.

ListView setting background color to an item ERROR

Well, I have a horizontal LinearLayout, with items of a custom class.
What I want: Change background color of the item i've clicked (like if it's selected). If user clicks other item, the first one changes again to original color and the new one changes to "selected color"
What is my problem: This works fine when all items of the listView fit in the screen ( in this case 3 items). If there are more items, let's say 7, if user clicks item number 0, it changes color, but if user scrolls to the last item and clicks item number 6, it changes color, but item 0 doesn't change. If both items are visible, it works fine.
this is the code of my onItemClickListener.
listView.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(it.sephiroth.android.library.widget.AdapterView<?> parent, View view, int position, long id) {
for(int i=0;i<listView.getCount();i++){
try{
getViewByPosition(i, listView).setBackgroundResource(R.drawable.skin);
listView.getChildAt(i).setBackgroundResource(R.drawable.skin);
}
catch(Exception e){
System.out.println(e.toString());
}
}
view.setBackgroundResource(R.drawable.skin_selected);
}
});
why not use the adapter for the modification?I presume you are using a custom adapter?
in You Adapter
public int selectedItem;
.
//Rest of the stuff
.
.
#Override
public View getView(final int position, View convertView, final ViewGroup parent) {
if (null == convertView) {
LayoutInflater inflater = (LayoutInflater) _context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.list_item, parent, false);
}
if(selectedItem==position){
convertView.setBackgroundResource(R.drawable.skin_selected);
}else{
convertView.setBackgroundResource(R.drawable.skin);
}
return convertView;
}
and your onclick listener becomes :
listView.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(it.sephiroth.android.library.widget.AdapterView<?> parent, View view, int position, long id) {
((NameOfYouAdpater)listView.getAdapter()).selectedItem=position;
((NameOfYouAdpater)listView.getAdapter()).notifyDataSetChanged();
}
});
And I noticed you are using a custom list view in itself.Maybe look into the implementation of the library to check for interference?
EDIT:the cause of your issue is most probably dude to view recycling.The background colour does not get updated because everytime the list item is outside your ViewPort,the view is recycled.

Show dialog inside gridview

I've implemented a gridview of images that are picked from sdcard.Now i want every image to be shown in dialog on click.How to do it within Image Adapter?Any help will be appreciated.Thanks in advance.
You wouldn't do that in your adapter... Do it inside your click listener (from wherever you call and set the gridview adapter).
gridview.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
ImageView iv = (ImageView) v.findViewById(R.id.imageviewid); // get the resource id
Drawable image = iv.getDrawable(); // get the image
CreateYourDialog(image); // pass the image to a method to create your dialog
}
});
If your GridView is only the imageview for each cell, then you can skip the findViewById line of code and simply use Drawable image = v.getDrawable();.

Android gallery scrolling issue (custom images)

I am new to android. I am using a custom gallery to show images and check boxes over images. Both have their own click listeners in adapter class getView() method. When I scroll the gallery, it doesn't scroll from images and check boxes. Instead it scrolls from free spaces between the image thumbs.
i have solved this issue in my scenario i have an image and a check box i have to implement click listener on image and check box i use "checkedChangedlistener" instead of Click listener on check box in get view method of adapter class
public View getView(int position, View convertView, ViewGroup parent)
{
holder = new ViewHolder();
convertView = mInflater.inflate(R.layout.icon, null);
holder.checkbox = (CheckBox) convertView.findViewById(R.id.gCheckBox);
holder.checkbox.setOnCheckedChangeListener(new OnCheckedChangeListener()
{
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
{
if(isChecked)
{
}
else if(!isChecked)
{
}
}
});
}
and after this i need Click listener for my image thumb so i use on item click listener of gallery in my activity.
gallery.setOnItemClickListener(new OnItemClickListener()
{
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,long arg3)
{
Intent i = new Intent(Conditions.this, TestExampleGUIActivity.class);
startActivity(i);
}
});
when item click event fires i perform my action which i want to perform on my image thumb. its working fine and gallery behavior is also fine
i am sorry! i am new to this forum so i have many issues to explain my problems i will improve this in future thanks to all of you

Android: How to extract data from GridView?

Hi all
I have a GridView of FrameLayout made of an image and textview. I want to get the corresponding image and the text when I click a cell of Grid. Mentioned that textview is top of image.
Does anybody have idea how to do it?
First register item click listener of grid view
e.g. gridview.setOnItemClickListener(gridItemClickListener);
Then
OnItemClickListener gridItemClickListener = new OnItemClickListner()
{
public void onItemClick(AdapterView<?> parent, View view, int position, long id)
{
// here you will get your clicked cell of grid and you can find the image and text.
ImageView image = view.findViewById(R.id.grid_raw_image);
TextView textView = view.findViewById(R.id.grid_raw_text);
}
};
If you use the viewHolder technique as recommended by google, use:
gridView.setOnItemClickListener(new OnItemClickListner()
{
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id)
{
ViewHolder holder=(ViewHolder)view.getTag();
//use the views you hold in the viewHolder
...
}
});

Categories

Resources