Android GridView - update View based on position - android

I have a GridView, using a custom adapter (myAdapter extends BaseAdapter), where each item in the grid holds an ImageButton. getView() is working fine.
However, from elsewhere in my code, how can I update the image (setImageResource) for one particular item in the grid based on its position in the GridView?
So far, I've added this to my adapter class:
public void changeImage() {
Log.d(TAG, "Image change");
this.ImageButton.setImageResource(R.drawable.newImage);
}
And would like to write something like this: mygridview.getItemIdAtPosition(20).changeImage(); (doesn't compile).

If u want to update the image for one particular item in the grid, what u can do is...have an onClickListener for all the images in the grid.
Now when u will click on any of the image, get the position of the image and change ur background resource for that particular image.
Example:
While adding the image: imageView.setOnClickListener(imageListener);
And the listener will be something like dis:
private OnClickListener imageListener = new OnClickListener() {
public void onClick(View v) {
int id = v.getId();
ImageView imgView = imageArray.get(id);
imgView .setBackgroundResource(R.drawable.newImage);
};
I hope u find it helpful.

In case of Images in a GridView I did this and it works also:
gridview.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View imgView, int position, long id) {
ImageView imageView = (ImageView) imgView;
imageView.setImageResource(R.drawable.newImage);
}
}); }

That's also my last problem. Here my solution I use data Model and adapter for my RecyclerView
Firstly, register your new data to your model
DataModel detail = new DataModel(id, name, sat, image);
after that, use set to replace old value with the new one
mData.set(index, detail);
finally, refresh your adapter
if(adapter!=null)
adapter.notifyItemChanged(index);

Related

Android: Display images in full screen mode using Picasso

Hi i have some images displayed in gridView using Picasso, when i try to open each image (after onItemClickListener) in full screen mode (inside new acivity) it display only the first image in my gridView whatever the image i click, this is my problem.
This is my code where i display the images from the url inside gridView and OnItemClickListener.
Picasso.with(ToolDescription.this)
.load(SaveSettings.ServerURL +"Images/"+ imagepath)
.into(imageView);
ls.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Intent intent = new Intent(ToolDescription.this, ImageFull.class);
intent.putExtra("imagepath", SaveSettings.ServerURL +"Images/"+ imagepath);
startActivity(intent);
}
});
return myView;
This is my second activity (named ImageFull) where i want to display the clicked image:
ImageView imageView = (ImageView) findViewById(R.id.fullImage);
String imagepath = getIntent().getStringExtra("imagepath");
Picasso.with(ImageFull.this)
.load(imagepath)
.noFade()
.resize(800, 800)
.centerCrop()
.into(imageView);
Either set an individual click listener to each imageview or use the int position to pull the correct imagepath from your gridview item array.
Grid View Android Doc
This link has a easy to follow code for exactly what you're trying to do.. Use the position to find which element you are clicking on an get the respective image url
Looks like what's happening is that you're calling ls.setOnItemClickListener() every time getView() returns.
AdapterView.OnItemClickListener is a single listener for the entire AdapterView, and you're intended to differentiate which item was clicked on by using its position argument.
I'm not sure where imagepath is coming from inside getView(), but I assume it's something like String imagepath = getItem(position).getImagePath(). If that is the case, you can fix your problem by:
1) Move ls.setOnItemClickListener() out of getView()... probably to your onCreate() or whever you set up your GridView.
2) Change
intent.putExtra("imagepath", SaveSettings.ServerURL +"Images/"+ imagepath);
to
String currentImagePath = getItem(position).getImagePath();
intent.putExtra("imagepath", SaveSettings.ServerURL +"Images/"+ currentImagePath);
Probably there will be syntax errors because I can't see the rest of your code, but hopefully this is enough for you to be able to work through them.

Android ListView How to get item id without clicking on the item

My ListView item consists of the following components - ImageView and then under it there are two more ImageViews - like and dislike.
So when I click on either like or dislike I want to be able to get corresponding ListView item id.
The only way that I am able to do it at the moment is if I first click on the ListView item and then on the like ImageView.
My question is - is there any way to get corresponding ListView item id without having to first click on the ListView item and only then on the like ImageView?
Here is my code
cursorAdapter = new PostCursorAdapter(this, null, 0);
ListView list = (ListView)findViewById(android.R.id.list);
list.setAdapter(cursorAdapter);
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, final long l) {
ivLike = (ImageView)findViewById(R.id.ivLike);
ivLike.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Log.d("Like","Like button for the post with id=" + l + " has been clicked");
}
});
}
});
I was able to solve it myself. What I needed to do was to add setOnClickListener to my like and dislike ImageViews not in MainActivity but in PostCursorAdapter. Thanks everyone!

Android: setImageResource in adapter changing image 3 positions later

I am using AndroidSwipeableCardStack to do a tinder like interface with a 5 star rating system. When I click on a button that is meant to change the image. The image changes three cards later not in the current card.
This is the project I used: https://github.com/wenchaojiang/AndroidSwipeableCardStack
I assume it is to do with it pre-loading the next card as a recyclerview normally does but I am really not sure.
This my adapter below, you can see at the bottom I call setimageresource on the one star rating image button onclicklistner. I set a Log.d and the button click is registering immediately upon click but the change in the image resource appears only after 3 cards have been swiped.
Edit: So I noticed that regardless of the project I imported, my adapter is only extending ArrayAdatper. So I guessed that the problem might be in the getView method and that I might need to override something else (not sure). But the important point, I checked the position in getView by logging the position and sure enough it said position 0,1,2,3 without anything being swiped away. This is analogous to the offset of the setImageResource so I believe they are interlinked but unfortunately that doesn't bring me any closer to an answer.
Thanks for your help.
public class SongPreviewCardsDataAdapter extends ArrayAdapter<SongDatabaseMappingAdapter> {
public SongPreviewCardsDataAdapter(Context context, int resource) {
super(context, resource);
}
ImageButton oneStarRating;
#Override
public View getView(int position, final View contentView, ViewGroup parent) {
// Initialise Song Views
SongDatabaseMappingAdapter item = getItem(position);
TextView songName = (TextView) (contentView.findViewById(R.id.songNameTextView));
songName.setText(item.getSongTitle());
com.mikhaellopez.circularimageview.CircularImageView songImage = (CircularImageView) contentView.findViewById(R.id.songImageView);
String ImageURL = (item.getPictureURL());
Picasso
.with(this.getContext())
.load(ImageURL)
.into(songImage);
// Initialise Rating Buttons
oneStarRating = (ImageButton) contentView.findViewById(R.id.ratingButton1);
// Create OnClickListners for Ratings Buttons
oneStarRating.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
oneStarRating.setImageResource(R.drawable.starfull);
Log.d("Star", "Star Clicked");
}
});
return contentView;
}
}
getView can be called at any time on any item, so if you want something to stay set on a view, you have to model it.
I don't know if your SongDatabaseMappingAdapter class is something you can change or not, but I'll assume you can change it.
Add a variable to your item class, like boolean mOneStar. Getter/setter left as an exercise for the reader.
In getView() make your item final so you can refer to it in the onClick callback:
final SongDatabaseMappingAdapter item = getItem(position);
Use the variable in getView to set up your view:
oneStarRating = (ImageButton) contentView.findViewById(R.id.ratingButton1);
oneStarRating.setImageDrawable(null); // clear out recycled value
if (item.isOneStar()) {
oneStarRating.setImageResource(R.drawable.starfull);
}
In your OnClickListener, set the property on the item and call notifyDataSetChanged():
oneStarRating.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Log.d("Star", "Star Clicked");
item.setOneStar(true);
notifyDataSetChanged();
}
});
The main point is: Never change a view from an event handler. Always change the item from the event handler then call adapter.notifyDataSetChanged(), and always change your view in getView() based on the item.

Assigning a string to every icon on a gridview

I am creating an Android App, and I've run into this problem: I have a MainActivity that has a gridview layout. This layout is composed of 6 icons. I want 2 things for each icon
1.On click: start a new activity (I think I know how to do this)
2.On long click: show a brief string from /res/values/strings that is associated with that icon and activity
up to now I've managed to make placeholders for both actions like so:
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
GridView gridview = (GridView) findViewById(R.id.gridview);
gridview.setAdapter(new ImageAdapter(this));
//for each gridview item, we want one click reaction (that creates a new Activity) and
//a long click reaction, that shows an informative text on what this button does
//Simple Click Action
gridview.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
//Toast.makeText(MainActivity.this, "" + position, Toast.LENGTH_SHORT).show();
//placeholder. In future will be replaced with new activity creation
}
});
//Long Click Action
gridview.setOnItemLongClickListener(new OnItemLongClickListener(){
public boolean onItemLongClick(AdapterView<?> parent, View v, int description, long id){
Toast.makeText(MainActivity.this, "" ,Toast.LENGTH_LONG).show();
return true;
}
});
}
Now what I want to do is in the Toast.makeText function I want to print a resource string. This string is different for each icon, and so I have a feeling it should be returned through the ImageAdapter class, which creates the gridview.
In that class, among other things, I have created an array that has each string's resource ID. So my question is:
a) How do I return that resource ID so it is usable by the MainActivity?
b) If there is a different way to do this, what is that?
I'm a newbie so it is entirely possible I've made some colossal mistake
Thanks for the help in advance!
In onItemLongClick(...) use the parent parameter (which should be your GridView) and call...
ImageAdapter theAdapter = (ImageAdapter) parent.getAdapter();
In your ImageAdapter as long as you have a method to return the string resource ids, you can then use the position parameter (named description in your code) something like...
int resId = theAdapter.getId(description);
Then for the Toast use...
Toast.makeText(MainActivity.this, MainActivity.this.getString(resId),Toast.LENGTH_LONG).show();
On Click you will get the grid layout which you have set the setOnItemClickListener , Switch the view.getId() so that you have 6 case within swich and make transition on each case as you need , similarly toast within case in setOnItemLongClickListener
Thank you for your useful answers.Turns out I found a different solution,that works perfectly.
Seeing how to 'access' images in a gridview you create references to them,I created an Integer array of references like this:
private Integer[] testDescriptions = {
R.string.reflexes_description,
R.string.agility_description,
R.string.intellect_description,
R.string.strength_description,
R.string.proximity_description,
R.string.voice_description };
and implemented the gridview.setOnItemLongClickListener as follows:
gridview.setOnItemLongClickListener(new OnItemLongClickListener(){
public boolean onItemLongClick(AdapterView<?> parent, View v, int position, long id){
Toast toast = Toast.makeText(MainActivity.this, testDescriptions[position] ,Toast.LENGTH_LONG);
TextView v1 = (TextView) toast.getView().findViewById(android.R.id.message);
if( v1 != null) v1.setGravity(Gravity.CENTER);
toast.show();
return true;
}
});

Android listview toggle image by click, changes more than one image

I have a listview and on click I try to change an arrow image inside my listview adapter row.
This works fine, but I have the problem, that not only a single image changes. A few rows above and below the arrow images also change.
It changes the images in every 6th row.
the code for my onClick event is this:
list.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent,View view, int position, long id) {
if (row != null)
{
ImageView PfeilImage = (ImageView) row.findViewById(R.id.imageView1);
PfeilImage.setImageResource(R.drawable.pfeil);
}
ImageView PfeilImage2 = (ImageView) view.findViewById(R.id.imageView1);
PfeilImage2.setImageResource(R.drawable.pfeil_aktiv);
row = view;
getInfoById(position);
}
});
It would be nice if someone could help me with that problem.
thanks in advance
Frank
That's actually normal for how the ListView is setup since it is designed to not re-inflate Views, but instead re-use them; which is why you can't just change the image, you need to change the data it's pointing to.
The solution to this, inside your Adapter's getView() method, is to check the data and set the background image based on that.
You may want to create a new Class to hold your current data and the new image to use holder. ie.
Class ListViewItem
-int imageToUse
-Object whateverDataYouWereOriginallyUsing
and then set the image for that view based on ListViewItem.get(position).imageToUse
Now, when you're actually changing the data based on a user click, you'll need to call notifyDataSetChanged() on the adapter so that it will re-evaluate the UI based on the new DataSet and your getView() method
got it, many thanks.
in my Adapter Class I made a new int variable "myClickedPosition".
In my click event I set this variable and after this I set notifyDataSetInvalid to redraw the listview:
list.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> theAdapter,View view, int position, long id) {
LazyAdapter mAdapter = (LazyAdapter) theAdapter.getAdapter();
mAdapter.myClickedPosition=position;
mAdapter.notifyDataSetInvalidated();
getInfoById(position);
}
});
and in my getView function I check the clicked position:
if(myClickedPosition == position)
{
PfeilIcon.setImageResource(R.drawable.pfeil_aktiv);
} else {
PfeilIcon.setImageResource(R.drawable.pfeil);
}
Now all is working fine.

Categories

Resources