I have a multiple choice list view and for its adapter I have designed a layout file. I have an image view in the layout and when it's clicked, I want to know what child of the list it is. I mean the child id for the its parent which is the list to further use the method list.getChildAt(???). can anyone tell me how to get that?
The image is independent of the list view and for its onClick attribute I've written a different method that changes image view resource... How can I know which child that a particular imageView is when I click on it?
In the XML layout I have this:
<ImageView
android:id="#+id/choice_image"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="deleteSelected" />
And here is a part of deleteSelected method:
public void deleteSelected(View view) {
icon = (ImageView)view.findViewById(R.id.choice_image);
list.getChildAt(???); \\ To know which child the view is
}
I have set the adapter as follows:
list.setAdapter(new ArrayAdapter<String>(this, R.layout.list_choice_multiple,
R.id.choice_text, terms) {
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View v = super.getView(position, convertView, parent);
icon = (ImageView)v.findViewById(R.id.choice_image);
icon.setImageResource(R.drawable.delete_off);
icon.setTag(R.drawable.delete_off);
return v;
}
});
How can I set the id for each image view so I can access it by getID() method within deleteSelected()?
ListView has a getFirstVisiblePosition method, using that you can calculate the child position based on its position in the list view (which is a parameter passed in to the onItemClicked method of the listener).
int childIndex = listView.getFirstVisiblePosition() - position;
If you're clicking on a child of the row view (View used in the ListView, created by the adapter), then you need to know which position the row belongs too - simplest way is to store the position in the tag of the child when you set the onClickListener
Simple answer: You are writing that the click happens on the ImageView. The ImageView is not associated with the ListView.
So there is no relationship between your click or any element in the ListView.
When clicking on the ImageView, though, the onClick receives a reference to the ListView itself. So what you are trying to achieve? Please be more precise.
Basically first you will have to set an onClickListener for your list-item's ImageView in getView() of the Adaptor.
Now with onClickListener you get the view that is clicked as function parameter to onClick(). Set a different background for this view (ImageView) object.
Hope this is clear. Let me know.
Related
I'm Noob at Java Android.
I searched more about my question and I found that I can use
listView.getFirstVisiblePosition();
To get my first visible position. How can I implement a new Background Resource only to my first visible position?
I tried to do that by far.
View get = lv.getFirstVisiblePosition()
get.setBackgroundResource(R.drawable.newconversation);
Thanks.
There is a method in your adapter which controls your list items views
#Override
public View getView(int position, View convertView, ViewGroup parent) {
You can just use this condition :
if(position == 0)
//set your background
After convertView has been inflated
getFirstVisiblePosition() returns an Integer position. You want to get the view at that position.
Try
View view = list.getChildAt(list.getFirstVisiblePosition());
Then set your background on the view.
In my android app my Main Activity extends List Activity. I store there elements called Items, their layout is defined by itemLayout xml file and I use custom adapter (called ItemAdapter) to transfer data to List View in Main Activity. In itemLayout there's an ImageView and my aim is to change its image when user clicks on the particular item in list view. In my opinion the easiest way to achieve that is to get access to particular item's (the one that was clicked) layout, there find the ImageView and call method setImageBitmap. How can I "find" this layout of clicked item? I tried many things in method:
#Override
protected void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
}
but nothing worked. Do I have to add anything to my ItemAdapter? Or is it possible to do in onListItemClick(…) but I can't find out how?
You're thinking about it in slightly the wrong way. Adapter's are a way to map data to views. So if you want to change how a particular view looks for a given position, you need to change it's correlating data so that the rendered View then changes. Attempting to modify a view directly kinda goes against how adapters are meant to be used.
So in your case, when a user clicks on an item. Find that item in your adapter via the position number. Then update the item and ensure notifydataset is called. Meanwhile, your adapter's getView() will then handle displaying the appropriate image.
As I understood you need to modify clicked item layout, right? Use argument from onListItemClicked:
v.findViewById(<your_img_view_id>)
For better performance use view holder.
onListItemClick is fired when you press on an element of the ListView. If you want to retrieve the element in the dataset, you can simply invoke
l.getItemAtPosition(position)
the returned value has to be casted to the specific object
Yes It is possible in your custom adapter class in the getView() mtheod u can change imageview bitmap by clicking on it
see this code
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
View rowView = convertView;
rowView = inflater.inflate(R.layout.playlist_item, null);
final ImageView im = (ImageView) rowView.findViewById(R.id.favorite);
im.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// Do your stuff here
}
});
return rowView;
}
I have a ListView items/cell that has a few layers, relative, linearLayout etc.
I tried doing v.getParent() inside the onClick of the button but it just went one layer up. How can I get a reference to the root of the current element inside onClick event handler?
Here is my getView()
Button v = (Button)findViewById(R.id.myButton);
v.setOnClickListener(new OnClickListener(){
onClick(View v) {
//Right here I want to find another view inside the cell.
View otherView = ???.findViewById(R.id.myOtherViewInListItem);
otherView.setBackgroundColor(...);
.........................................
}
});
I'm imagining that this button is inside the ListView element. Based on this assumption:
create only one OnClickListener. The way you're doing you're creating adapter.getCount() OnClick listener and waste memory like crazy.
Maybe you want to completely remove the button and use the OnItemClick to track clicks on your whole ListView element, this will pass to you the reference to the whole View.
If you really must have the button, inside your getView you store a reference to the parent, something like that:
#Override
public View getView(int position, View convertView, ViewGroup parent) {
// do whatever you're doing to get the view, let's call it v
Button b = (Button)v.findViewById(/* your button id */ )
b.setTag(v)
}
then on your click listener you can:
onClic(View v){
View parent = (View)b.getTag();
}
and if that answer works for you (I know it does) accept it as correct like Daniel Martinus pointed out.
Inside onClick(View v)
just call v.getViewParent(); then you will have a view from which you can call the find for the others.
(I have already seen this similar question)
I have a ListView for which I've written a custom adapter, and an onitemclicklistener. I'm having an issue where, when any element of the list is selected, getView is called (twice) for each of the top 4 elements of the ListView, even if those elements are not visible. This happens even if I don't call notifyDataSetChanged on the adapter - those first 4 views are fetched twice regardless. Is this normal behavior? My issue is not as much that it's being called twice for them, but that it is being called at all when updating them is not needed.
By the way, I am not using wrap_content for the height or width of the listview - the height is match_parent and the width is a fixed number of dp.
The onItemClick() method of the OnItemClickListener is here:
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
mPicture = pictures[position];
mPicturesAdapter.setCurrentPicture(mPicture);
mPicturesAdapter.notifyDataSetChanged();
}
getView() from my custom Adapter (which extends BaseAdapter) is here:
public View getView(int position, View convertView, ViewGroup parent) {
Log.v("tag", "Getting view for position "+position);
LayoutInflater inflater = LayoutInflater.from(mContext);
LinearLayout layout = (LinearLayout)
inflater.inflate(R.layout.picture_thumbnail, parent, false);
// set up the linearlayout here ...
return layout;
}
On any item click, getView() is called for positions 0 - 3 twice regardless of which item was clicked.
Just by modifying the adapter via
mPicturesAdapter.setCurrentPicture(mPicture);
the ListView already tries to update itself. I'm guessing the onClick method will still do it's job without you calling notifyDataSetChanged
Actually whatever List/Group you are using to populate the ListView, you need to first empty it and then recall it. For example, if you use ListA to populate the ListView, in the second or any consecutive update you need to empty the ListA first and then add items and then populate using it.
if (convertView != null){
Then populate list
}
I am developing an application in which I have to show ratings depending on the values that I am receiving after parsing the XML response in listview. I have implemented it using the Custom Adapter and showing the images in the getView() method like :
String rating = Constants.menuRatingList.get(position);
if (rating.equals("1")) {
rateImg1.setImageResource(R.drawable.stary);
}
The problem is when I scroll the down to the last item and again move upwards, it is redrawing the list row.
Someone please suggest me approach to stop the redrawing the list row and set the image value permanently.
How do you create the rateImg1 variable?
Maybe you should get it from the view that is passed to the getView() method?
Something like this:
public View getView(int position, View view, ViewGroup parent) {
if (view == null) {
view = LayoutInflater.from(ctx).inflate(R.layout.my_layout, parent, false);
}
((ImageView) view.findViewById(R.id.my_image)).setImageResource(R.drawable.some_drawable);
return view;
}
Also you can implement getViewTypeCount() and getItemViewType() and check them in the above method not to redraw the same image if it is already set.