Why is onListItemClick reporting the wrong position? - android

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!

Related

How to get data from object in Android

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()

How to get access to the fields of custom adapter in ListView in Android?

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).

how to get listview id from touchevent

Can anyone tell me how I can find the relevant list item id for a touch event associated with a listview?
I want to set the background of a list item to a different color when the user presses down on it, and change it back when they release. I prefer to do this with code and not the background setting.
I already have an onclicklistener, but want to get this ontouchlistener working as well.
I know this is an old question. But if anyone else had this same question the solution is the pointToPosition method found in classes that derive AbsListView.
public boolean onTouch(View arg0, MotionEvent arg1) {
// ... other logic ...
int lListViewPosition = mYourListView.pointToPosition((int)arg1.getX(), (int)arg1.getY());
// get the item id
int lItemId = mYourListView.getItemIdAtPosition(lListViewPosition);
// or get the actual item
Object lYourItemObj = mYourListView.getItemAtPosition(lListViewPosition);
}
#Override
protected void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
// Get the item that was clicked
Object o = this.getListAdapter().getItem(position);
//do Magic
}
Hope this helps!

Capturing checkbox event in a listview

I am using a list view containing check boxes. Could you please help me how to identify which check box has the user selected? Basically I want different functions to be performed when a checkbox is clicked or a list item is selected similar to Android alarm clock.
does this help you:
how to display a list of checkboxes using dynamic data
That is how I did it with dynamic data.
Basically, in your listView class override:
#override
protected void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
boolean isClicked = l.isItemChecked(position);
// do something
}

Getting text from a Listview

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.

Categories

Resources