I am trying to get the selected value out of a single choice list view and it won't let me use the setOnItemClickListener any ideas?
final ListView lv = (ListView) findViewById(R.id.listView1);
ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_single_choice, values);
lv.setAdapter(arrayAdapter);
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> myAdapter, View myView, int myItemInt, long mylng) {
String selectedFromList = (lv.getItemAtPosition(myItemInt));
}
}
modify like this: String selectedFromList = (String) (lv.getItemAtPosition(myItemInt));
you don't need to bind the "onItemClickListener", just use "getCheckedItemPosition" and you can get the checked position. then get the selected item from your data source.
Related
i know how to remove a list item by using list adapter.But i want to remove a list item from a actvity which showing the list view.i am using onitemClick Listener for getting data from list item.after getting data i need to remove that item from a list view.how to do that?
You can try to this code..
ListView lv = (ListView) findViewById(R.id.lv);
TestAdapter adpter = new TestAdapter(Test.this, arralist);
lv.setAdapter(adapter);
lv.setOnItemClickListener(new AdapterView.OnItemClickListener()
{
#Override
public void onItemClick (AdapterView < ? > adapterView, View view,int position, long l){
arralist.remove(position);
adapter.notifyDataSetChanged();
}
}
);
While not best practice you can also call the change on the adapter.
view.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
adapter.remove(adapter.getItem(position));
}
});
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 :)
I have the following code that's supposed to remove the item that is clicked on:
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
String itemValue = (String) listView.getItemAtPosition(position);
email_addresses.remove(position);
String size = Integer.toString(email_addresses.size());
Log.d("Size: ", size);
//listView = (ListView) findViewById(R.id.email_list);
//ArrayAdapter<String> adapter = new ArrayAdapter<>(getApplicationContext(), android.R.layout.simple_list_item_1, android.R.id.text1, email_addresses);
//listView.setAdapter(adapter);
ArrayAdapter<String> adapter = new ArrayAdapter<>(mainAppContext, android.R.layout.simple_list_item_1, android.R.id.text1, email_addresses);
((ListView)parent).setAdapter(adapter);
parent.refreshDrawableState();
}
When I click on an item the list is cleared and nothing is displayed. The size is the correct value and the list can be rebuilt but I am unable to get this code to work.
Thank you
Instead of creating a whole new Adapter each time, have you thought about simply making your Adapter a member variable and calling notifyDataSetChanged?
ArrayAdapter<String> adapter = new ArrayAdapter<>(mainAppContext, android.R.layout.simple_list_item_1, android.R.id.text1, email_addresses);
((ListView)parent).setAdapter(adapter);
Becomes:
mAdapter.notifyDataSetChanged();
Define your list and adapter as instant variables and also You have to notify the adapter in order to make the deletion affect ,
Try this ,
listview.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
email_addresses.remove(position);
adapter .notifyDataSetChanged();
}
});
I used list view to list the items of my SQLite database as follows:
listView = (ListView) findViewById(R.id.listView1);
db.open();
Cursor c = db.getAllHistory();
ArrayList<String> temparr = new ArrayList<String>();
if (c.moveToFirst()) {
do {
temparr.add(c.getString(0)+'\t'+c.getString(1)+'\t'+c.getString(2)+'\t'+c.getString(3)+'\t'+c.getString(4)+'\t');
} while(c.moveToNext());
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, temparr);
listView.setAdapter(adapter);
} else {
db.close();
}
Now I want to access the specific list item by using OnItemClickListener. How should i do it?
To get your perticular data-
Implement setOnItemClickListener()-
and position will return list's item position number:-
listView.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view,int position, long id) {
String yourData = temparr.get(position);
}
});
Just call listView.setOnItemClickListener() with your implementation of the listener.
listView.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view,int position, long id) {
// your code
// Toast.makeText(context,temparr.get(position),Toast.LENGTH_SHORT).show();
}
});
parent -> The AdapterView where the click happened.
view -> The view within the AdapterView that was clicked (this will be a view provided by the adapter)
position -> The position of the view in the adapter.
id->The row id of the item that was clicked.
You can set the listener like this:
Put this code in onCreate()
TextView disp = (TextView) findViewById(R.id.textView1);
listView.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
String temp = (String) listView.getItemAtPosition(position);
disp.setText(temp);// display on screen
}
});
EDIT:
As you want to display the listItem on screen in a permenant sort of way rather than displaying a toast, the easiest way that you can do this is by adding a TextView in your layout. See the updated code
Hope this helps.
This is my first time using eclipse for android, I want to know how can I get a value from listview and send it to another activity; something like session in c#
I want to make it so when I choose one of item in listview, and send it to another activity
listview1.setOnItemClickListener(new android.widget.AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,int position, long id) {
/*some code to save data in MainActivity*/
Intent in = new Intent(MainActivity.this,Order.class);
startActivity(in);
}});
and show it in another listview in another activity;
array_list=new String[7];
/*array_list[0]= *something to get data from MainActivity* */
ListView lv = (ListView) findViewById(R.id.listViewMakanan);
ArrayAdapter adapter = new ArrayAdapter(this,android.R.layout.simple_list_item_1,array_list);
lv.setAdapter(adapter);
EDIT:
this is my array string in MainActivity:
private String array_list[];
array_list=new String[7];
array_list[0]="Nasi Gr Seafood";
array_list[1]="Nasi Gr Magelangan";
array_list[2]="Cap Cay Goreng";
array_list[3]="Cap Cay Kuah";
array_list[4]="Sapi Cabe Hijau";
array_list[5]="Iga Lada Hitam";
array_list[6]="Sapo Tahu Ayam";
and I use this to put my array in ListView:
ListView lv = (ListView) findViewById(R.id.listview1);
ArrayAdapter adapter = new ArrayAdapter(this,android.R.layout.simple_list_item_1, array_list);
lv.setAdapter(adapter);
Make it like this.
listview1.setOnItemClickListener(new android.widget.AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,int position, long id) {
Toast.makeText(getApplicationContext(), array_list[position], Toast.LENGTH_LONG).show();
/*some code to save data in MainActivity*/
Intent in = new Intent(MainActivity.this,Order.class);
in.putExtra("ListValue", array_list[position]);
startActivity(in);
}});
In your Order.class
//to get the value from the MainActivity.this.
String value= getIntent().getExtras().getString("ListValue");
Hope this will help you.
You can use Intent to pass the data between Activities.
Second Solution:
You can use public static Arraylist<String> MyArraylist in your MainActivity & you can access it in another Activity as MainActivity.MyArraylist.