Setting up Spinner filled with data without any reference to Android resources - android

Busy creating an interface with spinner (dropdown) programmatically. To populate with data you need an (Array)Adapter. What I don't understand is why you need a reference to an Android resource at all, like "android.R.layout.simple_spinner_dropdown_item". Is it possible to populate the Spinner with webservice data, without any reference to such an Android resource? If not, why not and how should this resource look like, in an environment without layout's. If it is possible, please show me how, since google is no help here (to me).
Thanks in advance!
Update:
Spinner spinner = new Spinner(this.getActivity());
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this.getActivity(), whatComesHere, list);
spinner.setAdapter(dataAdapter);
tableRow.addView(spinner);
The questions is: what to substitute for 'whatComesHere'?
thanks in advance.
Update-2
<Spinner
android:id="#+id/spinner_admin_platform"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5dip"
android:layout_marginRight="5dip"
android:layout_marginTop="5dip"
android:layout_marginBottom="5dip"
android:entries="#array/my_spinner" />

Here is an example:
private void FillCitySnipper() {
ArrayList<MyCity> ListOfCity=db.LoadMyCity();
// TODO Auto-generated method stub
if (ListOfCity.size() > 0) {
Spinner spinner = (Spinner) this
.findViewById(R.id.spnr_Cityname);
ArrayAdapter<MyCity> spinnerArrayAdapter = new ArrayAdapter<MyCity>(
this,
android.R.layout.simple_spinner_dropdown_item,
ListOfCity);
spinner.setAdapter(spinnerArrayAdapter);
}
}

Related

Android Spinner not displaying array, but showing places

I've been trying to figure out how to make my spinner work for days straight now, and can't find anything to help me. My spinner doesn't show the values. The spinner opens up, and the spaces for each value is there, but they're all blank. I have tried using an ArrayList<String>, and also a String[] array, neither of which made a difference, but do I need to use one over the other? I have tried many different types of ArrayAdapters, but none of them have worked. The one in my code below is the one that works the closest, (as described above), and the other 3 are suggestions I saw online. I think my main problem is in my array adapter, but I'm not certain.
Here is my java class
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_info);
engineSpinner = (Spinner)findViewById(R.id.rockets);
engineNameList = StagesData.getEngineNameList();
engStrList = engineNameList.toArray(new String[engineNameList.size()]);
adapter = new ArrayAdapter<String>(InfoActivity.this, android.R.layout.simple_spinner_item, engStrList);
// adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, R.array.array_engines);
// adapter = new ArrayAdapter<String>(InfoActivity.this, android.R.layout.simple_dropdown_item_1line, R.id.rockets, engStrList);
// adapter = ArrayAdapter.createFromResource(InfoActivity.this, android.R.layout.simple_spinner_item, R.array.engines);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
engineSpinner.setAdapter(adapter);
engineSpinner.setSelection(0);
Here is my xml spinner setup
<TableRow>
<TextView
android:text="#string/rocket"
android:padding="3dp"/>
<Spinner
android:id="#+id/rockets"
android:gravity="right"
android:entries="#array/engines"
android:clickable="true"
android:textColor="#color/colorblack"
android:spinnerMode="dropdown"
android:backgroundTint="#color/colorPrimary" />
</TableRow>
And finally, this is my string-array file I wanted to pull from (Do I put my string-array in it's own file in the values folder, or does it go inside the string file, I've seen both and don't know which, Thanks)
<?xml version="1.0" encoding="utf-8">
<resources>
<string-array name="engines">
<item>Ant</item>
<item>Dart</item>
<item>Dawn</item>
<item>Flea</item>
<item>Hammer</item>
<item>Kickback</item>
<item>MainSail</item>
<item>Mammoth</item>
<item>Nerv</item>
<item>Poodle</item>
<item>Puff</item>
<item>R.A.P.I.E.R</item>
<item>Reliant</item>
<item>Rhino</item>
<item>Skipper</item>
<item>Spark</item>
<item>Spider</item>
<item>Swivel</item>
<item>Terrier</item>
<item>Thud</item>
<item>Thumper</item>
<item>Twin Boar</item>
<item>Twitch</item>
<item>Vector</item>
</string-array>
</resources>
So to recap my questions, do I need to use ArrayList<String> or String[], how do I set up my ArrayAdapter, and where do I need to put my string-array?
Thank you in advance!
I recreate your sample and works fine for me changing
engStrList = engineNameList.toArray(new String[engineNameList.size()]);
adapter = new ArrayAdapter<String>(InfoActivity.this, android.R.layout.simple_spinner_item, engStrList);
to
engStrList = new String[]{"a","b","c"};
adapter = new ArrayAdapter<String>(InfoActivity.this, android.R.layout.simple_spinner_item, engStrList);
So my guess is that you should check what StagesData.getEngineNameList() is returning.
Also if you what to use your string array this is the right way
ArrayAdapter adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, getResources().getStringArray(R.array.engines));
Hope this helps

What to use to put layout in activity Android?

I have an activity that uses setContentView to initiate a listview, and then I have an If-Else condition. In the If part of the statement I use a simpleAdapter to place a new layout in the listview and other data from a cursor. In the Else part, I just want to put a sentence in a label to provide some information. How can I do that? I tried parameters and textview but they didn't work. I tried to put another setcontentView but it can't work either.
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_listview);
doMySearch() is a cursor that extracts data from database based on a keyword input by user.
public void onPostExecute() {
SimpleCursorAdapter adapter;
Int count = doMysearch().getCount;
If (count >= 1){
adapter=new SimpleCursorAdapter(this, R.layout.activity_results,
doMySearch(), new String[] {
DB.COL_NAME,
DB.COL_CITY },
new int[] { R.id.lblName, R.id.lblCity },
0);
setListAdapter(adapter);
}
Else {
}
I just want a sentence that says "No data found" for the Else part. Thanks.
It is not clear for me what you are asking but here we go.
There is empty view setting in ListView for empty lists, you have to add another empty view in your current layout. Then you will set the empty view layout in code like below:
//add to your layout
<LinearLayout
android:id="#+id/empty"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="There is no item" >
</TextView>
</LinearLayout>
//set empty view
ListView listView = (ListView) container.findViewById(R.id.list);
LinearLayout emptyView = (LinearLayout) container.findViewById(R.id.empty);
listView.setEmptyView(emptyView);
As far as I understand the label doesn't show up because of the listview, right?`
EDIT:
Okay I think I get it now.
To keep it simple you could hide your ListView with myList.setVisibility(View.GONE);
Then make a premade Label visible with myLabel.setVisibility(View.VISIBLE) that already holds your error-string.
The harder way you could make a StringArray, like this in (for example) the strings.xml
<string-array name="error">
<item>No results found</item>
</string-array>
Then, in your else-block you can do somethink like that:
ListView lv = (ListView) findViewById(R.id.myList);
String[] content = getResources().getStringArray(R.array.error);
ArrayAdapter<String> typeList = new ArrayAdapter<String>(this, R.layout.simple_list_item_custom, content);
lv.setAdapter(typeList);

Superscript/subscript in ListView (android)

I'm new to android and I'm just learning.
I want some help on how to get superscript in ListView in android.
For now, I have a TextView which can make it by < sup > tag using Html.fromHtml() function to get e.g. 23.
But now I have just start learning to use ListView and don't know exactly how to use it, but < sup > tag doesn't work.
In onCreate I have this:
...
// Create and populate a history list
String[] history = new String[] {};
ArrayList<String> historyList = new ArrayList<String>();
historyList.addAll(Arrays.asList(history));
// Create ArrayAdapter using the history list.
listAdapter = new ArrayAdapter<String>(this, R.layout.history_list, historyList);
// Set the ArrayAdapter as the ListView's adapter.
historyListView.setAdapter(listAdapter);
And in onClick/AsyncTask/onPostExecute I have this:
...
listAdapter.add(outputToHistory);
In history_list.xml I have this:
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/textView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textIsSelectable="true" >
</TextView>
Thanks
According to this answer for a related question,
the tag should work:
((TextView)findViewById(R.id.text)).setText(Html.fromHtml("X<sup>2</sup>"));

Adding new elements to the top of the ListView

I'm writing an IM client and I need to download (from filesystem or network) and show new elements at the top of ListView (it is history of messages -- older messages are at the top, newer -- at the bottom). I implemented my own Adapter for ListView but I can't add new elements at the beginning of the list and redraw it. (notifyDataSetChanged() isn't good for me, because indexes of messages in ListView changes and android can't redraw it normally).
How do other apps do something similar?
I don't create special code for it, I am simply creating new Adapter for my ListView:
messagesListView.setAdapter(new MessagesListAdapter(this));
And redefine getView() and getCount() method in MessagesListAdapter (extends ArrayAdapter now).
My XML for ListView is
<ListView
android:id="#+id/dialog_messages_list"
android:layout_height="fill_parent"
android:layout_width="fill_parent"
android:layout_marginTop="#dimen/title_height">
</ListView>
And my XML for one element (one message) is
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:id="#+id/dialogMessageText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=""
android:background="#drawable/dialog_message_in"
/>
<TextView
android:id="#+id/dialogMessageDatetime"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text=""/>
</LinearLayout>
May be you need other code?
EDIT: I tried
messagesListView.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, arrayList));
(new Thread() {
public void run() {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
arrayList.add(0, "qwer");
}
}).start();
But it also not seems good. I tried to call ((ArrayAdapter<String>)messagesListView.getAdapter()).notifyDataSetChanged(); in thread, but it makes exception.
I suggest reversing the order of the List to display the newest result first.
Run this example:
public class Example extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
String[] array = {"oldest", "older", "old", "new", "newer"};
List<String> list = new ArrayList<String>();
Collections.addAll(list, array);
Collections.reverse(list);
// When you want to add new Strings, put them at the beginning of list
list.add(0, "newest");
ListView listView = (ListView) findViewById(R.id.list);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, list);
listView.setAdapter(adapter);
}
}
You don't have to override anything in the ArrayAdapter or ListView this way.
you can programmatically add some views in the class associated with your list view. For example:
To add stuff to a layout and make new elements:
TextView tv = new TextView(getApplicationContext());
EditText edit = new EditText(getApplicationContext());
relative.addView(tv);
relative.addView(edit);
This is to manipulate an existing element in the xml layout:
final TextView tv = (TextView) v.findViewById(R.id.listItem);
tv.setText("This an item I am changing");
If you look at some of the related questions, they will give your more information on this. But you can also checkout other people's custom listviews and adapters online. This one is really nice: http://www.androidhive.info/2012/02/android-custom-listview-with-image-and-text/

AutoCompleteTextView doesn't show suggestions on style/Theme.Dialog window

i have a problem with AutoCompleteTextView.
I use it in several activities, it works just fine with all of them but one, which is a dialog themed activity.
in this specific activity, the suggestion list doesn't show at all
this is my code:
<AutoCompleteTextView android:layout_height="wrap_content" android:id="#+id/etTesterName" android:completionThreshold="1" android:gravity="center" android:layout_width="fill_parent" android:layout_weight="1"></AutoCompleteTextView>
mTestersList = new ArrayList<Testers>();
mAdapter = new ArrayAdapter<Testers>(this, android.R.layout.simple_dropdown_item_1line, mTestersList);
((AutoCompleteTextView) findViewById(R.id.etTesterName)).setAdapter(mAdapter);
...
mTestersList = result;
mAdapter.notifyDataSetChanged();
mTestersList, is a list of Testers class which has the toString() overrided. this code works for all my activities but the dialog themed on..anyone has an idea why?
Vlad
hold reference of (AutoCompleteTextView) in some pointer that has visibility in activity:
AutoCompleteTextView autoCompleteTextView = (AutoCompleteTextView) find......
autoCompleteTextView.setAdapter(mAdapter);

Categories

Resources