I have a list of "categories" that are stored as Strings in an ArrayAdapter in my app. This much is pretty simple. The adapter is a field of the Activity and accessible everywhere. It is populated with values during onCreate().
I have a "Entry" Dialog that contains an AutoCompleteTextView which uses this adapter and works very well. This is basically a dialog to add new items to a list.
I also have a second Dialog which acts as a filter for my list and has a single Spinner which when the Dialog is created, uses the same ArrayAdapter. Here's the problem.
If I use the filter Dialog before the entry Dialog, the Spinner is populated with all of the items from the adapter. Works well.
Any and every time I use the entry Dialog, the AutoCompleteTextView works properly.
The problem comes if I use the entry Dialog and select an item in the AutoCompleteTextView when it suggests something. After selecting a suggested item in the popup, even if I cancel the entry Dialog, the next time I bring up the filter Dialog, the Spinner initially shows the item that was last selected from the AutoCompleteTextView (instead of the first item in the adapter), and only displays that single item in the Spinner's list if clicked/touched. The only way to fix it is to end the application and re-open it. I'm not getting any errors or anything in logcat that is helpful.
EDIT - Okay, I've removed the previous code to replace it with a simple test case that I produced. The bottom line is that I would like to know if this is a bug, or if if it is expected results. When a suggestion is selected in an AutoCompleteTextView, I can confirm that the ArrayAdapter that is linked to it has filtering applied so that it's count is affected and the only items shown if the adapter is accessed in a Spinner will be those that were filtered down to. I also added button to display a toast to show that the count is affected. Type "te" in the autocomplete, select either Test entry. They try the spinner or click the button to see the adapter's count is only 2.
So the final question is now... can the adapter's filter be reset (other than by typing in and clearing the AutoCompleteTextView)? I can't find any method to do this. I have worked around the problem in my actual app by setting up a temporary adapter, copying over the main adapters items and setting the views to use the temporary adapter instead. My phone is running 2.2, and I have tested as high as 2.3.3 API level 10 in an emulator.
The xml for main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<AutoCompleteTextView android:id="#+id/actv"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" />
<Spinner android:id="#+id/spinner"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_centerInParent="true" />
<Button android:id="#+id/button"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Check It"
android:layout_alignParentBottom="true"
android:layout_centerInParent="true" />
</RelativeLayout>
The code for MainActivity.java
public class MainActivity extends Activity {
/** Called when the activity is first created. */
AutoCompleteTextView actv;
Spinner spinner;
Button button;
String [] adapterList = {"Test1", "Test2", "Garbage1", "Garbage2"};
ArrayAdapter<String> adapter;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
adapter = new ArrayAdapter<String>(this, android.R.layout.simple_dropdown_item_1line, adapterList);
actv = (AutoCompleteTextView) findViewById(R.id.actv);
spinner = (Spinner) findViewById(R.id.spinner);
button = (Button) findViewById(R.id.button);
actv.setAdapter(adapter);
spinner.setAdapter(adapter);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(MainActivity.this, "" + adapter.getCount() + " Items in Adapter", Toast.LENGTH_SHORT).show();
}
});
}
}
Looking at AutoCompleteTextView source code, it does filter the ArrayAdapter:
http://grepcode.com/file/repository.grepcode.com/java/ext/com.google.android/android/2.2_r1.1/android/widget/AutoCompleteTextView.java#AutoCompleteTextView.performFiltering%28java.lang.CharSequence%2Cint%29
You may want to remove the filter on an onClick callback in the spinner:
Filter filter = adapter.getFilter();
filter = null;
(see the setAdapter method in AutoCompleteTextView, around line 600)
http://grepcode.com/file/repository.grepcode.com/java/ext/com.google.android/android/2.2_r1.1/android/widget/ArrayAdapter.java#ArrayAdapter.getFilter%28%29
Let me know if it works
BTW, is the adapter going to change dynamically? You're "workaround" of two adapters might be a better option than messing with the filter. What if the user starts a word, then clicks the spinner, cancels it and goes back to the word? Now the autocompletion will not make sense, unless you save the filter and restore it somehow.
I do believe that two adapters (based on the same string array) is a better solution.
Related
PROBLEM
My problem is that after changing data in my SpinnerArrayAdapter my Spinner does not react to item clicks on dropDownList.
However after orientation change occurs everything is working fine(?!).
EDIT: I noticed it does catch the item clicks but not representing/showing it on Spinner. Because after orientation change the selected item appears on Spinner
CONSTRUCTION
I have AutoCompleteTextView(ACTV) that's connected to AutoCompleteAdapter implementing Filterable. After entering some data into ACTV the result is passed to SpinnerArrayAdapter that is connected to Spinner.
There is a customListener set on AutoCompleteAdapter that is connected to SpinnerArrayAdapter and responsible for passing data between them.
Reason behind such construction is that user can have a 2-step choice. One on drop-down when choosing the data from ACTV and second one in case he change his mind.
So you can put POSTCODE in the ACTV select province that's connected to and change province when you miss-clicked/changed mind without forcing to enter POSTCODE again.
CODE
This is the part that is responsible for data change inside SpinnerArrayAdapter.
#Override
public void setCitiesFromPostcode(ArrayList<String> cities) {
this.clear();
this.addAll(cities);
notifyDataSetChanged();
}
I had a similar problem with ArrayAdapter. I just changed it to BaseAdapter and it works. Don't really know what the reason is, but it's somewhere in the implementation of ArrayAdapter.
Two common causes for this:
Event though it may look big enough, if your Spinner is too small it may not be able to display the value, confirm this by hardcoding the Spinner width and height to something large. Using a custom spinner item layout may help if this is the problem.
You're using custom objects in your array, not simple Strings or numbers that can be converted to strings. Use a custom class MyAdapter extends BaseAdapter implements SpinnerAdapter { } class in this case.
When the app starts, I load some objects from the Device's SD Card and add them to a ListView.
Objects = new ArrayList<DataObject>();
Items = new ArrayList<MenuItem>();
Adapter = new MenuItemAdapter(this, Items);
listView.setAdapter(Adapter);
LoadObjects();
When I load objects I deserialize the object, check it's unique ID and if it's already in the list of Objects it should not be added. My method LoadObjects loads tries to load all files in a directory with a given extension. Then if successful, tries to load it into the Items list by using this method:
private boolean LoadObject(DataObject obj)
{
for (DataObject do : Objects)
{
if (do.GetID().equals(obj.GetId())
return false;
}
boolean added = Objects.add(do);
if (added)
{
Items.add(new MenuItem(do.GetID(), do.GetName());
Adapter.notifyDataSetChanged();
}
return added;
}
When this method is run "onCreate" it works fine, it loads in all the objects that can be loaded and is stored on the SD Card. When I try to add one after this method, the first item appears in the list where the new item should be added. I input a new name with a Dialog to create a new DataObject, which will later call LoadObject(new DataObject(name));
I've tried:
Resetting the Items adapter
Adding a "Add" method on the custom Adapter so that instead of adding to Items, I add through the Adapter by Adapter.Add(new DataObject(name)) which adds to it's own list; I also have a SetList adapter, to recreate the list without success.
Skip the notifyDataSetChanged
Cleared the Object list, Items list, Create a new Adapter each time - reloading all Objects, all MenuItem's, recreating the adapter and setting a new List - all with the same result.
Skip LoadObjects() and just let all newly added items appear, first one is correct - but whatever I add later shows up as the same as #1 from the ListView.
And I have to use FragmentActivity and not ListActivity because I have some Dialogs I need to show.
Simply put, I use the ListView to show specific Files from the SD Card to be able to load them onto another Activity later - but this hinders me from adding any new items without having to restart the Activity just to show new items, which means I need to be able to start out with an empty list and then
Probably becouse you use Adapter that have multiple references. Try to rename and don't name variables with uppercase.(see also import list to see what Adapter did you have there)
Your attempt #4 has to work. Give next loadObject() a shot:
private boolean LoadObject(DataObject obj)
{
for (DataObject do : Objects)
{
if (do.GetID().equals(obj.GetID())
return false;
}
Objects.add(obj);
Items.add(new MenuItem(obj.GetID(), obj.GetName());
Adapter = new MenuItemAdapter(this, Items);
listView.setAdapter(Adapter);
return true;
}
Seems that the problem occured due to a ListView being inside a RelativeLayout, altough I can't figure out why.
This is how my Layout was first:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ListView
android:id="#+id/main_menulist"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
</ListView>
</RelativeLayout>
When I instead created a new Layout with a ListView only, the problem no longer occured:
<?xml version="1.0" encoding="utf-8"?>
<ListView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/main_menulist"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</ListView>
I've only read about how you should not put a ListView in a ScrollView, but I've used this type of solution (perhaps not by dynamically adding items) before and it's worked.
I have form with a spinner control and a text box; and tables are stored in the SQLite database.
I am populating spinner inside onCreate method of activity. Is it right place to call database query & update ui?
Depending upon value user selects from spinner, i have to give auto suggestion in text box. (which control i can use instead of text box, i want to force user to select from list coming from database.)
Also i have heard about fragment, can anyone guide me is it helpful for improving performance of the application.
it would be better u call a function inside onCreate method and fill the spinner value in that function like
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.spinner);
addItemsOnSpinner1();
}
public void addItemsOnSpinner1() {
Spinner spinner = (Spinner) findViewById(R.id.spinnerSource);
List<String> list = new ArrayList<String>();
list.add("sony");
list.add("apple");
list.add("samsung");
list.add("htc");
list.add("blackberry");
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, list);
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
Collections.sort(list);
spinner.setAdapter(dataAdapter);
And for using fragment use this linkWhen should I use fragments in Android applications ? Why to use fragments?
You must populate your spinner with data from that moment on you want to provide it to the user. So just in the onCreate of the containing Activity.
For more info I guess Spinner | Android Developers .
Fragments give you the ability to create independent parts for your UI. For example a Fragment which contains a List and another showing detail information to the selected element. On Tablet you could display both beside each other. On a small display handheld device you have to display the list and on item selection the details. With Fragments you have reusability. Take a look here Fragments | Android Developers - I recommend to read this if you will use Fragments. It will save you the trouble.
I have list view. I need to set delete button to each row. Is there any standard way to
do this in android?
Thank you very much.
Android way is to not have a delete button but an action based on long press. So when you long press on the list it will show a list of stuff you can do and include delete in it. For example, you can check your Gmail, sms and long press on the thread to get this option.
You need to write your own list view item layout, which includes a delete button, see here for some details on that. In the button XMl you need to have the android:onclick attribute set, then in your code have an appropiate matching method. for example
<button android:onclick="deleteItem" android:text="Delete" ...>
public void deleteItem(View view) {
... code to delete...
}
You have to do xml file with single row (with button) then make class which extends some Adapter for example:
arrayAdapter = new MyArrayAdapter(this, R.layout.news_row, newsList)
where MyArrayAdapter extends ArrayAdapter, news_row is xml with view of single row, newsList is ArrayList with data
I've created an AutoCompleteTextView to search through a list of course titles (obtained from an sqlite db) and what I want to do is, when the user clicks on a title from the drop-down menu, the whole information from the database about his selection appears in a text view created below the AutoCompleteTextView.
I am pretty new to programming, especially for android and I would really appreciate it if someone could explain me how exactly to use setOnItemClickListener to call an instance in the database in the TextView below.
The code for the layout (R.layout.main_courses) is
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="5dp">
<AutoCompleteTextView
android:id="#+id/autocomplete_course"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="Search for a course"/>
<TextView
android:id="#+id/text"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#id/autocomplete_course"
android:hint="Information about the course will appear here" />
</RelativeLayout>
and the code for the AutoCompleteTextView I've written so far is:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_courses);
DataBase db = new DataBase(this.getApplicationContext());
db.openDataBase();
ArrayList<String> aCourses = db.getCoursesArr();
db.close();
AutoCompleteTextView search = (AutoCompleteTextView) findViewById(R.id.autocomplete_course);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.list_courses, aCourses);
search.setAdapter(adapter);
}
First of all you should try using a CursorAdapter instead of getting an array from it. Check this link for more info.
There is a method in AutoCompleteTextView that let you decide how many letters the user must type before the dropdown is shown, setThreshold. The issue is that it only allows >=1 values.
If you check this class src code, the good news is that the variable set by setThreshold() is only used in this method:
public boolean enoughToFilter() {
return getText().length() >= mThreshold;
}
So the first thing I would try is extending AutoCompleteTextView and override that method to always return true.
NOTE: Keep in mind that this might change in the future and it can get broken.