This code gives error on clicking Listview
listv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> av, View view, int i, long l) {
Toast.makeText(Display_List.this, "myPos "+i, Toast.LENGTH_LONG).show();
String name = (String) (listv.getItemAtPosition(i));
Toast.makeText(Display_List.this, name, Toast.LENGTH_LONG).show();
}
});
Hello you are using below code so put it in try catch and check for below options :-
(listv.getItemAtPosition(i)
Returns the item at your position so there could be 2 reasons
1.) make sure you are having any item at that position and it is not returning null
2.) make sure your item at given position is giving you string object
EDIT:-
Use below code and also let me know and if it is still not working ,after not working give me your array initialization code
String name = String.valueOf((listv.getItemAtPosition(i)));
Related
Stackoverflow has really helped me get to this level. Thank you all.
Basically, I want to get an element (sub-part not the whole data) from the selected item in a ListView. I've read here that it could be done using Cursors but no success. This is the associated snippet:
listView.setAdapter(adapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
Cursor member = (Cursor)parent.getItemAtPosition(position);
Toast.makeText(getApplicationContext(),
"Click ListItem Number "+member.getString(2), Toast.LENGTH_LONG)
.show();
}
});
parent.getItemAtPosition(position) gives me the (Array, I suppose):
{name="xyz_name", uname="xyz_user", desig="xyz_desig"} correctly.
I want to, suppose, fetch unamefrom this, so, have used Cursor to fetch it but the above snippet is giving the error,
java.lang.ClassCastException: java.util.HashMap cannot be cast to android.database.Cursor
Adding detail: I'm getting the text associated with the listView item correctly. What I actually want a sub-part of the data retrieved. For example the data retrieved is obviously an array containing a name and his role. Now, I just want to fetch the 'role' element in the array. Since, the data is retrieved as an array, what I've searched on net is that I can use Cursors to retrieve different elements. Hope you got me now.
There's seem to be a silly syntax error by my side. If you have any alternatives, please tell.
Thanks :)
go with the simple way
ListView list = (ListView) findViewById(R.id.listview);
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Object listItem = list.getItemAtPosition(position);
Toast.makeText(getApplicationContext(),"Click ListItem Number"+listItem.getString(), Toast.LENGTH_LONG).show();
}
});
Understanding your case, getItemAtPosition(position) is returning you an Object.
The answer posted by #Charuka Silva might not be working for you as you may have used hashmap object something like HashMap<String,String> obj = new HashMap<String,String>();
So, the solution is to use HashMap again for getting the returned object and using get("key") function to retrieve the value associated with it. I think this is what you want:
listView.setAdapter(adapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
//Use HashMap here
HashMap<String, Object> member = (HashMap<String, Object>) parent.getItemAtPosition(position);
//Use obj.get("key") here to retrieve the value associated with the "key"
Toast.makeText(getApplicationContext(),"member.get("uname"), Toast.LENGTH_LONG)
.show();
}
});
I had a similar problem in my Minor Project and an answer by #user1068963 helped me. Adding Link.
Hope this helps.
I am trying to search for some data in Mysql database, then display it Listview
When more than 1 result is found, clicking on any one of them will take you to its respective details page.
But.. I am looking to go to details page directly if there is only one result in list view. Put simply, how would I call onItemClick on the list view automatically when there is only one item in the list.
My Listview's onClickItemListener looks like this:
#Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
// when a student is selected then the following methods are called
DetailsFragment.setLUNCH_TYPE(searchResult.get(position).lunchtype);
Log.d("Lunch type selected", ""+DetailsFragment.getLUNCH_TYPE());
((HomeActivity) getActivity()).startStudentSale(searchResult
.get(position).name.toString(),searchResult
.get(position).isStudent,searchResult.get(position).id);
// it was comming null here when running this this need to find out why
// ((HomeActivity)getActivity()).setmSaleType(SALE_TYPE.SEARCHED_STUDENT);
}
When you know the count of the resultSet, say cnt and if it is 1, i.e.
if(cnt==1){
Intent intent=new Intent(getApplicationContext(),DetailActivity.class);
startActivity(intent);
}
I have a custom ListView with two TextViews both containing different values. What I want to be able to do it get the contents from one of these TextViews when an item is clicked.
This is the code I have so far:
listView.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
String value;
// value = (get value of TextView here)
}
});
I want to be able to assign value to the text of one of the TextView's.
Although #Sam's suggestions will work fine in most scenarios, I actually prefer using the supplied AdapterView in onItemClick(...) for this:
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Person person = (Person) parent.getItemAtPosition(position);
// ...
}
I consider this to be a slightly more fool-proof approach, as the AdapterView will take into account any header views that may potentially be added using ListView.addHeaderView(...).
For example, if your ListView contains one header, tapping on the first item supplied by the adapter will result in the position variable having a value of 1 (rather than 0, which is the default case for no headers), since the header occupies position 0. Hence, it's very easy to mistakenly retrieve the wrong data for a position and introduce an ArrayIndexOutOfBoundsException for the last list item. By retrieving the item from the AdapterView, the position is automatically correctly offset. You can of course manually correct it too, but why not use the tools provided? :)
Just FYI and FWIW.
You have a few options. I reference the code from your previous question.
You can access this data from the row's layout view:
ViewHolder holder = (ViewHolder) view.getTag();
// Now use holder.name.getText().toString() and holder.description as you please
You can access the Adapter with position:
Person person = mAdapter.getItem(position);
// Now use person.name and person.description as you please
(By the way in your Person class, name and description are public so you don't need the get methods.)
Override following method in adaterclass.
public String[] getText() {
return text;
}
I've create the android application program using list adapter that extends the list activity....Inside the list i am having the spinner.. how can i get the value from the spinner inside the list..tell some idea.Thanks in Advance..
try this ::
#Override
public void onItemSelected(AdapterView<?> parent, View view,
int position, long id) {
String str = mSpinner.getSelectedItem().toString();
// getting item id by this
myId = parent.getItemAtPosition(position).toString();
}
I am assuming you only have one spinner in the list. In that case you can get the view by id of that spinner and fetch the value. If there are more than one then you need to add different tags to them while adding them to the list (in ListAdpater) and get these views from tag and get the value.
I am new to android programming. I need some help here. I have used this site example on creating a listview. What I want to achieve is when the user clicks a particular row, the row clicked will perform its respective action. (Eg. When clicked row 1 will show a toast. When clicked row 2 will direct the user to another new view, etc.)
I have set a OnItemClickListener to the listview but am lost on how to do it. Any help will be appreciated. Thanks!
Below is my code:
.......
final ListView list = new ListView(this);
list.setOnItemClickListener(new OnItemClickListener(){
#Override
public void onItemClick(Adapter<?> arg0, View v, int i, long l){
// At implementation
}
});
.......
It looks like you're on the right track, everything should be clearer with onItemClick's parameter names :
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
Toast.makeText(getApplicationContext(), "Clicked row " + position,
Toast.LENGTH_SHORT).show();
}
So you actually get the position of the item clicked, and can have a different action for each item.
To get the index of the selected item:
int selected_item = myListView.getPositionForView(view);
or to get the string:
String chosen_item = (myListView.getItemAtPosition(selected_item).toString());
If your class is extending ListActivity replace "myListView" with "this".