Android: Storing selected ListView item in global variable - android

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

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

get string value from item clicked in a listView

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

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

Android start activity by selecting item on listview and place data sent with intent on another list view

Hi i have a problem i would like on my android app that uses a list once an item is clicked on the list the id is captured and a query run the data received is then placed in an intent and a new activity is started and the data placed in a another list any help how this can be achieved with an example if possible
yourlist.OnItemClickListener(listenerOflistView);
private OnItemClickListener listenerOflistView = new OnItemClickListener() {
private String getSelectedItemOfList;
public void onItemClick(AdapterView<?> view, View view1, int pos, long arg3) {
Intent in = new Intent(Context, nextActivity.class);
String getSelectedItemOfList = mylist.get(pos).toString();
in.putExtras("AnythingName", getSelectedItemOfList);
startActivity(in);
}
}
In another activity:
Intent in = getIntent();
in.getStringextras("AnythingName");
then, you get the value of another activity.
I think you can achieve by using OnItemClickListener() for ListView. So that when you click list item you will get the content of Listview.
yourlist.OnItemClickListener(listenerOflistView);
private OnItemClickListener listenerOflistView = new OnItemClickListener() {
private String getSelectedItemOfList;
public void onItemClick(AdapterView<?> view, View view1, int pos, long arg3) {
getSelectedItemOfList = mylist.get(pos).toString();
// here you will get selected item. And you can perform rest of your task here.
}
}

How to interact with Android ListView

I have created a ListView where I have to add different items. Now, when I click on a particular item it displays another window. On that window, I want to display the name of that item which I click on the ListView.
My code:
private ListView contactList;
private String lv_arr[]={"Android","iPhone","BlackBerry","AndroidPeople"};
#Override
public void onCreate(Bundle icicle)
{
super.onCreate(icicle);
setContentView(R.layout.contact_activity);
contactList=(ListView)findViewById(R.id.ListView01);
contactList.setAdapter(new ArrayAdapter<String (this,android.R.layout.simple_list_item_1 , lv_arr));
contactList.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
/*Intent myIntent = new Intent(view.getContext(), CallActivity.class);
startActivity(myIntent);*/
}
});
}
you have to get the name of the item on itemclick event. pass it to the activity which will be called. in in the calling activity get the name of item and display
contactList.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
String selecteditem = lv_arr[position];
Intent myIntent = new Intent(view.getContext(), CallActivity.class);
intent.putExtra("item", selecteditem);
startActivity(myIntent);
}
});
In CallActivity.java
write the following to get the selected item name
String selectedItem=getIntent().getStringExtra("item");
Instead of starting a new activity, use AlertDialog. You already have the position of the list item clicked. So displaying it on the dialog shouldn't be a problem if you follow the article linked.
EDIT :
As per your requirement, you have to launch a new activity to display a string
In the sending list activity
intent.putExtra(String key, String value)
In receiving activity,
String value = getIntent().getStringExtra(key);

Categories

Resources