I have a ListView that gets filled from a cursor (using rawQuery) that I need to get the text of the selected item from on click.
protected void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
Intent mViewChaptersIntent = new Intent(this, ViewWeb.class);
String s = ((TextView) l.getItemAtPosition(position)).getText().toString(); // Tried this, didn't work.
mViewChaptersIntent.putExtra("extension", s);
mViewChaptersIntent.putExtra("itmClicked", String.format("%d", id));
startActivity(mViewChaptersIntent);
}
But I'm not sure about the right way to do it. The getItemAtPosition that I've seen in other posts doesn't seem to work...
getItemAtPosition() should return you a Cursor that is positioned at the specified row. You will then need to call getString() to retrieve whatever column you are looking for.
Related
I've overridden onListItemClick in ListFragment to get the selected item.
The function works great the first time I open the fragment. But after I navigate to some different activities and return via the back button or finish(), the position reported is wrong. Do I need to do something to reset this fragment after it's returned to?
onListItemClick:
#Override
public void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
Brewery brewery = ((BreweryAdapter)getListAdapter()).getItem(position);
if (mCallbacks != null) {
mCallbacks.onBrewerySelected(brewery);
}
}
My BreweryAdapter is very simple- it just populates the view pieces with their data.
Any ideas? My code matches the examples I've looked at online. Thanks!
I'm having somewhat of an issue in an application I'm developing.
So, all my tables have _id fields for primary keys, and I use SimpleCursorAdapter to bind them to ListViews and Spinners.
What I wanted to know is how can I make the ListView or Spinner selected item have the same ID as the corresponding row?
The strange thing is that this works with the ContextMenu, which I am using straight of the NotePad example:
AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();
info.id
This ID field IS the same as the RowID on the table, and I can delete items fine, but when I try something like this:
getListView().setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> adapter, View view,
int position, long arg) {
Intent i = new Intent(getApplicationContext(),
FuelingList.class);
long id = view.getId();
The ID field is some random rubberish.
So my question is, in the first code bit, what Id is the AdapterContextMenuInfo getting and how can I retrieve it in other parts of my code?
Since you are using a SimpleCursorAdapter, the onItemClick is passing the database row id in to you..
public void onItemClick(AdapterView<?> adapter, View view, int position, long arg)
The long arg part is actually your row id from the database.
So your code should be:
getListView().setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> adapter, View view,
int position, long arg) {
Intent i = new Intent(getApplicationContext(),
FuelingList.class);
long id = arg;
Since you know you are using a SimpleCursorAdapter, then in your onItemClick method, you can call
public void onItemClick(AdapterView<?> adapter, View view, int position, long arg) {
String value = ((SimpleCursorAdapter) adapter.getAdapter()).getCursor().getString(COLUMN_INDEX);
}
Where COLUMN_INDEX in the column that you want to fetch from the currently select row in the cursor.
Alternately, calling
adapter.getAdapter().getItem(position)
and then casting that to a Cursor, works as well.
NOTE 1: The AdapterView is actually your ListView.
NOTE 2: Since you using a SimpleCursorAdapter then getItem(position) on the Adapter returns the Cursor positioned at the row your specifed
NOTE 3: When you have Cursor, you can fetch data by providing the column index (0 based) or by using cursor.getString(cursor.getColumnIndex("COLUMN_NAME"))
NOTE 4: As per Barak's comment the arg parameter is the value from the _ID field when using a CursorAdapter, if all you need to know is the row _ID, then just use the arg value.
Can you store the ids of rows in an arraylist and take the id of the row from corresponding position of arraylist. This worked for me..
protected void onListItemClick(ListView l, View v, int position, long id) {
// TODO Auto-generated method stub
super.onListItemClick(l, v, position, id);
Intent intent=new Intent(this , Details.class);
Bundle extras = new Bundle();
extras.putLong("ID", IDList.get(position));
intent.putExtras(extras);
startActivityForResult(intent, SHOW_DETAILS);
}
You can use listview.setonitenclicklistener... Hope it helps
In the following code, on click of list item I want to fetch variable present inside the object, How I can achive this
Below is code snippet
private void onListViewItemClick() {
// TODO Auto-generated method stub
// item click switch to next activity
listCustomListViewId.setOnItemClickListener(new OnItemClickListener() {
/* on click gets list view item id */
public void onItemClick(AdapterView<?> myAdapter, View myView,
int myItemInt, long mylng) {
// fetching clicked item id
Object o = listCustomListViewId.getItemAtPosition(myItemInt);
Log.i("Victory Item Id:.....", String.valueOf(o);
long strid = (long) (listCustomListViewId
.getItemIdAtPosition((int) mylng));
Log.i("Item Id...#######", String.valueOf(strid));
/* switch on next 'ListItemDeleteUpdateActivity' activity */
Intent intent = new Intent(FeedsActivity.this,
VictoryDetailActivity.class);
intent.putExtra("customElements", o.toString());
startActivity(intent);
}
});
}
I have tried to get data into Object but unable to fetch.
#Override
protected void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
// Get the data associated with selected item
Object item = l.getItemAtPosition(position);
String myitem = item.toString();
edittxt.setText("Selected item is :"+ myitem); // You can Set EditText from Here.
/* switch on next 'VictoryDetailActivity' activity */
Intent intent = new Intent(FeedsActivity.this,VictoryDetailActivity.class);
intent.putExtra("customElements",myitem);
startActivity(intent);
}
Hope this helps you.
On DataSource you have probably an collection of something. myItemInt represent the corresponding item in your collection to the item clicked. Use that.
If you have some views inside the clicked view you need to get, use findViewbyId on myView, like this:
myView.findViewbyId(R.id.myEditTextControl) //demo, use yours
If this answer is not enough for you, post some code from your adapter and tell us more details about what you need to do.
To handle events when the items of ListView is selected. You must override onListItemClick() method. The medthod have 4 parameters:
#Override
protected void onListItemClickonListItemClick(ListView l, View v, int position, long id)
To get information about ItemSelected, you just call getItemAtPosition(position) method to return a object which contains data.
It should be onListItemClick()
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 can add to a db and list as a listview. When I click a list item using onListItemClick, what statement do I need to get the value?
String[] from = new String[] {NotesDbAdapter.KEY_BODY, NotesDbAdapter.KEY_TITLE, NotesDbAdapter.KEY_NUMBER};
int[] to = new int[] {R.id.toptext, R.id.middletext, R.id.circle};
SimpleCursorAdapter notes = new SimpleCursorAdapter(this, R.layout.row, notesCursor, from, to);
setListAdapter(notes);
//some other code
protected void onListItemClick(ListView l, View v, int position, long thisID)
{
super.onListItemClick(l, v, position, thisID);
**String **resposponseid** = Activity2.getData();**
}
I have responseid for every row unique which I want to use when I click on list position. So how to get responseid when I click on list?
In general, it should something like this. You will need to tailor it to suit your needs.
#Override
protected void onListItemClick(ListView l, View v, int position, long thisID)
{
super.onListItemClick(l, v, position, thisID);
// Get the item that was clicked
Object o = this.getListAdapter().getItem(position);
String keyword = o.toString();
}
I would suggest you go through a tutorial to learn, especially if you are new to Android and Java. Here's one - Android ListView and ListActivity - Tutorial.
In the onListItemClick method, the parameter int position is exactly what you're looking for. The list is filled by an ArrayAdapter which utilizes some List (let's say an ArrayList) to get Views for each row.
The fact that the View in each row can be pretty complicated (not consist of a single value), leads to the way it is implemented, and you don't get the value itself, but the position in the list. What you need to do to get the value is to query that ArrayList (its get(int index) method) you used to fill the ListView with the position param.