I am using merge adapter to merge two custom cursor adapters in android.I could not find clicked section to get data from my custom cursor adapter.How can I get adapter object when listview clicked?.
I tried following way in my onitemclick of listview. But it does not print "clicked on mention question section" text.But it returns android.content.ContentResolver$CursorWrapperInner#41b9a638.How can I find which section is clicked in listview?
if (parent.getAdapter().getItem(position) instanceof FeedMentionQuestionAdapter) {
LivLog.info(getClass(), "clicked on mention question section ");
}
How can I get adapter object when listview clicked?
You need to hold onto your own adapter, or call getAdapter() on your ListView.
But it returns android.content.ContentResolver$CursorWrapperInner#41b9a638
Presumably, you put a CursorAdapter in the MergeAdapter.
Also, getItem() will never return an adapter. It returns items. If you are trying to determine the adapter that handles a particular position, call getAdapter().
How can I find which section is clicked in listview?
If you are referring to SectionIndexer, then sections are not clicked.
Related
i want to find the position of the view which is the part of the list-view's custom XML file. How can find the position of the views. Can i automatically perform onItemClick?
I face this difficulty. In my case when i click on textview which is part of list-view's custom XML file then the position is right, but when i click on the button which is the part of list-view's custom XML file then it always gives first position.
After i have to decided to perform automatic onItemClick and store all the values of clicked position into another declared variable which is below.
See the image i used kotlin language instead of java. I stored all the map values into another variable which is declared above with public access specifier.
[Now when i perform operation on Button click i use
lvShayari.performItemClick(v, lvShayari.getPositionForView(v), lvShayari.selectedItemId)
this statement in which lvShayari is listview and performItemClick() method perform the onItemClick automatically when we clicked on button. Here v is the view, lvShayari.getPositionForView(v) method gives the position of v in my case it is button.]2
check out below code , hope it will help you:
listView.onItemClickListener = AdapterView.OnItemClickListener { adapterView, view, position, id ->
Toast.makeText(this, "Click on: " + position, Toast.LENGTH_SHORT).show()
}
for more Details follow this link
#Suvidha Malaviya
Make your own custom adapter for your listview with one interface in which there is one method with three parameters namely View, Position, Model class of your list which you are passing in your adapter.
After making this interface initialize that interface in your adapter and set that interface method in your view click event.
Now set this interface in your activity or fragment where you are using your custom adapter and get the value which you want...
For example, see below link in which there is explain how to use custom listener in list view adapter. Here you have to just make your own click listener in which you have to pass three parameters which I have mention above.
Try it. I hope you will resolve your issue...
I have created a custom list view whose parent class is Base Adapter. Now i need to delete its menu item .i searched for it and most of the tutorial says to use "remove" method and will be done.But actually in all tutorials they extend their class from Array-adapter.Now problem in my case is i didn't find the remove method in Base Adapter class to use it . so how i can remove my list view item in this case ?.
You won't find a remove in a BaseAdapter, because ArrayAdapter is already an Array so..
What you can do is remove the item from the ArrayList<> and then set it again for the adapter, and after that call notifyDataSetChanged(),
have a setData(ArrayList) in your adapter so you don't have to instantiate a new one. i.e.
setData(ArrayList list) {
this.list = list;
notifyDataSetChanged();
}
hope this helps
is there a way to add data from cursor to viewpager, I have data in my cursor for example 4 items and I need to place each item on a page how can I do this using cursor and viewpager is it possible?
thanks.
Yes it is possible, you need a PagerAdapter, it will bind your PagerView with data and generate every "item" (every fragment or page) that shows up on the PagerView.
So first override a PagerAdapter, for example:
public class MyPagerAdapter extends PagerAdapter
Then you have to override the method that generates every item on the PagerAdapter, the instantiateItem() method. This method gives you an integer argument named position, it corresponds to the item number on the list of items that will show on your PagerView. Imagine a book, if every page is an item, the page number is the position.
So according to this position, every item will be different in a way that you must explain to the adapter through your code. To do this, you can use this position to access your list of data and create a correspondence between the list and the page.
Lets say you have a table Person with people names and you retrieve all entries, you create a List that holds all the names data from that cursor. You can now access your list using the item position, for example, list.get(position) and by retrieving the data from that particular list item you can create the individual page that you must return on instantiateItem. You can, for example inflate a layout, which contains a TextView called personName, then do something like personName.setText(list_name.get(position)). Now return the LayoutView. The Adapter will then create a view for every item on your List.
So you can do something like this:
ArrayList<String> names_list = new ArrayList<String>();
then store the cursor information on that List (You can do more than one list, and you can do it directly with a cursor using a CursorAdapter, this is just an example) and then instantiate your adapter:
MyPagerAdapter adapter = new MyPagerAdapter(context, layout, names_list);
For every item on the list, the adapter will run the instantiateItem method and create an item to populate a ViewPager, so now all you have to do is bind the Adapter with your ViewPager:
ViewPager pager = (ViewPager)findViewById(R.id.activity_search_viewpager);
pager.setAdapter(adapter);
This is just an example to kinda explain the idea of how a PagerAdapter works, there is a bit more to this subject than just this. Anyway, once you get confortable with the way things work, you can also check the CursorAdapter, so you can instantiate items directly from cursor data, no need for a list.
Here are some SDK references you should check:
http://developer.android.com/reference/android/support/v4/view/PagerAdapter.html
http://developer.android.com/reference/android/support/v4/widget/CursorAdapter.html
Good luck :)
)
I found a tutorial about using the ListView in android. But I have a question about it. Here the tutorial:
http://www.vogella.com/articles/AndroidListView/article.html#listadvanced_interactive
You have to scroll to section 13.2.
The idea of the tutorial is on one hand you have a ListView (with checkboxes in each item), on the other hand you have an ArrayList (the items of the ArrayList are objects, which contain the information which are displayed in the items of the ListView, e.g. CheckBox checked or not, text etc.). The adapter schould keep both things equal. If you change the ArrayList, the ListView will be changed, too.
But now my question. If the user touch on one item of the List, the adapter will call the method "onCheckedChanged". But what happen there? An object will create there and get a tag from the the CheckBox. Ok. Now the method is done. The garbage collector will destroy the object or doesn't? What is when I need this infomation from there in my Activity. Imagine I have a button "Delete" under my list. So I have to transfer these information from the listener of the adapter to my Activity. How I can ensure that I use the same ArrayList with the right information in every class?
#Override
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
Model element = (Model) viewHolder.checkbox
.getTag();
element.setSelected(buttonView.isChecked());
}
I hope you know what I mean. Can you explain it to me please?
Sorry for the language, but english isn't my mother tongue.
Bye
If I understand your question, the response is in the Chapter 14.1 of the tutorial you gaved...
If you want to do some treatment when delete is checked you add a listener in the ListView.
These functions normally have the position clicked and you can do whatever you want knowing the position clicked. For exemple:
list.setOnItemLongClickListener(new OnItemLongClickListener() {
#Override
public boolean onItemLongClick(AdapterView<?> parent, View view,
int position, long id) {
Toast.makeText(MyList.this,
"Item in position " + position + " clicked",
Toast.LENGTH_LONG).show();
// Return true to consume the click event. In this case the
// onListItemClick listener is not called anymore.
return true;
}
});
I'm not totally sure I understand you're question. There's a lot to unpack in there but I will give it a shot. To understand what's going on you need to understand the relationship between the ListView and the data that backs it which is contained in the ListAdapter.
The ListView displays the data contained in the ListAdapter. The ListAdapter gets its data from some other source, in this case its the ArrayList that you're referring to. Why use an ArrayList? In the tutorial its not entirely clear because the example uses a fixed set of data (e.g. a list of operating systems) that are written out as strings. In practice, however, often times you are using a dynamic list, like a list of users, places, whatever.
The ArrayList holds the objects that will eventually be displayed. This is the "models" class in the tutorial. The same applies for any other class of data, like a list of places. If you imagine a place as a class, that class would contain fields that describe the place (e.g. address, a description, some other unique features). As data is downloaded from some location, a new instance of place would be created for each location. Those objects would be collected in an ArrayList. When the downaloding process was finished, you would then begin the process of passing the contents of that ArrayList to the ListAdapter so that the ListView could eventually be updated.
How do you know you will always use the right ArrayList? Because your ArrayList is linked to you ListAdapter. The data the the ListAdapter processes, whether a list of models or users, is supplied by the ArrayList. You cannot use the wrong one. The same goes for the ListAdapter. Because you must set the adapter that the ListView is linked it it will always pull from the correct source. You cannot use the wrong data.
What's happening with the onCheckChanged listener, if you pay close attention, is that the user is really just updating a field contained in the model class. Each modle has an isChecked field. When the check box is clicked, that value for the object is updated. This is how you know what is checked and what is not.
Response to: "Nobody told the adapter "Hey it belongs to the list, which is your source"
Look at the documentation for ArrayAdapter
and for ListView
The constructors for the ArrayAdapter all take some list of object as an argument. This list would be the list of object that will be displayed within the ListView.
Additionally the ListView requires that you call setAdapter which takes some ListAdapter as its argument. This is what actually tells the ListView the source of the data it is going to display.
In that sense, the List, ListAdapter, and ListView are all linked together.
I'm building a simple App with a ListView in it. In the Java code, I'm trying to figure out which item(s) is currently selected. From the ListView documentation at http://developer.android.com/reference/android/widget/ListView.html I see there's a method in the ListView class called
public void setSelection (int position)
I looked for a similarly named method named getSelection(), but there is none.
So, first, how do I determine which item in my ListView is currently selected? I already have the object for the ListView, so I just need the method to tells me which item the user clicked on.
ListView myLV = (ListView) findViewByID(R.id.myListView);
Also, can I define a "callback" function which is called whenever an item is selected?
Finally, how can I use the documentation to figure this out? This is a very basic question which I should be able to figure out on my own. I did what seems to be the right thing: I looked at the Official Developer pages for the ListView class, but it didn't provide the answer. How can I answer these (basic) questions for myself?
Try this
mylv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> myAdapter, View v, int position, long lng) {
String selectedFromList =(String) (mylv.getItemAtPosition(position));
}
});
I looked for a similarly named method named getSelection(), but there is none. So, first, how do I determine which item in my ListView is currently selected?
The super class AdapterView has getSelectedItem(), along with getSelectedItemId() and others. AbsListView has getSelectedView() too. But these only tend to work when you have used setChoiceMode()
The OnItemClickListener lets you know which it is clicked as it is clicked.
Finally, how can I use the documentation to figure this out?
You were right to start in ListView's documentation, but go to the "Inherited Methods" section and check the super classes as well.
You can get the position by using onitem click listener.by using on item click listener you can get position of selected item in the list.And you can put this position in Toast