show an open spinner - android

I have Async task to provide me list of some cities and after I have the list, I want to show me an OPEN spinner directly without any dialogs. My code opens spinner with propper list, but clickListener doesn't seems to work.
my code:
ArrayAdapter<String> listAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, spinnerCities);
listAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner = new Spinner(this);
spinner.setAdapter(listAdapter);
spinner.performClick();
spinner.setOnItemSelectedListener(new OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> aView, View selectedItemView,
int position, long longID) {
System.out.println("ON ITEM CLICK LISTENER HERE");
}
public void onNothingSelected(AdapterView<?> aView) {
}
});
What I am missing here?

So after several hours, I figured it out. I need to use Dialog, but result is the same I have wanted. Here is source in case, someone is find useful.
private void MyMethod(){
ArrayAdapter<String> listAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_dropdown_item,
(String[])MyClass.getListOfCities().toArray());
AlertDialog.Builder ad = new AlertDialog.Builder(this);
ad.setIcon(icon);
ad.setTitle("Title");
ad.setSingleChoiceItems( listAdapter, -1, new OnClickListener() {
public void onClick(DialogInterface dialog, int position) {
// DO something when I click on item
dialog.dismiss();
}
});
ad.show();
}

Related

Can i close an alert dialog on list click?

I have an Alert Dialog which has a list on it, and i want to close onlistclick is it possible?
AlertDialog.Builder builder = new AlertDialog.Builder(this);
final String[] Categories = SQLiteHelper.getAllCategories();//this is where i get the array for my list
ListView myList = new ListView(this);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.alert_dialog_list_view, Categories);
myList.setAdapter(adapter);
myList.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view, int position, long rowId) {
//doing something in here and then close
}
});
builder.setTitle("Please Choose");
builder.setInverseBackgroundForced(true);
builder.setView(myList);
final Dialog dialog = builder.create();
dialog.show();
}
The alert dialog is running perfect i just dont want to put any buttons in it.
If you define the onItemClickListener after the Dialog you can just call dialog.dismiss(); in the onItemClick() method.
check below code
myList.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view, int position, long rowId) {
//doing something in here and then close
dialog.dismiss();
}
});

Control spinner after click button

I want to show spinner element after click to TextView.
I use the next code
final Spinner spin = new Spinner(context);
String[] tlt = {"Apple", "Orange", "Plum"};
ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(context, android.R.layout.simple_spinner_dropdown_item, tlt);
spin.setOnItemSelectedListener(new OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id) {
Log.v("itemNO", position+"");
}
public void onNothingSelected(AdapterView<?> parentView) {
// your code here
}
});
spin.setAdapter(arrayAdapter);
txt = (TextView)findViewById(R.id.lang);
txt.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
spin.performClick();
}
});
In my .xml file only TextView present (may be it's wrong?)
After TextView click appear Spinner menu but when I choose some item nothing happen. It looks like setOnItemSelectedListener() does not work.
Have you any ideas?
Thank you for answers!
Please check this example from the google developer site:
http://developer.android.com/resources/tutorials/views/hello-spinner.html
You should add the Spinner to the xml file.

android:opening new intent in spinner view

I am making an app in which i have to use spinner view to show some items and i want on click of item it should go to that page. i want when i clicke on spanish i should go to spanish pageMy code is as follows.
System.out.println("test1");
Spinner spinner = (Spinner) findViewById(R.id.spinner);
System.out.println("test2");
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
this, R.array.models, android.R.layout.simple_spinner_item);
System.out.println("test3");
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
System.out.println("test3");
spinner.setAdapter(adapter);
System.out.println("test4");
spinner.setOnItemSelectedListener(new MyOnItemSelectedListener());
}
public class MyOnItemSelectedListener implements OnItemSelectedListener {
public void onItemSelected(AdapterView<?> parent,
View view, int pos, long id) {
}
public void onNothingSelected(AdapterView parent) {
// Do nothing.
}
}
You have to write intent in your onItemSelected method for page/activity which you want to open.
and set position for that.
#Override
public void onItemSelected(AdapterView<?> adaptername, View view,int position, long id)
{
if(position==0)
{
// write the intent for page which you want to open
}
if(position==1)
{
//
}
.
.
.
and same
}

listview open alert dialog depending on choice android

Im desperately trying to get my listView to open an alert dialog or a normal dialog that is filled with information. I cant seem to get it to work. I want it to display different information aswell depending on which item on the list is clicked
public class learn_tab1 extends ListActivity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setListAdapter(new ArrayAdapter<String>(this, R.layout.list_item,
BASICLIST));
ListView lv = getListView();
lv.setTextFilterEnabled(true);
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
AlertDialog dialog = new AlertDialog.Builder(this).create();
dialog.setCancelable(false);
dialog.setTitle("Instructions");
dialog.setIcon(R.drawable.bone_icon);
dialog.setMessage("test");
dialog.setButton(DialogInterface.BUTTON_POSITIVE, "Done", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
finish();
}
});
dialog.show();
}
}
});
}
Try new AlertDialog.Builder(learn_tab1.this).create(); instead of new AlertDialog.Builder(this).create().
I'm wondering how it ever get complied....
EDIT
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
String item = (String)parent.getItemAtPosition(position);
// ...
dialog.setMessage(item);
// ...
}

Remove ListView items in Android

Can somebody please give me an example code of removing all ListView items and replacing with new items?
I tried replacing the adapter items without success. My code is
populateList(){
results //populated arraylist with strings
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, results);
listview.setAdapter(adapter);
adapter.notifyDataSetChanged();
listview.setOnItemClickListener(this);
}
// now populating list again
repopulateList(){
results1 //populated arraylist with strings
ArrayAdapter<String> adapter1 = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, results1);
listview.setAdapter(adapter1);
adapter1.notifyDataSetChanged();
listview.setOnItemClickListener(this);
}
Here repopulateList() method will add to ListView items, but it doesn't remove/replace all ListView items.
You will want to remove() the item from your adapter object and then just run the notifyDatasetChanged() on the Adapter, any ListViews will (should) recycle and update on it's own.
Here's a brief activity example with AlertDialogs:
adapter = new MyListAdapter(this);
lv = (ListView) findViewById(android.R.id.list);
lv.setAdapter(adapter);
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> a, View v, int position, long id) {
AlertDialog.Builder adb=new AlertDialog.Builder(MyActivity.this);
adb.setTitle("Delete?");
adb.setMessage("Are you sure you want to delete " + position);
final int positionToRemove = position;
adb.setNegativeButton("Cancel", null);
adb.setPositiveButton("Ok", new AlertDialog.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
MyDataObject.remove(positionToRemove);
adapter.notifyDataSetChanged();
}});
adb.show();
}
});
I think if u add the following code, it will work
listview.invalidateViews();
To remove an item, Just remove that item from the arraylist that we passed to the adapter and do listview.invalidateViews();
This will refresh the listview
You can use
adapter.clear()
that will remove all item of your first adapter then you could either set another adapter or reuse the adapter and add the items to the old adapter. If you use
adapter.add()
to add data to your list you don't need to call notifyDataSetChanged
int count = adapter.getCount();
for (int i = 0; i < count; i++) {
adapter.remove(adapter.getItem(i));
}
then call notifyDataSetChanged();
Remove it from the adapter and then notify the arrayadapter that data set has changed.
m_adapter.remove(o);
m_adapter.notifyDataSetChanged();
names = db.getSites();
la = new ArrayAdapter<String>(EditSiteList.this,
android.R.layout.simple_list_item_1, names);
EditSiteList.this.setListAdapter(la);
listview.invalidateViews();
this code works fine for me.
You should use only one adapter binded with the list of data you need to list
When you need to remove and replace all items, you have to clear all items from data-list using "list.clear()" and then add new data using "list.addAll(List)"
here an example:
List<String> myList = new ArrayList<String>();
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, myList);
listview.setAdapter(adapter);
listview.setOnItemClickListener(this);
populateList(){
List<String> result = getDataMethods();
myList.addAll(result);
adapter.notifyDataSetChanged();
}
repopulateList(){
List<String> result = getDataMethods();
myList.clear();
myList.addAll(result);
adapter.notifyDataSetChanged();
}
Try this code, it works for me.
public class Third extends ListActivity {
private ArrayAdapter<String> adapter;
private List<String> liste;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_third);
String[] values = new String[] { "Android", "iPhone", "WindowsMobile",
"Blackberry", "WebOS", "Ubuntu", "Windows7", "Max OS X",
"Linux", "OS/2" };
liste = new ArrayList<String>();
Collections.addAll(liste, values);
adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, liste);
setListAdapter(adapter);
}
#Override
protected void onListItemClick(ListView l, View v, int position, long id) {
liste.remove(position);
adapter.notifyDataSetChanged();
}
}
At first you should remove the item from your list. Later you may empty your adapter and refill it with new list.
private void add(final List<Track> trackList) {
MyAdapter bindingData = new MyAdapter(MyActivity.this, trackList);
list = (ListView) findViewById(R.id.my_list); // TODO
list.setAdapter(bindingData);
// Click event for single list row
list.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
final int position, long id) {
// ShowPlacePref(places, position);
AlertDialog.Builder showPlace = new AlertDialog.Builder(
Favoriler.this);
showPlace.setMessage("Remove from list?");
showPlace.setPositiveButton("DELETE", new OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
trackList.remove(position); //FIRST OF ALL REMOVE ITEM FROM LIST
list.setAdapter(null); // THEN EMPTY YOUR ADAPTER
add(trackList); // AT LAST REFILL YOUR LISTVIEW (Recursively)
}
});
showPlace.setNegativeButton("CANCEL", new OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
}
});
showPlace.show();
}
});
}
It's simple .First you should clear your collection and after clear list like this code :
yourCollection.clear();
setListAdapter(null);
You can also use listView.setOnItemLongClickListener to delete selected item.
Below is the code.
// listView = name of your ListView
listView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
#Override
public boolean onItemLongClick(AdapterView<?> parent, View view, int
position, long id) {
// it will get the position of selected item from the ListView
final int selected_item = position;
new AlertDialog.Builder(MainActivity.this).
setIcon(android.R.drawable.ic_delete)
.setTitle("Are you sure...")
.setMessage("Do you want to delete the selected item..?")
.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which)
{
list.remove(selected_item);
arrayAdapter.notifyDataSetChanged();
}
})
.setNegativeButton("No" , null).show();
return true;
}
});
//try this:
String text = ( (TextView) view).getText().toString();
adapter.remove(text);

Categories

Resources