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()
Related
I have generated a list of customers. On click this should open edit view to edit the customer. Here the parameter should pass the _id of the row according to stored in the database. But everytime passing it's position in the list. So the edit view is opening wrong data. Please help.
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> l, View v, int position, long id) {
Uri customerEditUri = Uri.parse(CustomerBean.CONTENT_URI + "/" + id);
customerEdit(customerEditUri);
}
});
Answer:
Thank you all. Your comments helped me to solve this. I have created following function inside my CustomerObject class:
#Override
public String toString() {
return this.name;
}
After that created an array of CustomerObject in activity like following:
List<CustomerObject> customers = new ArrayList<CustomerObject>();
Created ArrayAdapter by following:
adapter = new ArrayAdapter<CustomerObject>(this, R.layout.list, R.id.customer_name, customers);
Finally called setOnItemClickListener() like this:
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> l, View v, int position, long id) {
CustomerObject custObj = adapter.getItem(position);
Uri customerEditUri = Uri.parse(CustomerBean.CONTENT_URI + "/" + custObj.pkid);
customerEdit(customerEditUri);
}
});
You will have to set the ID you would like it to return in the adapter, the List View Adapter that you used to bind data to the ListView.
If I am not wrong, the method is in the adapter class under the following method name:
public long getItemId(int position) {
return myitem[position].getId();
}
Returning the appropriate ID will get you the results you wanted.
I believe that the "long id" is not the record id but the internally generated view id.
If you want to get back to the datasource id then you need to use position and something like:
// Assuming datasource is an ArrayAdapter<Customer>
Customer customer = customerAdapter.getItemAtPosition(position);
// then you can do
Uri customerEditUri = Uri.parse(CustomerBean.CONTENT_URI + "/" + customer.getId());
customerEdit(customerEditUri);
Replace id with position.
Use
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> l, View v, int position, long id) {
Uri customerEditUri = Uri.parse(CustomerBean.CONTENT_URI + "/" + position);
customerEdit(customerEditUri);
}
})
In my opinion, through the position, you can get row item with adapter's getItem(position).
So, the position mean the data position in the adapter.
For the id parameter, I think it is a help method, as you know, the data in adapter always is a recorder. general speaking, a recorder should have an id column(something like the database id). when coding, you can get the item through position, then get the item's id(if the item has id). but you can get such "id" directly with "id" parameter.
By the way, if you want use the id parameter, you have to implement the getItemId() method in adapter. the default implement in ArrayAdapter is just return the position.
I try to get the value of a selected Item within a custom adapter on a listview. I try this with following code:
public void onItemClick(AdapterView<?> parent, View v,
int position, long id) {
View curr = parent.getChildAt((int) id);
TextView c = (TextView)curr.findViewById(R.id.tvPopUpItem);
String playerChanged = c.getText().toString();
Toast.makeText(Settings.this,playerChanged, Toast.LENGTH_SHORT).show();
}
At the beginning, if I click, the values are good, but once I scrolled and I click on another Item, I get the wrong value of that clicked item... Any idea what is causing this?
The parameter v is the current row. so use:
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
TextView c = (TextView) v.findViewById(R.id.tvPopUpItem);
String playerChanged = c.getText().toString();
Toast.makeText(Settings.this,playerChanged, Toast.LENGTH_SHORT).show();
}
(Or you could use getChildAt(position) but this would be slower.)
Understand you might be able to simplify this more depending on your layout.
Just Small Change is Required
TextView c = (TextView) v.findViewById(R.id.tvPopUpItem);
Since you are using custom view you need to pass the View argument in your OnItemClickListener then you need to use that value to get the Details of TextViews present in that
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Intent i=new Intent(ListOfAstrologers.this,AstroProForUser.class);
i.putExtra("hello",adapter.getItem(position).getUsername() );
startActivity(i);
}
});
Here username is a string field declared in a class and you must over ride getItem() method in adapter class to get the details of inflated row when you click.
I dont have much experience but I think if you want to pass a variable from an OnClick (which is an anonimous class) to the outside of the onClick and to the other activity, you want to pass it via Intent intent.putExtra... (it's quite easy)
Otherwise you might end up using "public static" variable, which might be a memory leak...
I'm having a very simple problem, which can be solved fairly simple but for some reason I cannot figure it out why is it not working.
I have a view composed of 6 Listviews and when the user clicks on a detail position I'm returning the position.
This position will be used to get the items from an entire row in a database.
When passing the position to another activity, The detail activity, the position I get is the one which I initialized, which is 0.
Here is my code.
1st Activity
The item click listener:
detailsLi.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> aV, View v, int arg2,
long arg3) {
// TODO Auto-generated method stub
/*
* Intent openDetails = new Intent(
* "com.DCWebMakers.Vairon.APPOINTMENTDETAILS");
* startActivity(openDetails);
*/
positionView = detailsLi.getSelectedItemPosition();
Dialog showPosition = new Dialog(MyAppointment.this);
showPosition.setTitle("Position is:" + positionView);
}
});
the return position method:
public Integer getPosition() {
return positionView;
}
2nd activity
Setting the row:
AppointmentInfo details = new AppointmentInfo(this);
details.open();
String name = details.getName(new MyAppointment().getPosition());
details.close();
So, is the return position method not being used correctly?
Thanks.
new MyAppointment().getPosition()
A new instance will return your default value. You should be passing the position:
In your list item click:
Intent intent = new Intent("com.example.newactivity");
intent.putExtra("list_position", arg2);
startActivity(intent);
arg2 will give you the item position in the list..
to get the item use aV.getItemAtPosition(arg2).toString()
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!
in my Android app I have created a ListView component called myList, and filled it with objects of my own custom type:
class MyClass{
private String displayName;
private String theValue;
... //here constructor, getters, setters and toString() are implemented
}
I used the ArrayAdapter to bound the ArrayList theObjects with myList:
ArrayAdapter<MyClass> adapter=
new ArrayAdapter<MyClass>(this, R.layout.lay_item, theObjects);
myList.setAdapter(adapter);
This works fine, the list is populated and etc., but when I'm trying to access the selected item, i receive a Null object. I've done this using
myList.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> adapter, View v, int position, long id) {
MyClass selItem = (MyClass) myList.getSelectedItem(); //
String value= selItem.getTheValue(); //getter method
}
What seems to be the problem? Thank you
By default, when you click on a ListView item it doesn't change its state to "selected". So, when the event fires and you do:
myList.getSelectedItem();
The method doesn't have anything to return. What you have to do is to use the position and obtain the underlying object by doing:
myList.getItemAtPosition(position);
You are implementing the Click Handler rather than Select Handler. A List by default doesn't suppose to have selection.
What you should change, in your above example, is to
public void onItemClick(AdapterView<?> adapter, View v, int position, long id) {
MyClass item = (MyClass) adapter.getItem(position);
}
Since the onItemClickLitener() will itself provide you the index of the selected item, you can simply do a getItemAtPosition(i).toString(). The code snippet is given below :-
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
String s = listView.getItemAtPosition(i).toString();
Toast.makeText(activity.getApplicationContext(), s, Toast.LENGTH_LONG).show();
adapter.dismiss(); // If you want to close the adapter
}
});
On the method above, the i parameter actually gives you the position of the selected item.
On onItemClick :
String text = parent.getItemAtPosition(position).toString();
myList.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> adapter, View v, int position, long id) {
MyClass selItem = (MyClass) adapter.getItem(position);
}
}
Using setOnItemClickListener is the correct answer, but if you have a keyboard you can change selection even with arrows (no click is performed), so, you need to implement also setOnItemSelectedListener :
myListView.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> adapterView, View view, int position, long l) {
MyObject tmp=(MyObject) adapterView.getItemAtPosition(position);
}
#Override
public void onNothingSelected(AdapterView<?> adapterView) {
// your stuff
}
});
In touch mode, there is no focus and no selection. Your UI should use a different type of widget, such as radio buttons, for selection.
The documentation on ListView about this is terrible, just one obscure mention on setSelection.
Though I am using kotlin, the following code answered your question. This return selected item:
val item = myListView.adapter.getItem(i).toString()
The following is the whole selecteditem Listener
myListView.setOnItemClickListener(object : OnItemClickListener {
override fun onItemClick(parent: AdapterView<*>, view: View, i: Int,
id: Long) {
val item = myListView.adapter.getItem(i).toString()
}
})
The code returns the item clicked by its index i as shown in the code
MyClass selItem = (MyClass)
myList.getSelectedItem(); //
You never instantiated your class.