Hi guys this is rather a silly question because i'm confused. what i'm trying to do is to call a method(share to facebook) from an activity inside my adapter.
this is the illustration:
Activity:
postToFacebook(Pets dog);
listview that holds list of dogs.
Adapter:
adapter in which i use to create custom view for better looking.
problem is that i want to set an onLongClickListener on the LinearLayout that holds all the dog's info (this way user can hold any part of the info) to be shared to facebook.
is there a way to do this?
thanks a lot!
If I understand your question you are complety on bad way
In your activity you have an ArrayList (or List) with Dogs information
Something like
ArrayList<Pets> listDogs;
now, you should do this (myListView is your ListView obj)
myListView.setOnItemLongClickListener(new OnItemLongClickListener() {
public boolean onItemLongClick(AdapterView<?> arg0, View arg1,
int pos, long id) {
postToFacebook(listDogs.get(pos));
return true;
}
});
In this way you get the position of element selected and call your method from activity and not from adapter.
listDogs.get(pos)
this line return the element of the row selected.
Related
I have a listview itemClickListener that should get the model instance associated with the row clicked.
I read about tags in http://developer.android.com/reference/android/view/View.html#setTag(int, java.lang.Object)
I know that listview recycles rows, so it would not be a good idea to use v.setTag(currentItem), because that would result in an earlier row being associated with a later item.
So to solve my original problem, it looks like I need to use setTag(int, object) where the body of my click handler needs to know the unique key. The documentation states to use a resource id value, but that is not unique amongst multiple rows. How do I get the model instance for the row I clicked on?
You should just be able to grab the item out of your adapter like this:
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id)
{
ListAdapter a = (ListAdapter)parent.getAdapter();
Object itemAtPosition = a.getItem(position);
}
I've read lots of tutorials from my manual and on the internet that explain the getView method, but I haven't understood why they use it.
Could anyone explain it to me with some examples or snippets?
getItem() returns the item's data object. It provides a way for you to access data in the adapter. For example, your array adapter holds string elements, getItem() returns a string object.
getView() is used to construct or reuse the child item of your AdapterView.
AdapterView is a view that contains multiple items. For example, a ListView contains some items that have the same (or might not) structure. getView() is used to build the View at some position and fill it with data.
getItem() is used to get the item that provides a data for the specified View item.
For example, getItem() must return a String or CharSequence if you have a ListView of text items. This is made for convenience, for example in your onItemClickListener
#Override
public void onItemClick(AdapterView<?> av, View view, int pos,
long arg3) {
String selectedText = (String) av.getItemAtPosition(pos);
// or av.getAdapter().getItem(pos);
}
I want to know how to get the listview item value like this
item1
item2
I want to get first item value item1, like this code
if (listview part of value == item1)
{
// do something
}
Can anyone help me to solve this problem?
Your question is very sparse on details so I'll take my best guess at interpreting it (consider expanding your question).
You want to get an item from the listadapter at a given position, yes? If so...
yourListView.getAdapter().getItem(position);
See also:
http://developer.android.com/reference/android/widget/ListView.html#getAdapter()
http://developer.android.com/reference/android/widget/ListAdapter.html
http://developer.android.com/reference/android/widget/Adapter.html
I'm trying to guess your question.
You want, maybe, that when user clicks on an item execute some code.
If this is your case then you can do in this way:
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parentAdapter, View view, int position,
long id) {
// do your code here
}
}
where lv is your Listview. If you want to have more information go to my blog here
I have a list view that uses different layout sheets for different rows. Each sheet has different variables on them. So, when I want to implement the click listener for my list I need to know which type of row I am clicking on so that I can try to access to the correct values. For example:
list.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
//do this for layout A
//......
// do that for layout B
//......
}
});
How can I access to the layout information and the corresponding values ?
Make sure that you have a variable in your activity where you've kept the data with which you've populated the adapter.
Then, based on the position variable, you'll be able to get the exact row which was clicked. Then based on the row, you'll be able to figure out what type of row it is, right? :)
you can use the method:
public abstract int getItemViewType (int position) of your custom adapter.
I have an Android ListView created with a SimpleAdapter that has more items in it than fit in the screen. After the list has been scrolled, I need to get the position in the data model of the first visible item in the list.
Basically I want a function like: listView.getChildAt(0).getPositionInDataModel().
Adapter has a few functions in it, like getItemId(position) that looked useful; however, the SimpleAdapter implementation just returns the passed in position, not a row id like I'd hoped.
A brute force solution would be to get the View at index 0, and compare it to the view for each item in the adapter. However, there doesn't seem to be an easy way to get the view for a particular position from the adapter.
Anyone have any thoughts?
It's very easy. Just use ListView.getFirstVisiblePosition() + indexYouWant. For instance, to get the position in the adapter of the 2nd child displayed in the ListView, just use getFirstVisiblePosition() + 1.
No need for all the scary stuff shown in the reply above :)
listView.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener()
{
public void onItemSelected(AdapterView<?> parent,View view, int pos, long id)
{
AisleId= parent.getSelectedItemId();
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
In this we will get list item Id parent.getSelectedItemId();
Simply use the getPositionForView(View) (see documentation). The main advantage of this method is it also works with descendant Views of an item.