get string value from item clicked in a listView - android

Hi i am new to android programming, i have make an Http post request to get json data from an external sql database and displayed my result in a lisView. i want to be able to retrieve the string value from a clicked item in the listView. Please any help with this will be much appreciated

I would try something like this, which has worked for me in the past:
String itemValue = (String) listView.getItemAtPosition(position);
This is from within the listView.setOnItemClickListener(new OnItemClickListener() portion of your code.
If your strings are aggregated into one string, then try this:
//Let itemValue = "item1 item2 item3" for example:
String[] parts = itemValue.split(" ");
String part1 = parts[0]; // item1
String part2 = parts[1]; // item2

Set OnItemClickListener on listview. See on below.
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
String itemString=listView.getSelectedItem().toString();
}
});
Enjoy!!!...

try this:
listView.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
String itemString=listView.getSelectedItem().toString();
}
});

Related

How to get value of a ListView item, populated through FirebaseListAdapter

I have a ListView that I populate using the FirebaseListAdapter. Now I want the OnItemClickListener to show the value of the item clicked (the text displayed on the item) in a Toast.
ListView listView;
ListAdapter listAdapter;
mFirebaseRef = new Firebase("https://<firebase root ref>/posts");
mFirebaseQuery = mFirebaseRef.orderByChild("author_id").equalTo(id); //`id` is a variable defined earlier
listAdapter = new FirebaseListAdapter<Post>(this, Post.class, android.R.layout.simple_list_item_, mFirebaseQuery) {
#Override
protected void populateView(View view, Post post) {
String postTopic = post.getTopic();
((TextView) view.findViewById(android.R.id.text1)).setText(postTopic);
}
};
listView = (ListView) findViewById(R.id.postsList);
listView.setAdapter(listAdapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
String topic = String.valueOf(parent.getItemAtPosition(position));
Toast.makeText(getApplicationContext(), "List Item Value: "+topic, Toast.LENGTH_LONG).show();
}
});
Instead of returning the text displayed on the item (as is in the case an ArrayAdapter is used), String.valueOf returns some reference to the model class I use - Post.class.
The output is something like this: com.example.android.model.Post#a1e1874
Can anybody tell me how to get the value of the list item in this case?
AdapterView.getItemAtPosition() returns a Post object, so:
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Post post = (Post)parent.getItemAtPosition(position);
String topic = post.getTopic();
Toast.makeText(getApplicationContext(), "List Item Value: "+topic, Toast.LENGTH_LONG).show();
}

Android: Storing selected ListView item in global variable

I have two activities.
The first activity contains a ListView widget that is populated such:
String products[] = {"Pull Restaurant names here", "Res. 1", "Res abc", "Res foo", "Res hello",
"Res xyz", "Res test", "Res test2", "Denny's"};
lv = (ListView) findViewById(R.id.listView);
// Adding items to listview
adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, products);
lv.setAdapter(adapter);
Once the user selects an item from the list, I load a new activity such:
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
android.content.Intent intent = new android.content.Intent(GuestPage.this, RestaurantProfile.class);
startActivity(intent);
}
});
I want to be able to store the selected item string from the ListView into a global variable. I have a Global class that extends application, I have included the necessary name tag in my manifest. I am using something similar to:
Globals g = (Globals)getApplication();
g.setData(String s);
in order to set the Global variable.
My issue is with storing the selected ListView item because I don't know how to call it. I am not using any xml Item files to populate my ListView. How do I know which item is selected and how do I store that into my global variable?
FOR EXAMPLE: user selects "Res foo". New activity is loaded and I have a global variable to use among all activities that contains String "Res foo".
Thanks.
What you're doing with your Globals is a very bad practice. Instead take a look a this solution for passing data between Activities.
https://stackoverflow.com/a/7325248/3474528
To get the selected string you would want to do something like:
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
String selected = products[position];
....
}
public void onItemClick(AdapterView<?> parent, View view, int position, long id)
int position holds the index of the item clicked in the ListView.
You can then use that position to get the text:
String theText = (ListView)parent.getItemAtPosition(position);
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> myAdapter, View myView, int myItemInt, long mylng) {
String selectedFromList = (String) (lv.getItemAtPosition(myItemInt));
android.content.Intent intent = new android.content.Intent(GuestPage.this, RestaurantProfile.class);
startActivity(intent);
}
});
I hope this fix your problem :)

Getting ID value when clicking on listview

I'm having some trouble getting the correct id value when clicking on the listview.
I made a custom adapter since I'm drawing from SQLite data, I get the right text back but when I click on a row, I get a 0 index id back, which is not what I want.
I'm also using Sugar ORM, so I'm not sure if that requires some special work from me to get the id field.
I'm not sure what I should show you in terms of code, so whatever you think will help you, let me know and I'll post it.
Thanks
BoardArrayAdapter adapter = new BoardArrayAdapter(this, boards);
setListAdapter(adapter);
ListView lv = getListView();
registerForContextMenu(lv);
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(getApplicationContext(), "Board ID " + id, Toast.LENGTH_SHORT).show();
}
});
You need to override the getItemId() method in your adapter.
According to the SugarORM docs you can call getid()to return the db id.
ArrayList<Object> data;
#Override
public long getItemId(int position) {
return data.get(position).getid();
}
This is how I am using on my project (with SQLite and CustomListView).
getData method
public void getData(View view, long id)
{
//storing the ID into a public static variable and converting to int
CLIENTE_ID = (int)id;
//When the user touches on the field in the listview, I get the touched field's name and email
TextView textViewnome = (TextView) view.findViewById(R.id.tv_cliente_nome);
TextView textViewemail = (TextView) view.findViewById(R.id.tv_cliente_email);
//Stores both name and email of the touched field
String nome = textViewnome.getText().toString();
String email = textViewemail.getText().toString();
//store into static variable
CLIENTE_NOME = nome;
CLIENTE_EMAIL = email;
}
within onCreate:
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id)
{
getData(view, id);
Log.d("myClass", "ID: " + view.getId());
}
});
I hope it helps! :)

Issue. Get position arraylist in autocompleteTextView in android

I'm beginning learn Android. I have issue. I have autocompleteTextView with content is arrayList. Then I want to get position of element in arraylist when I click on autoCompleteTextView. Can you show me do it ?
Thank you so much !
This code !
/* ================= looping through All categories ==================== */
for(int i=0;i<product.length();i++){
JSONObject c = product.getJSONObject(i);
// Storing each json item in variable
pro_id = c.getString(TAG_ID);
pro_name = c.getString(TAG_NAME);
name_product.add(pro_name);
} // end for products
} catch (JSONException e) {
e.printStackTrace();
}
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_dropdown_item_1line, name_product);
AutoCompleteTextView textView = (AutoCompleteTextView)findViewById(R.id.result_search);
textView.setThreshold(1);
textView.setAdapter(adapter);
textView.setOnItemClickListener(new OnItemClickListener(){
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long rowId) {
//here position is your selected item id
/*String selecteditem =name_product.get(position);
Log.d("Test",selecteditem.toString());
int pos = Integer.parseInt(selecteditem);
Toast.makeText(getApplicationContext(), "cvxcvcv",pos).show();*/
// I want to get position when i click element in autocompletetextView
}
});
Try
autocompletetextview.setOnItemClickListener(new OnItemClickListener(){
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long rowId) {
//here position is your selected item id
int selectedposition=position;
}
});

getting the text of the listitem which i clicked in android

i have a listview. i have to get the text of the list item which i clicked. i tried using onitemclick. but i am not able to get. can some 1 please explain me how to get it.
here is my onitemclick method
lstcities.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick (AdapterView<?> parent, View view, int position, long id) {
// TODO Auto-generated method stub
// String str =(String) lstcities.getItemAtPosition(position);
String S =view.getContext().toString();
Toast.makeText(getApplicationContext(),
"Click ListItem Number " + S, Toast.LENGTH_LONG)
.show();
thanks
ramu
try this,
setListAdapter(new ArrayAdapter<String>(this, R.layout.yourlayout,your list));
ListView listView = getListView();
listView.setTextFilterEnabled(true);
listView.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// When clicked, show a toast with the TextView text
String selectedValue = (String) getListAdapter().getItem(position);
Toast.makeText(this, selectedValue, Toast.LENGTH_SHORT).show();
}
});
Assuming you are using a TextView, you should be able to do
String text = parent.getItemAtPosition(position).getText().toString()
You can get string from ArrayList used in adapter.
like : String str = arrayList[position]; //Assume arrayList here is String[];
If your using customize view(own xml) for list item. You will get view of whole xml as 2nd parameter(View view) using that you can find out your textView like view.findViewById(your text view id);
If your using android define item android.R.layout.list_item_1 then typecast your view object with textview;
Using this textView you can get text of textView or like same manner any view will be accessible from list item.
Another way to do it is:-
lstcities.setOnItemClickListener(new OnItemClickListener()
{
#Override
public void onItemClick (AdapterView<?> parent, View view, int position, long id)
{
String text = (String) (((ListAdapter)parent.getAdapter).getItem(position));
// Do what you want to with this text
}
}
All you need to simply add below statement :
String selectedFromList = (String) (lv.getItemAtPosition(position));

Categories

Resources