I am stuck on this! I have a listView populated by a a cursor. My question is how can i get the specific item information from the cursor when a certain position in the listview is selected. For example, i select the third index in the listView, I want the information associated with that index from the cursor. How can I do that? Thanks in advance
Implement the onListItemClicked() method like so:
#Override
protected void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
yourCursor.moveToPosition(position);
yourRowId = yourCursor.getLong(0);
}
Your Activity must extend ListActivity for this to work as expected, like so:
public class YourActivity extends ListActivity {
You are creating listview from cursor.So i am assuming that your list item 0 comes from Cursor's first record and so on.So if you click list item at position 3 you can move to cursor corresponding position by cursor.moveToPosition(3).Then can work accordingly
Related
I have a activity displaying call logs in a ListView. The adapter used here to populate listview extends CursorAdapter. Listview is set to onItemClickListener(..). My Question is whenever an item is clicked how does cursor get the data? how does cursor know which position is clicked and need to get data from clicked position only? I have provided a code snippnet.
public class CallLog extends Activity
{
ListView mListView;
Cursor cursor;
//other variables
public void OnCreate()
{
setContentView(R.layout.calllog);
//SQLiteDatabaseInstance db
cursor = db.query(...parameters to get all call logs...);
mListView.setOnItemClickListener(this);
}
public void OnItemClick( // all arguments... )
{
//there is a column name 'NAME' in call log table in database
String name = cursor.getString(cursor.getColumnIndex(Database.NAME))
//here name is obtained of the clicked item.
}
Cursor is a result set. how does the cursor know which item is clicked? What can be the methods implicitly called by cursor that gives it position of clicked item?
If there are any links of similar question then pls provide.
I hope I am able to make you understand the question. Thank you
Try this:
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//move cursor to clicked row
cursor.moveToPosition(position);
}
Specifically it is NOT the Cursor that knows who clicked on what. This is actually handled by the Adapter. The adapter is used to group elements together and allow abstraction as such that they can be handled in a uniform way.
Any form of list, always has an adapter, and this is exactly why the adapter works so well. If you look at a Custom Listview with a Custom Adapter, you'll see exactly how this is done.
Example:
http://android.vexedlogic.com/2011/04/02/android-lists-listactivity-and-listview-ii-%E2%80%93-custom-adapter-and-list-item-view/
You should use cursor.moveToposition(position) inside function to get to the position of that clicked item. After that you apply this and when you will click on any item, the cursor will be set on that item and then you can use that particular item for your operation.
mListView..setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0,
View view, int position, long id) {
// here position gives the which item is clicked..
}
});
Additionally check this link for ListView ListView and ListActivity
It may help you..
In my project I use a custom adapter to define the items of a ListView. So I want to define special behavior of item in ListView, which will be depend on the value of the field in adapter. Like this :
protected void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
}
So, here, in this method I want to get access to the element of adapter to get the value from it, number of which is defined by the int position. So, what is code to do this?
you can simply cast it if you're confident enough it is always your adapter that is used:
protected void onListItemClick(ListView l, View v, int position, long id) {
((MyAdapter) l.getAdapter()).myMethod(position);
}
If you are defining an custom adapter you can save a reference to the adapter in the activity that registers the onClick behaviour. You now can call getItem on the Adapter, if you have implemented the getItem method properly.
You can get selected item(object) from list. Lets say your custom adapter containing Restaurent object per row, and your listView name foodJntListView, then
Restaurent rest= (Restaurent) foodJntListView.getSelectedItem();
will give you the selected item(object).
I use a CustomCursorAdapter to populate a ListView. Also the onitemclicklistener works
for reading text from the clicked ListItem.
I only display partial data from the cursor used..
How can i access the corresponding item in the database , when a listitem is clicked
so that i can display all the data in a toast or dialog box.
What is your Object about, what do you want to do on list item click?
Posting the details will help us to answer but here is a simple code to show a tost:
protected void onListItemClick(ListView l, View v, int position, long thisID)
{
super.onListItemClick(l, v, position, thisID);
// Get the item that was clicked
String keyword = (String)this.getListAdapter().getItem(position);
Toast.makeText(Activity.this, keyword, Toast.LENGHT_SHORT).show();
}
I'm trying to access to the data that is present in my Cursor on a ListView; however the parameter onSetItemClickListener long id is always 0.
How can I do that, without using the position (because my Cursor doesn't represent the database directly; it is a query).
reading that you are working with a database I suppose that you are filling the ListView with a custom Adapter you created with what you want to show, and a it's corresponding XML to represent each custom row with the data you are bringing back.
I faced something similar wanting to return the Ids of clicked element, what I did was in the XML of the row add an invisible TextView.
<TextView
android:layout_width="fill_parent"
android:layout_height="0dip"
android:id="#+id/post_Id"
android:visibility="gone"
/>
and then I just access that value on the click event doing the following:
protected void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
int id = Integer.valueOf(((TextView)v.findViewById(R.id.post_Id)).getText().toString());
// THE REST OF THE CODE GOES HERE
}
Hope this helps!!
I'm developing an Android application.
I have several objects loaded on a ListActivity. I want to know the item clicked on event onListItemClick.
I see method onListItemClick has a parameter called id. How can I setup this id?
My objects are identified by an ID, how can I add this ID to listItem?
Thanks.
if SectionObj is your object that you want to access later, set that in the adapter when you set the source.
ArrayList<SectionObj> li
ArrayAdapter<SectionObj> adapter=new ArrayAdapter<SectionObj>(getApplicationContext(),android.R.layout.simple_list_item_1, li);
setListAdapter(adapter);
then in ur listener method..
protected void onListItemClick(ListView l, View v, int position, long id) {
SectionObj o=(SectionObj)getListView().getItemAtPosition(position);
}
What is the source of your list data? If you are using a cursor - then the id passed in onListItemClick(ListView l, View v, int position, long id) will automatically be the id of the cursor row.
Use the following;
listView.getItemAtPosition(position);
Where listView is the name of your list view.
You can set the id in your ArrayAdapter view.
Checkout the following pages then you should find the solution.
http://developer.android.com/reference/android/app/ListActivity.html#onListItemClick(android.widget.ListView,%20android.view.View,%20int,%20long)
http://sudarmuthu.com/blog/using-arrayadapter-and-listview-in-android-applications