I'm new to android and my scenario is as follows:
I have a list view that is bound to an array list of type Department (a simple object with id and name properties). My goal is to have the list view display the department names (done by overriding the toString() method of the Department class) and when I click on a list view item, I want to pass the ID property of the selected item to another Activity. So, how do I get the ID property on item select?
You should use this:
yourListView.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View arg1,
int position, long arg3) {
}
});
Here in onItemClick you will get the position of the selected item and you can get your id like this:
yourArrayList.get(position).getDepartmentId();
You can use Get Tag / Set Tag in android for this purpose.
Following example will help :
setTag() and getTag() on ListView
You can override onItemClick method and get the position of your view
public abstract void onItemClick (AdapterView<?> parent, View view, int position, long id)
Callback method to be invoked when an item in this AdapterView has been clicked.
Implementers can call getItemAtPosition(position) if they need to access the data associated with the selected item.
Parameters
parent The AdapterView where the click happened.
view The view within the AdapterView that was clicked (this will be a
view provided by the adapter)
position The position of the view in the adapter.
id The row id of the item that was clicked.
Related
How can I attribute an event to a specified element ( position 0 for example ) of a listView ? I've tried this but it doesn't work :
ListView liste ;
liste.setOnItemClickListener( itemClickedListener );
Any help please ?
Your code will only work if you initialize a new onCItemClickListener. Try this:
liste.setOnItemClickListener( new OnItemClickListener() {
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
//do somthing
});
}
You need to have an adapter that backs the list, then set an OnItemClickListener object for the List. When the user clicks an individual entry in the list, the system calls OnItemClickListener.onItemClick, passing in the listed arguments:
AdapterView<> arg0 is the ListView
View arg1 is the entry in the ListView
int arg2 (position) is the index of the data in the backing adapter, relative to 0
long arg3 (rowId) is the row ID of the item that was clicked (not always useful).
To attribute the click to a specified element, look at position. This will give you a pointer to the data underlying the View that was clicked. This pointer is usually what you're looking for. For example, if your ListView is backed by a Cursor from a database or content provider, position will give you a pointer to the row in the Cursor.
If I have a listview being populated with webservices and the elements added in each row have a unique id stored in the array list which utilizes a hashmap to store data,thn what would be the simplest way to obtain the id of the data,that was clicked in the row..
I am using a base adapter on my list,
Any help would be greatly appreciated.
set on item click listener on listview. it will return position of the clicked item in the adapter (equivalent to position in the array list). you can fetch the id using that.
lv.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(),String.valueOf(position), Toast.LENGTH_SHORT).show();
}
});
Third parameter is Position of Item in the list.
use this method listView.getItemAtPosition(position). It got a position you can use
listView.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> a, View v, int position, long id) {
// Get item at position like this:
// listView.getItemAtPosition(position));
}
});
if you use the adapter.getItem(position) function you'll be able to get the item stored in the ListView in that specific position.
update: this is of-course in the setOnItemClickListener
this example: http://saigeethamn.blogspot.com/2010/04/custom-listview-android-developer.html .There are 3 textview ids at custom_row_view.xml. There are 3 row data inside one position listitem when using onListItemClick. how to extract these data? how to use the id? Anyway to get row view data in the list view when the list item is clicked
[protected void onListItemClick(ListView l, View v, int position, long id]?
The parameter View v itself is the row view. You can get the attached data by calling v.getTag(). Should set it earlier in getView of adapter using v.setTag(Object)
Depends on the kind of data. For example id does contain the _id from a database table row that was set with one of the CursorAdapters. Usually this is the PK of that database table row.
listView.getAdapter().getItemAt(position)
gets you the object bound to the view V
I would like to recommend you not to get data from the view, instead use the ArrayList you have used to set data to the Adapter of the ListView.
In the example which you have pointed out, you are using an ArrayList of HashMap. So for an example..
listView.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
// arrayList is the variable which you have used as a list in your SimpleAdapter
hashMap = arrayList.get((int)id); // you need to typecast 'id' from long to int
value = hashMap.get(KEY);
}
});
I have a ListView with a CheckBox.
What I am trying to implement is an onItemClick (in my activity) that check/select my CheckBox if I click on the row, as you can see below:
#Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
MyItem row = ListofRows.get(position);
row.setSelected(true);
myAdapter.notifyDataSetChanged();
}
When I click in the row, the ListView is refreshed because of the method notifyDataSetChanged()
THE PROBLEM IS: this method refreshes the whole ListView and as a result, the ScroolBar goes to the beginning (the user is driven to the header of the list) even if the user is at the bottom of the list.
WHAT I NEED IS: Refresh just this single clicked row so the user won't be driven to the beginning of the list.
Are there any solutions to this problem?
use the selected index
View v = getListView.getChildAt(index);
Now update the view with the new values.
i have two spinners.
If in the first one the Item "Diesel" is selected i want to display the second one.
sKraftstoffArt.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
if(sKraftstoffArt.getSelectedItem().toString() == "Diesel"){
sPartikelfilterArt.setVisibility(sPartikelfilterArt.VISIBLE);
}
}
public void onNothingSelected(AdapterView<?> adapterView) {
return;
}
});
I've implemented this code in the onCreate method. When i select a item during the runtime i'm not getting the selected item text... It works only if the activity gets created and the default value gets selected....
Where else do i have to implement it?
Regards,
float
Unless your sKraftstoffArt object is not a final one, the check against it's selected item text inside an anonymous class won't work.
The adapterView among the parameters is your ListView instance to which you've assigned the AdapterView.OnItemClickListener.
The view parameter is the actual item (renderer) inside your ListView that has been clicked. This item is provided by your adapter's getView(int position, View convertView, ViewGroup parent) method.
Also, you should use the equals method of String to check whether two String values are equal.
So this won't work:
if(sKraftstoffArt.getSelectedItem().toString() == "Diesel")
Use insetad
if (adapterView.getSelectedItem().toString().equals("Diesel"))
You might also want to add an else clause after this if, to hide the sPartikelfilterArt spinner when the selected item in the previous spinner is not "Diesel".
Please note, that every time you assign a new adapter to this list (which probably you don't, i still mention it just in case...), you should add the AdapterView.OnItemClickListener to it again.