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>"));
Related
I tried a few different layouts to get deeper in possibilities and variances.
I started with an array to display the items in an listview that worked fine.
Now I wanted to display items that I got out of a database via JSON.
I get the following error: You must supply a resource ID for a TextView
I used the same XML-file, that worked before, here my all_products.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
android:orientation="vertical">
<ListView
android:id="#+id/mobile_list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:divider="#color/colorPrimaryDark"
android:dividerHeight="1px">
</ListView>
</LinearLayout>
In my java class I used the code that I used before for the array adapter, I changed only the parameter which should be displayed. Here part of my AllProductsActivity.java:
private void processValue(ArrayList<String> result) {
{
ArrayAdapter adapter = new ArrayAdapter<String>(AllProductsActivity.this, R.layout.all_products, result);
ListView listView = (ListView) findViewById(R.id.list);
listView.setAdapter(adapter);
}
}
result comes from my asynctask. here a snippet as well:
JSONArray jsonArray = new JSONArray(result.toString())
ArrayList<String> listdata = new ArrayList<String>();
for(int n = 0; n < jsonArray.length(); n++)
{
JSONObject object = jsonArray.getJSONObject(n);
listdata.add(object.optString("nr"));
}
return listdata;
protected void onPostExecute( ArrayList<String> result) {
pdLoading.dismiss();
processValue(result);
}
Why I get the error? And perhaps what about using only a Textview. As I was searching for the toppic, I found different threats where people using Textviews instead of Listview.
EDIT: So it works I added another xml file, mytextview.xml
<TextView android:id="#+id/mytext"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:divider="#color/colorPrimaryDark"
android:dividerHeight="1px"
xmlns:android="http://schemas.android.com/apk/res/android">
and changed the adapterstatement to the following:
ArrayAdapter adapter = new ArrayAdapter<String>(AllProductsActivity.this,
R.layout.mytextview,R.id.mytext , result);
ListView listView = (ListView) findViewById(R.id.mobile_list);
listView.setAdapter(adapter);
when you use custom layout R.layout.all_products then Adapter don't know about the view to set the data from data list
so simply you need to tell the adapter , the ID of your text view to set data on .
ArrayAdapter adapter = new ArrayAdapter<String>
(AllProductsActivity.this,
R.layout.all_products,R.id.your_text_view_id, result);
// ^^^^^^^^^ pass the text view ID
ArrayAdapter (Context context,
int resource,
int textViewResourceId,
T[] objects)
or
If you just want to display your data without any custom layout then can use in build android resource layout as
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,result);
in Pavneet's first solution take care that:
resource: is the resource containing a textView not your xml file that contain the list itself (not R.layout.all_products)
textViewResourceId: the ID of the textView contained in the resource
When instantiating your ArrayAdapter, the id you provide must be from a layout containing only a TextView. That is what the error You must supply a resource ID for a TextView means
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
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);
}
}
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);
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/