accessing an item in listview - android

I have a list of players whos name are displayed listview. and each row of listview contains button, textview and imageview. How can I get the value of textview?

If you are using Custom Adapter class to populate the listview,then in your adapter,you can use HashMap for saving key-value pair,saving position of listitem with the data into that textview.and then you can easily retrieve it on OnItemClickListener of listview.

Depending on the specifics of your implementation, I would go with one of the following approaches.
Option A.
Use setOnItemClickListener to register a click listener with the list (or if you're using a ListActivity or ListFragment simply use [onListItemClick](http://developer.android.com/reference/android/app/ListActivity.html#onListItemClick%28android.widget.ListView, android.view.View, int, long%29)). onItemClick gets passed in the View that was clicked and can be used to retrieve nested views, e.g. the TextView you're looking for.
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
TextView tv = (TextView) view.findViewById(R.id.textview);
String tvText = tv.getText();
}
}
Option B
Assuming you fill your list from some sort of data collection, you may be able to do something similar to above, but use the passed position parameter as an index to directly get the text from the objects in your collection; i.e.:
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
SomeObject so = myCollection.get(position);
String tvText = so.getTextViewText();
}
}
There are lots of more options though. I kind of like creating my own extension of ArrayAdapter to hold the models for the views of the items in the list. That way you could also call getItemAtPosition(int position) or getItem(int position) and cast the returned object to your data type.

Is it a static list or a dynamically generated one? If its a static one you can assign a different id to each textview in the xml itself, and then use FindViewById to access it. If it's not this is what you've got to do: you will obviously have one row and display it many times. So multiple textviews will have same ID. Use a for loop, inside which use FindViewById(Remember FindViewByID will only access the first element with mentioned Id, set its Id to something else, in the next iteration the next textview is selected, set its Id to something) then use these new ids to access them, thus you can access each textview

Related

How do I get a model instance associated with a listview row using setTag(int, object)?

I have a listview itemClickListener that should get the model instance associated with the row clicked.
I read about tags in http://developer.android.com/reference/android/view/View.html#setTag(int, java.lang.Object)
I know that listview recycles rows, so it would not be a good idea to use v.setTag(currentItem), because that would result in an earlier row being associated with a later item.
So to solve my original problem, it looks like I need to use setTag(int, object) where the body of my click handler needs to know the unique key. The documentation states to use a resource id value, but that is not unique amongst multiple rows. How do I get the model instance for the row I clicked on?
You should just be able to grab the item out of your adapter like this:
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id)
{
ListAdapter a = (ListAdapter)parent.getAdapter();
Object itemAtPosition = a.getItem(position);
}

using string to identify the elements of a listView in android

As the question suggests, is it possible to identify the elements of a listView using string ids or something similar to id? I know the typical signature is
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {...}
But I need to have string ids or such. How would I do that?
Elaboration (sorry if still unclear)
The problem is my underlying list is a rotating list (i.e. circular list)
myList.addFirst(myList.deleteLast());
So the id/index of an item keeps actually floating around. But I need an id that is essentially a part of the element so that no matter the rotation, I can always retrieve the exactly element I mean to.
Create an Array List of strings. Add the strings id for listview in it.
Add a hidden textview in your listitems.
Add all the string id from Array List to your listview hidden textview.
Access the string id as below :
public void onItemClick(AdapterView<?> parent, View view,int position, long id) {
TextView anyId = (TextView) view.findViewById(R.id.anyID);
String val_Id = anyId.getText().toString();
Using ArrayList allows to handle dynamic listview.
You can create an array of string ids for your listview. In onItemClick, you can use the position to assess your string array. As the list items and your string will be of same size, it should have one to one mapping.
I hope you understand what I mean..
If you want to have a String row ID kindly get the position then convert it into string.

Getting data from custom list view on click

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

Get layout information of the view

I have a list view that uses different layout sheets for different rows. Each sheet has different variables on them. So, when I want to implement the click listener for my list I need to know which type of row I am clicking on so that I can try to access to the correct values. For example:
list.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
//do this for layout A
//......
// do that for layout B
//......
}
});
How can I access to the layout information and the corresponding values ?
Make sure that you have a variable in your activity where you've kept the data with which you've populated the adapter.
Then, based on the position variable, you'll be able to get the exact row which was clicked. Then based on the row, you'll be able to figure out what type of row it is, right? :)
you can use the method:
public abstract int getItemViewType (int position) of your custom adapter.

Implementing OnItemClickListener() for a dynamic GirdView/ListView

That GridView adapter creates ImageView from a layout.
All images are downloaded from URLs respect to the database item IDs where the ID is got from a JSONArray.
Let say, the view is now showing items with
ID: 1,3,4,7.
As the GridView items are dynamic, the position (starting from 0) cannot really identify my item on the GridView.
Is there any other ways to identify that image from the database item IDs?
public OnItemClickListener ClickListner = new OnItemClickListener()
{
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//Toast.makeText(getApplicationContext(),
// ""+position, Toast.LENGTH_SHORT).show();
//Identification code for the item to be added here
Intent view =
new Intent(main.this, View.class);
view.putExtra("ID", id);
//expected to have an ID equal to database item ID
startActivity(view);
}
};
First option (the nice way)
You keep a reference to your adapter (or you get it by calling parent.getAdapter() and then cast it)
In your adapter make sure that you've overridden getItem(position) to return the object you used to fill up your adapter (probably something like return arrayList.getItem(position)if you used BaseAdapter)
On the adapter you call getItem(position) and this will give you the very same object, so you should have all the info you need now
Second option (easy way out)
You can put info in the gridviewitem's view using setTag()
then in onItemClick you call getTag() and there you have your unique id
You can use a POJO class to set the URL and ID of Image in that class and create and ArrayList for the same and passing that to the Adapter class. By, doing this you will bind your ImageView and the ImageID from your database. And, then inside onItemClick() you can simply use
POJO pojo = listview.getAdapter().getitem(position);
int id = pojo.getId();

Categories

Resources