Get element from the selected item in the listView - android

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.

Related

how to get text from listview on clicking that item

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

Android:Checked Listview Item Goes at Last

My ListView looks like this:
[CheckBox] [TextView]
My question is, how can I change the item position when CheckBox is checked? I mean to say, if the user checked any item from ListView, then the checked item goes to the end, so its position changes from current to last.
Thanks in Advance.
Use Custom adapter for listView. here is an example. Now from your ListActivity class
final ListView lv = getListView();
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
HashMap<String, Object> map = (HashMap<String, Object>)
lv.getItemAtPosition(position);
// get the item Position from here and make the nacessary changes
// and update the listview again.
}
Without posting any code, this will have to be a brief overview of the pseudo steps you need to take
You simply need to update the ordering of your data set being used by your adapter (usually an arraylist or array of objects), and then called
notifyDataSetChanged()
on your adapter.
So in your case, you want to take the element at the position that was clicked, and put it at the end of your arrayList of objects.
If you post some code, you may get more detailed answers however
Here is the step you can follow. I have not tested it but you can try this.
// your value array you are binding with listview
private final String[] values;
// put default value to "n". size will be the same as for values array.
private final String[] valuesChecked;
onClick of Checkbox Listview
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//get checkbox is checked or not
if it is checked then{
valuesChecked[position]="y";
}else{
valuesChecked[position]="n";
}
// short your values array by first "n" and after "y".
// call your adapter.notifyDataSetChanged();
}
Good Luck :)

Working with LazyList by Fedor onitemclick implementation

I will sound stupid to most of you by asking this, but I haven't been able to figure it out on my own and can't pass to my next task until I found an answer.
So far I have downloaded the LazyList project from https://github.com/thest1/LazyList made by Fedor, I'm trying to understand how it works so I can implement it on my own project. My problem is that I don't know where to implement the onitemclicklistener part:
AdapterView.OnItemClickListener onitemClick = new AdapterView.OnItemClickListener()public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
noteId= //the item id from the list
Toast.makeText(getApplicationContext(), noteId, 2000).show();
}
I have try to add it on my mainActivity but then I can't find how to connect to my LazyAdapter to find the item id to be displayed in a toast. My project will contain some other information in the list (as a table several columns) so I want to be able to access an specific columns from that rows using the item id. and testing with a simple toast will help.
Thanks, I hope you don't laugh to much and help me a little.
I think you are going about it in the wrong manner: AdapterView.OnItemClickListener is an interface and the onItemClick() method takes four parameters... I assume this is what you are trying to do:
ListView listView = (ListView) findViewById(R.id.list);
listView.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(getApplicationContext(), id + "", 2000).show();
}
});

how to get individual associated id's of listview elements when clicked

I am displaying an arraylist through a listactivity. Also, i have associated id's with these arraylist elements which i can get together in a hashmap maybe. But the problem is i want to obtain that particular id of the element when it is clicked. How to achieve that. Any help is appreciated.
You can return the id based on the clicked position if you an implementation of the getItemId(position) method in your adapter. The id you return here will be the long id argument in the onListItemClick(...) method of your ListActivity.
getItemId(int position) is a member of the ArrayAdapter class. The ListActivity will call that function from the adapter of yourListView. All you have to do is to provide the implementation, the rest will work out just fine.
http://developer.android.com/reference/android/widget/ArrayAdapter.html#getItemId(int)
ListView lv = getListView();
lv.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapter, View v, int position, long id) {
long pos=lv.getItemAtPosition(position);
//do your stuff here
}
});
if you are using your own ArrayAdapter, you can implement the getItem(position) method..
The answer that worked for me is as follows. Hope it maybe useful for others.
I had the data in Arraylist> testdata. I passed it like this :-
this.setListAdapter(new SimpleAdapter(Grammar_tab_start.this, testdata ,
.layout.tab_single_item, new String[] { "module_name"},
new int[] { R.id.from}));
ListView lv = getListView();
to single list item on click
lv.setOnItemClickListener(new OnItemClickListener()
public void onItemClick(AdapterView<?> parent, View view,
int position, long id)
{
Log.i("check","Module item clicked");
HashMap<String,String> pos=(HashMap<String, String>) lv.getItemAtPosition(position) ;
Log.i("check","----" + position + "id" + id +"actual is "+ pos.get("module_id"));
}
I used the lv.getItemAtPosition() function to retrieve the whole object that was clicked. And then took th eid part from it. Cheers.

Get reference to an object within a ListView

Up front: This is my first attempt at an Android app. I'm in that strange place of not knowing what to search for to find the answer to my question.
What I have accomplished is:
Created a custom class myCustomClass with properties of 'title' and 'youTubeUrl'
Created an ArrayList<myCustomClass>
Added multiple elements to ArrayList<myCustomClass>
Created a custom ArrayAdapter and attached it to the the arraylist.
Added an onItemClickListener to the custom ArrayAdapter.
All of that works good. I would like to show the title in the ListView and then when the user clicks the list view item, I'd like to get a reference to the youtubeUrl property.
Here's what I have for the adapter code:
MyListAdapter myListAdapter = new MyListAdapter(this, R.layout.my_list, elements);
myList.setAdapter(myListAdapter);
myList.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
String item = ((TextView)view).getText().toString();
Toast.makeText(getBaseContext(), item, Toast.LENGTH_LONG).show();
}
});
myListAdapter.notifyDataSetChanged();
Thanks for your help.
You can use the position property in onItemClick to go back to your data source and find the relevant item. From there you should be able to retrieve the Url.
As another poster implied, it depends on what you are using in your adapter. Assuming it's MyCustomClass. You can do something like this in your onItemClick method:
MyCustomClass selection = (MyCustomClass) getListView().getItemAtPosition(position);

Categories

Resources