Getting value of ListView TextView - android

I have this ListView setup:
ListView listView = getListView();
listView.setTextFilterEnabled(true);
listView.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> arg0, View arg1,
int arg2, long id) {
//
Intent i = new Intent(getApplicationContext(), Rate.class);
startActivity(i);
}
});
setListAdapter(new ArrayAdapter<String>(Items.this, R.layout.list,
items));
It is a standard ListView as you see. Each row has a single TextView populated with the List "items". I am pulling that in from a MySQL Database and outputting through a JSON loop.
I simply want to be able to click on the row, with its single name, and take that name and pass it into the Rate.class activity as a String. How can I do this in code?

Add putExtra to your intent (see below). I am assuming you have the list 'items' as the member of the activity, the arg2 argument is basically the index of the item clicked in the list, which you can use to get the name from the 'items' list.
public void onItemClick(AdapterView<?> arg0, View arg1,
int arg2, long id) {
//
Intent i = new Intent(getApplicationContext(), Rate.class);
i.putExtra("name", items.get(arg2));
startActivity(i);
}

You could create a specific array adapter instance instead of just creating one to pass to the function. Then in the OnItemClick() function, int arg2 is the index in the array adapter that the user selected. You can get the view from the array adapter and getText() from the text view.

One of the things passed to onItemClick is the view that was clicked:
Cast view to the appropriate type and call getText() on it; for example:
final String text = ((TextView)view).getText();

Related

How to get _id from listView (database) [android]

I have a problem with receiving _ID value when an item is clicked on listView.
I have this code:
List<SavedSearch> values = mydb.getAllSavedSearches();
ArrayAdapter<SavedSearch> adapter = new ArrayAdapter<SavedSearch>(this,
android.R.layout.simple_list_item_1, values);
//adding it to the list view.
obj = (ListView)findViewById(R.id.listView1);
obj.setAdapter(adapter);
obj.setOnItemClickListener(new OnItemClickListener(){
#Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
And my problem is that i want in onItemClick to somehow get the _ID value from database of clicked item in listView. int position and long id both are returning just position on the list.
Thanks for help, and I can say that any of previous topics helped me.
If you're using a database I would suggest you do not use an ArrayAdapter and instead use a CursorAdapter. Then simply call the method getItemId() from your CursorAdapter to retrieve the id of an item at a given position. So, do this:
CursorAdapter adapter = new CursorAdapter(Context yourAppContext, Cursor yourDBCursor, false);
obj = (ListView)findViewById(R.id.listView1);
obj.setAdapter(adapter);
obj.setOnItemClickListener(new OnItemClickListener(){
#Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
adapter.getItemId(position); // This is the _ID value
}
This is just the suggestion. I am not sure this will solve the issue.
Method 1:
Whenever you load the listView just set the id for the row and you can get the id from onItemClikListener method
Method 2:
Add an texView (set visibility gone) and set text as your id and get id using getText() method
Note
Don't use position this is always changing when the listview is reCycling.

Get a text on an Item using OnItemClickListener

I have a ListView with an adapter
And I want to use the implemented method to go on another activity(SendSmsActivity.class):
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
viewCampagneId = (TextView)findViewById(R.id.idCampagne);
final String theCampagneId = viewCampagneId.getText().toString();
viewCampagneName = (TextView)findViewById(R.id.nomCampagne);
final String theCampagneName = viewCampagneName.getText().toString();
Intent i = new Intent(RecupXml_Activity.this,SendSmsActivity.class);
i.putExtra("ID", theCampagneId);
i.putExtra("name", theCampagneName);
startActivity(i);
}
The SendSmsActivity.class will download an xml according to the ID in the textview2.
for example : If I click on the item "campagne22" it will go to another activity and load an xml at http://blabla/?id="**22**"
So I have to get the text in textview2 to put the value ID in a PutExtra (so activity2 can catch it)
How can I do it ?
With my actual code above, it doesn't work, it save the item at the top of the view not the one I selected..
PS : It seems I don't really know how the arguments in the method onItemClickListener works..
The second argument in onItemClick() is the view you clicked on, so to get the containing textView, you can use
viewCampagneId = (TextView) arg1.findViewById(R.id.idCampagne);
viewCampagneName = (TextView) arg1.findViewById(R.id.nomCampagne);

How to get data from ListView. (My List View has 3 items)

I want retrieve the data from the Listview, where my list is having 4 items like, name, date, sem, category.
i want to read the text present in the list view and pass it to another activity.
If you are retrieving your data from server then you need to get item on item click of ListView. Like
listview.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
String name =urarraylist.get(position).get("name");// here you have to pas keyname which is put inyour Hashmap arraylist
String category =urarraylist.get(position).get("category");
String date=urarraylist.get(position).get("date");
String sem=urarraylist.get(position).get("sem");
Intent i = new Intent(getBaseContext(),anotheractivity.class);
i.putExtra("name", name);
i.putExtra("category ", category );
i.putExtra("date", date);
i.putExtra("sem", sem);
startActivity(i);
}
});
Now retrieve this data in your next Activity on onCreate() method. Like
Intent n = getIntent();
String name = n.getStringExtra("name");
String category = n.getStringExtra("category ");
String date= n.getStringExtra("date");
String sem= n.getStringExtra("sem");
As it's name suggest, a ListView doesn't contains data, it only contains Views. These views are often created from an array or a List via an Adapter. So you have to ask your adapter to retrieve the data.
Unfortunately there is no default method for that, so you'll have to create your own getter, or you can also loop on the getItem() method associated with the getCount() one on your ListView's adapter.
listview.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
String name=(String)parent.getItemAtPosition(position);
Intent i = new Intent(getBaseContext(),anotheractivity.class);
i.putExtra("USERNAME", name);
startActivity(i);
}
});
if you click the listview row the value from corresponding position will be fetched and passed to intent and further to next activity.
The magic lies here:
String name=(String)parent.getItemAtPosition(position);

Get specific id from a listview

I have a ListView which is implemented using a customs adapter. For making the adapter i am using a holder class. The class has various TextViews and ImageViews as well as an Int variable id to store the id being fetched from the database. Now when i click the particular list i want to get the id so that using it I can further display the information on a new activity. The id is not meant to be displayed in the ListView. How can i get the id from onItemClickListener()
You can set a tag to a View (.setTag()) and then retrieve it, it happens on getView() method inside your custom Adapter
Here is a sample code:
listView.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
String transactionId = ((TextView) view
.findViewById(R.id.tvTID)).getText().toString();
handler.getTransactionDetails(callback, transactionId);
}
});
#Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
YourObject selected = adapter.getItem(position);
Then you can send the selected list item (Object) to your new activity
Intent mIntent = new Intent(ThisActivity.this, NewActivity.class);
mIntent.putExtra("list_selected", selected);
startActivity(mIntent);
as well as an Int variable id to store the id being fetched from the
database.
You didn't provide more infomation about that id (i don't know exactly what it presents) but to make it simple you can set this id for each widget in ListAdapter via setTag() method and then simply retrieve it from your onItemClick() method.

How to retrieve the clicked string from a listview using OnItemClick?

I've got some problem here. It looks simple and i keep searching for its solution. Unfortunately, i cant find anything. This is my problem.... What i'm trying to do is to get the string showed in the listview from an On item click method.
This is my listview :
- lol
- hi
- waw
When i click "lol" i want to get the "lol" string.....
What should i put in my code here? :
lv = (ListView) findViewById(R.id.list_view);
lv.setOnItemClickListener(new OnItemClickListener()
{
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,long arg3)
{
// Intent newI = new Intent(this,PDetail.class);
Intent newI = new Intent (Create.this, PDetail.class);
//String sd = ((() arg1).getText()).toString();
//newI.putExtra("x", arg2);
startActivity (newI);
// db.getList(arg3);
}});
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,long arg3)
{
String data=(String)arg0.getItemAtPosition(arg2);
}});
data contains your clicked position's data. Do what ever you want to do with that.
arg0 is your AdapterView, typically a ListView. arg2 is the position in the ListView. You can get the items from your Adapter :
Object item = arg0.getItemAtPosition(arg2);
Depending on the type of your object in your adapter, a trivial solution is:
String value = item.toString();
The arg1 parameter of your listener is the clicked item's view.
Assuming that the items of your ListView are TextViews you can try:
String itemText = ((TextView) arg1).getText();
use String val = (String)arg0.getItemAtPosition(arg2)
For Kotlin use this:
mListView.setOnItemClickListener{ parent, view, position, id ->
println("Item : " + (view as TextView).text)
}
This only works if you inflated your adapter with just a TextView otherwise instead of casting it to TextView you should get your TextView by doing something like
view.findViewById(R.id.your_textview_id)

Categories

Resources