AutoCompleteTextView Drop Down list shows empty textViews - android

I have written the following code for populating the AutoCompleteTextView from a MySQL database using internet. The database returns data and autocompletetextView adapter contains that data, however the drop down list shows empty cells.
if(suggestionList != null)
{
Toast.makeText(getApplicationContext(), "Contains Data", Toast.LENGTH_SHORT).show();
ArrayAdapter<String> adapter = new ArrayAdapter<String>(getApplicationContext(),android.R.layout.simple_dropdown_item_1line,suggestionList);
searchBox.setAdapter(adapter);
for(int i=0; i<suggestionList.length;i++)
System.out.println(suggestionList[i]);
}
NOTE:
SuggestionList is a String array and contains all the data
What can be the issue here?
Regards

Try this..
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, suggestionList);
searchBox.setAdapter(adapter);
searchBox.setThreshold(1);

As reference you could take a look here:
https://github.com/dentex/AutoCompleteTextView-with-SQlite
It stores user searches into the DB and retrieves them as you type.

Try adding items to your adapter manually.
For example:
adapter.add("Item 1");
Refer to documentation for adding items in the list view.

Related

replace an element from listadapter with an image

So I'm getting the data from my table and put it in a list. The data is showing ok. What I want to do now is: I have a field that display 0 or 1 and based on those values to display in the list 2 images: image 1- value 0; image 2-value 1. This is my code for displaying the data in the list:
ArrayList<HashMap<String, String>> userList = controller.getOld();
// If users exists in SQLite DB
if (userList.size() != 0) {
// Set the User Array list in ListView
ListAdapter adapter = new SimpleAdapter(OldTips.this, userList, R.layout.old_tips, new String[] {
"userId", "tara","ora_meci" ,"echipa_acasa","echipa_deplasare","cota","pronostic","explicatii","rezultat"}, new int[] { R.id.userId, R.id.tara , R.id.ora_meci, R.id.echipa_acasa, R.id.echipa_deplasare, R.id.cota,R.id.pronostic,R.id.explicatii,R.id.rezultat});
final ListView myList = (ListView) findViewById(android.R.id.list);
myList.setAdapter(adapter);
}
The fields are: rezultat is showing up in the R.id.rezultat. In the R.layout.old_tips I defined 2 images(A and B) that are invisible. Now I want if rezultat is 1 to make the image A visible and if 0 to make the image B visible.I have no ideea how to obtain this that's why I didn't try anything. I don't have any idea. Any help would be much appreciated. Thanks.
You need to use a Custom Adapter for your list. I recommend you the use of RecyclerView instead of ListView. Have better performance and you can find a lot of tutorials on Internet.
Good luck!

Spinner appears empty but contains data

I'm having an issue with my spinner. When I debug, I can see that there is data : my adapter contains objects.
Debug spinner adapter screenshot
However, the spinner appears like it has nothing in it :
My empty spinner
Here is the (supposed) interesting code :
`
ArrayList<ActivityToSteps> activityConversionList;
ArrayList<String> activityList;
ArrayAdapter<String> categories_adapter;
activityConversionList = new ArrayList<>();
activityList = new ArrayList<>();
categories_adapter = new ArrayAdapter<>(this, android.R.layout.simple_spinner_item, activityList);
categories_adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
activityConversionList.addAll((ArrayList<ActivityToSteps>)task.getResult());
for(ActivityToSteps ats : activityConversionList){
activityList.add(ats.getActivityName());
}
categories_adapter.notifyDataSetChanged();
`
ActivityToSteps is a class I created, containing a String "ActivityName" attribute and a float "StepsPerMin" attribute.
I'm getting a list of ActivityToSteps from an async task, and I want a spinner containing all their "ActivityName", stored in the ArrayList "activityList" (which is not empty, but can't show screenshot beacause reputation < 10...).
I hope I've been clear enough!
Thanks in advance for your time!!
Please double check that you have passed your list to spinner adapter.
your_categories_spinner.setAdapter(categories_adapter);
just forgot to set the adapter to the spinner..
spinner.setAdapter(categories_adapter);

To set the positions of items in spinner

I am populating my spinner from database. I have a collection of mobile brands. I have added "Add a new brand" also... But when I am setting the spinner items from DB, it comes in somewhere middle.. I want it to go at the end.. Can i do it? if yes, how? please help, thanks in advance.
Spinner brand;
brand=(Spinner)findViewById(R.id.spinner_brand);
private void loadSpinnerData() {
// database handler
DatabaseHandler db = new DatabaseHandler(getApplicationContext());
// Spinner Drop down elements
List<String> Brand = db.getBrands();
// Creating adapter for spinner
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, Brand);
// Drop down layout style - list view with radio button
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
// attaching data adapter to spinner
brand.setAdapter(dataAdapter);
}
Add time column in your brand table and on querying sort by time. So, you will get last added item at last in spinner.
Create an ArrayList and add all your database items to it. and then add your "Choose a brand" string to it. and then pass the ArrayList to Spinner.
Although I should warn you, you are "probably" doing it wrong. If you add "Choose a brand" item to spinner, it will also be selectable, which you might not want :)

Spinner data sorting in Android

I have spinner with array list thats work fine, but i want to sort out the datas from a to z (example: apple,ball,cat,dog...)order. I submit my code below
ArrayList<String> SourceArray = new ArrayList<String>();
Spinner Sourcespinner;// = new Spinner(this);
Sourcespinner = (Spinner)findViewById(R.id.Spinner1);
ArrayAdapter<String> SourceArrayAdapter = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item,SourceArray);
SourceArrayAdapter.add("Chennai");
SourceArrayAdapter.add("Mumbai");
SourceArrayAdapter.add("Kolkatta");
SourceArrayAdapter.add("Delhi");
Sourcespinner.setAdapter(SourceArrayAdapter);`
I don't know how to do sorting for this
you can use this to sort your data
Collections.sort(SourceArray);
Try to add data to the ArrayList and just use the Collections class to sort for you:
Collections.sort(SourceArray);
If you need to add your own objects they need to implement the Comparable interface and implement the method compareTo(). When changing the ArrayList's data make sure to notify the adapter that new data might have been added by using this code:
SourceArrayAdapter.notifyDataSetChanged();

How do you add array to listview

I have an array of apps(PInfo) and I am wondering how do I add that array to a listview?
ArrayList<PInfo> info = appsGetter.listPackages();
int number = 0;
PInfo appInArray;
while(number < info.size()){
appInArray = info.get(number);
}
This is what I have at the moment, the listPackages() is a method that is getting the names of the apps from the device.
At the moment I am trying to get the information out of the array one by one and add it to the listview like that. Is that how I should do it our should I add the array straight to the listview? And how do you do that?
You can use an ArrayAdapter and initialize it like this:
ArrayAdapter<PInfo> adapter = new ArrayAdapter(context,
android.R.layout.simple_list_item_multiple_choice,
info);
Then you can you use ListView.setAdapter(adapter).
I'm not sure if this is what you're asking though. So please clarify further if this is not what you're asking
Try using an Adapter. For example (using just the String value of an object) you could do the following:
ListView listView = (ListView)findViewById( R.id.myListView );
final ArrayList<String> listItems = new ArrayList<String>();
final ArrayAdapter<String> adapter = new ArrayAdapter<String>( this, android.R.layout.simple_list_item_1, listItems );
listView.setAdapter( adapter );
Just a quick example, but I hope it gives you a starting place. Just make sure if you add values to your data source later (in this case the ArrayList) to call the adapter's "notifyDataSetChanged()" method so that it can be properly reflected in whatever has been bound to the adapter (in this case the ListView).
You need to use an ArrayAdapter. Just search for a ListView and ArrayAdapter sample online. It's quite simple once you see it done.

Categories

Resources