create an Android Scroll List programmatically - java

Coming from a web dev background, what I'm looking for should be quite simple. I have pulled all the contacts from the android phone.
Their names and numbers are in a HashMap called contacts (number = key) I'm iterating through them and I'm looking to create a list of them for the user to see. The key (the phone number) must be available there but not be seen, and The List must scroll. So something like the <option value="phone">Name</option> would be perfect. I'm stuck. Any ideas?

Use an adapter to inflate the list into ListView.
Like:
public class SimpleListView extends ListActivity {
private String[] lv_arr = {};
private ListView mainListView = null;
final String SETTING_TODOLIST = "todolist";
private ArrayList<String> selectedItems = new ArrayList<String>();
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.simple);
// Prepare an ArrayList of todo items
ArrayList<String> listTODO = [INSERT THE VALUES FROM THE CONTACTS HERE];
this.mainListView = getListView();
// Bind the data with the list
lv_arr = listTODO.toArray(new String[0]);
mainListView.setAdapter(new ArrayAdapter<String>(SimpleListView.this,
android.R.layout.simple_list_item_2, lv_arr));
}
}
You also need a layout:
simple.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="450dp" >
<ListView
android:id="#+id/mainListView"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_above="#+id/size"
android:layout_below="#+id/editText1"
android:gravity="fill_vertical|fill_horizontal"
android:horizontalSpacing="15dp"
android:isScrollContainer="true"
android:numColumns="1"
android:padding="5dp"
android:scrollbars="vertical"
android:smoothScrollbar="true"
android:stretchMode="columnWidth" >
</ListView>
</RelativeLayout>

Related

Android: java.lang.ClassCastException [duplicate]

I am getting an error when trying to set my view to display the ListView for the file I want to display(text file). I am pretty sure it has something to do with the XML. I just want to display the information from this.file = fileop.ReadFileAsList("Installed_packages.txt");. My code:
public class Main extends Activity {
private TextView tv;
private FileOperations fileop;
private String[] file;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.fileop = new FileOperations();
this.file = fileop.ReadFileAsList("Installed_packages.txt");
setContentView(R.layout.main);
tv = (TextView) findViewById(R.id.TextView01);
ListView lv = new ListView(this);
lv.setTextFilterEnabled(true);
lv.setAdapter(new ArrayAdapter<String>(this, R.layout.list_item, this.file));
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
// When clicked, show a toast with the TextView text
Toast.makeText(getApplicationContext(), ((TextView) view).getText(), Toast.LENGTH_SHORT).show();
}
});
setContentView(lv);
}
}
list_item.xml :
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:padding="10dp"
android:textSize="16sp"
android:textColor="#000">
</LinearLayout>
main.xml :
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:weightSum="1">
<ScrollView
android:id="#+id/SCROLLER_ID"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:scrollbars="vertical"
android:fillViewport="true">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="5sp"
android:id="#+id/TextView01"
android:text="#string/hello"/>
</ScrollView>
</LinearLayout>
The ArrayAdapter requires the resource ID to be a TextView XML exception means you don't supply what the ArrayAdapter expects. When you use this constructor:
new ArrayAdapter<String>(this, R.layout.a_layout_file, this.file)
R.Layout.a_layout_file must be the id of a xml layout file containing only a TextView(the TextView can't be wrapped by another layout, like a LinearLayout, RelativeLayout etc!), something like this:
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
// other attributes of the TextView
/>
If you want your list row layout to be something a little different then a simple TextView widget use this constructor:
new ArrayAdapter<String>(this, R.layout.a_layout_file,
R.id.the_id_of_a_textview_from_the_layout, this.file)
where you supply the id of a layout that can contain various views, but also must contain a TextView with and id(the third parameter) that you pass to your ArrayAdapter so it can know where to put the Strings in the row layout.
Soution is here
listitem.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="#+id/textview"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</TextView>
</LinearLayout>
Java code :
String[] countryArray = {"India", "Pakistan", "USA", "UK"};
ArrayAdapter adapter = new ArrayAdapter<String>(this, R.layout.listitem,R.id.textview, countryArray);
ListView listView = (ListView) findViewById(R.id.listview);
listView.setAdapter(adapter);
If you are getting that message when you are extending an ArrayAdapter, you are getting that error because you have not provided the correct resource id to display the item. Call the super class in the constructor and pass in the resource id of the TextView:
//Pass in the resource id: R.id.text_view
SpinnerAdapter spinnerAddToListAdapter = new SpinnerAdapter(MyActivity.this,
R.id.text_view,
new ArrayList<>());
Adapter:
public class SpinnerAdapter extends ArrayAdapter<MyEntity> {
private Context context;
private List<MyEntity> values;
public SpinnerAdapter(Context context, int textViewResourceId,
List<MyEntity> values) {
//Pass in the resource id: R.id.text_view
super(context, textViewResourceId, values);
this.context = context;
this.values = values;
}
The problem is in the line:
lv.setAdapter(new ArrayAdapter<String>(this, R.layout.list_item, this.file));
Because when i change it to:
lv.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, this.file));
It works!

Add icons to Listview using Arraylist

I have successfully created a listview that contains all the options (text). However I would like to add unique icons next to each of the options. How can I go about doing this, with my excising code?
Here is what I am trying to achieve:
Here is my code:
AccountSettingsActivity.java
//All Options in Account Settings
private void setupSettingsList(){
ListView listView = findViewById(R.id.lvAccountSettings);
ArrayList<String> options = new ArrayList<>();
options.add(getString((R.string.editProfile)));
options.add(getString(R.string.notifications));
options.add(getString(R.string.privacy_settings));
options.add(getString(R.string.security));
options.add(getString(R.string.ads));
options.add(getString(R.string.help));
options.add(getString(R.string.about));
options.add(getString(R.string.logout));
ArrayAdapter adapter = new ArrayAdapter(mContext, R.layout.listview_row_adjustment, options);
listView.setAdapter((adapter));
}
listview_row_adjustmnet.xml (This simply changes the text color and size of the options in the listview)
<TextView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/listViewAdjustment"
android:layout_width="fill_parent"
android:layout_height="50dp"
android:textSize="17sp"
android:gravity="fill"
android:textColor="#color/white"/>
Create Array in /res folder:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<array name="images">
<item>#drawable/img1</item>
<item>#drawable/img2</item>
<item>#drawable/img3</item>
</array>
</resources>
update Account Setting:
//All Options in Account Settings
private void setupSettingsList(){
ListView listView = findViewById(R.id.lvAccountSettings);
TypedArray images = getResources().obtainTypedArray(R.array.images);
ArrayList<String> options = new ArrayList<>();
options.add(getString((R.string.editProfile)));
options.add(getString(R.string.notifications));
options.add(getString(R.string.privacy_settings));
options.add(getString(R.string.security));
options.add(getString(R.string.ads));
options.add(getString(R.string.help));
options.add(getString(R.string.about));
options.add(getString(R.string.logout));
// add images to adapter
ArrayAdapter adapter = new ArrayAdapter(mContext, R.layout.listview_row_adjustment, options, images);
listView.setAdapter((adapter));
}
Add Image View in listview_row_adjustmnet.xml
in Adapter set the image:
imageView.setImageResource(images.getResourceId(i, -1));
Use a data model with title and icon like below
class Data {
public int imageId;
public String txt;
Data(Drawable imageId, String text) {
this.imageId = imageId;
this.txt = text;
}
}
Data menuItemSearch = new Data(ContextCompat.getDrawable(this, R.drawable.ic_search_orange), resources.getString(R.string.title))
and when set drawable in image use this one
yourImage.setImageDrawable(data.imageId)
Firstly, You have to add in your listview_row_adjustmnet.xml.
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/imageView"
android:layout_gravity="center_horizontal|top"/>
<TextView
android:id="#+id/listViewAdjustment"
android:layout_width="fill_parent"
android:layout_height="50dp"
android:textSize="17sp"
android:gravity="fill"
android:textColor="#color/white"/>
And for Code AccountSettingActivity class:
private void setupSettingsList(){
ListView listView = findViewById(R.id.lvAccountSettings);
ArrayList<String> options = new ArrayList<>();
options.add(getString((R.string.editProfile)));
options.add(getString(R.string.notifications));
options.add(getString(R.string.privacy_settings));
options.add(getString(R.string.security));
options.add(getString(R.string.ads));
options.add(getString(R.string.help));
options.add(getString(R.string.about));
options.add(getString(R.string.logout));
ArrayList<ImageView> images = new ArrayList<>();
//Please add Customized addapter
}
for the customized adapter, you find help in this Link https://www.youtube.com/watch?v=_YF6ocdPaBg.
To set an ImageView in code Please see this answer Using variable to change image in Java.

Android - Display Query's Result in an Activity

I have the result of a sql query expressed in this way :
//The column attributes of a result table
ArrayList<String> columns_attributes;
// This contains the data of every row of the result
ArrayList<ArrayList<String>> rows_data;
How can i dinamically display it on an activity ? Thanks
MainActivity
public class MainActivity extends AppCompatActivity {
List<String> list = new ArrayList<String>();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
list.add("Android");
list.add("iPhone");
list.add("Windows");
list.add("Blackberry");
list.add("Mac");
list.add("Laptop");
list.add("LCD");
list.add("Dell");
ArrayAdapter adapter = new ArrayAdapter<String>(MainActivity.this, R.layout.list_view_item, list);
ListView listView = (ListView) findViewById(R.id.mobile_list);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(MainActivity.this, "Clicked: " + list.get(position), Toast.LENGTH_SHORT).show();
}
});
listView.setAdapter(adapter);
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.vzw.www.listviewalert.MainActivity">
<ListView
android:id="#+id/mobile_list"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</RelativeLayout>
list_view_item.xml
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/label"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="10dip"
android:textSize="16dip"
android:textStyle="bold" >
</TextView>
You can iterate over these list with simple for loops like this:
// for columns
for (String colAttr : columns_attributes) {
// do something with the string value
}
// for rows
for (List<String> row : rows_data) {
for (String rowAttr : row) {
// do something with the string value
}
}
To display a dynamic amount of data, a ListView is recommended to do the job. If you just want to display those strings, you can use a ListActivity and assign a ArrayAdapter to that ListView with the setListAdapter() method.
You may want to check this tutorial to get this done.
http://androidexample.com/Create_Listview_With_ListActivity_-_Android_Example/index.php?view=article_discription&aid=66

Displaying ListView on Android

How do I create a working ListView in Android?
I am not looking for you to just fix my code, but am looking for a simple working example of a ListView in Android so I can understand the process of creating one and working with it. But I have included my code so you can see where I am coming from and what I have been trying.
I have done the following and had no success:
--
Made a xml layout with only a TextView item in it:
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="#+id/dir_text_view"
/>
Created the following class as per the instructions at the following tutorial:
http://www.vogella.com/tutorials/AndroidListView/article.html
public class DataTempleArrayAdapter extends ArrayAdapter<String> {
HashMap<String, Integer> mIdMap = new HashMap<String, Integer>();
public DataTempleArrayAdapter(Context context, int textViewResourceId,
List<String> objects) {
super(context, textViewResourceId, objects);
for (int i = 0; i < objects.size(); ++i) {
mIdMap.put(objects.get(i), i);
}
}
#Override
public long getItemId(int position) {
String item = getItem(position);
return mIdMap.get(item);
}
#Override
public boolean hasStableIds() {
return true;
}
}
And in the main activity I have a snippet of code where I attempt to add a list of strings to the ArrayList associated with the DataTempleArrayAdapter here:
int i;
for (i=0;i<dirContents.length;i++) {
dirList.add(dirContents[i]);
//Toast.makeText(this, dirList.get(i), Toast.LENGTH_SHORT).show();
}
adapter.notifyDataSetChanged();
dirList is successfully populated, while the adapter doesn't update the ListView at all.
--
Before you ask for it, here I am including the rest of the relevant code:
activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context="org.hacktivity.datatemple.MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="text"
android:hint="#string/directory"
android:ems="10"
android:layout_alignParentTop="true"
android:layout_alignParentStart="true"
android:id="#+id/dirEditText" />
<Button
android:text="→"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_alignParentStart="true"
android:id="#+id/dirButton"
android:onClick="populateDirList" />
</LinearLayout>
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:id="#+id/dirListView" />
</LinearLayout>
</RelativeLayout>
And alas the MainActivity class:
public class MainActivity extends AppCompatActivity {
ListView dirListView;
EditText et;
DataTempleArrayAdapter adapter;
ArrayList<String> dirList;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
dirListView = (ListView) findViewById(R.id.dirListView);
et = (EditText) findViewById(R.id.dirEditText);
dirList = new ArrayList<String>();
dirListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
Toast.makeText(getApplicationContext(),
"Click ListItem Number " + position, Toast.LENGTH_LONG)
.show();
populateDirList(view);
}
});
ArrayList<String> dirList = new ArrayList<String>();
adapter = new DataTempleArrayAdapter(this,
R.id.dir_text_view, dirList);
dirListView.setAdapter(adapter);
}
public void populateDirList (View view) {
File f;
// NO INPUT.
if (et.getText().toString().equals("")) {
Toast.makeText(this, "empty string", Toast.LENGTH_SHORT).show();
return;
}
f = new File(et.getText().toString());
if (f == null) { return; }
String dirContents[] = f.list();
if (dirContents == null) { return; }
dirList = new ArrayList<String>(Arrays.asList(f.list()));
adapter.clear();
int i;
for (i=0;i<dirContents.length;i++) {
dirList.add(dirContents[i]);
//Toast.makeText(this, dirList.get(i), Toast.LENGTH_SHORT).show();
}
adapter.notifyDataSetChanged();
}
}
One of the best resources for understanding ListView is indeed the
one you mentioned from Vogella
Another cool resource to understand how the the
notifyDataSetChanged() method works in ListView this post from StackOverflow
For a short, simple explanation of how to use CustomLayouts in
ListView (without the ViewHolder pattern) check another of the best
references available: Mkyong
Discussing the benefits of the ViewHolder pattern in ListView:
check this StackOverflow post
Concise example and explanation of the ViewHolder pattern in
ListView: check this example from JavaCodeGeeks
And to fix your code I think the answer given before is only part of the problem:
You must indeed comment the line
//ArrayList<String> dirList = new ArrayList<String>();
because, like #F43nd1r mentioned this would also be a different instance of a list passed into the adapter
but there is more, when you do this:
dirList = new ArrayList<String>(Arrays.asList(f.list()));
you are instantiating a new, different, list, the old reference held by the adapter will NOT be changed... it will still hold the OLD object list
you should perhaps substitute it for something like:
dirList.clear();
dirList.addAll(Arrays.asList(f.list()));
Hope this helps!
Excerpt from your code:
#Override
protected void onCreate(Bundle savedInstanceState) {
//...
dirList = new ArrayList<String>();
//...
ArrayList<String> dirList = new ArrayList<String>();
adapter = new DataTempleArrayAdapter(this,
R.id.dir_text_view, dirList);
//...
}
I bet you already see what the problem is, but in case you don't: You have a field and a local variable with the same name. You pass the local variable to the adapter. It is only naturally that the adapter does not react to changes on the field, as it has no knowledge of its existence.
I think what you have done wrong is to supply a UI Component to the Array Adapter with:
adapter = new DataTempleArrayAdapter(this, R.id.dir_text_view, dirList);
The second item should not be an ID, but a layout file. Android have already implemented a List item layout with a textview that you can use: android.R.layout.simple_list_item_1.
so replace your row with
adapter = new DataTempleArrayAdapter(this, android.R.layout.simple_list_item_1, dirList);
and you are one step closer.
(This way you don't need your "xml layout with only a TextView item in it")

ArrayList Adapter (for ListView) only displaying the first added item

Edit3: My own item list layout:
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#android:id/text1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceListItemSmall"
android:gravity="center_vertical"
/>
Edit2: Honestly, I might just make a for-loop that creates buttons for each file. This is way too much of a headache to be worth the time.
Edit: I'd like to emphasize the fact that I've copy+pasted my exact code into a new test app and it works fine. That might give you guys a clue.
After tons of debugging, I've narrowed down a problem. I'm trying to add items (files) to an ArrayList, then put that into an ArrayAdapter, and finally display the items in a ListView. The problem is that only the first added item is being displayed.
Here's how I was trying to do it:
ListView listView = (ListView) view.findViewById(R.id.templateFilesList);
ArrayList<String> templateList = new ArrayList<>();
File myDir = getContext().getFilesDir();
File[] templateFiles = myDir.listFiles();
int length = templateFiles.length;
for(int i = 0; i < length; i++){
templateList.add(templateFiles[i].getName());
}
ArrayAdapter arrayAdapter = new ArrayAdapter<>(getContext(), android.R.layout.simple_list_item_1,
templateList);
listView.setAdapter(arrayAdapter);
The templateFiles[] array properly gets the 8 files in Internal Storage (confirmed via logcat/debugger), but only the first item is displayed in the ListView. Here's a clearer look at the issue:
// If I replace the for loop with this:
templateList.add(templateFiles[1].getName());
templateList.add(templateFiles[0].getName());
Only templateFiles[1] is displayed. Similarly, if I do this:
templateList.add(templateFiles[0].getName());
templateList.add(templateFiles[1].getName());
Only templateFiles[0] is displayed. Any ideas on what's going wrong?
Here's my XML:
<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"
tools:context="com.template_housing.MyTemplatesFrag"
android:orientation="vertical"
android:gravity="center_horizontal"
>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/my_templates"
android:textSize="34sp"
android:textColor="#color/black"
android:background="#color/Gold1"
android:gravity="center_horizontal"
android:padding="5dp"
android:layout_marginBottom="10dp"
/>
<ListView
android:id="#+id/templateFilesList"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
</ListView>
If you are updating the content of your adapter you should be calling notifyDataSetChanged() as well to make sure your adapter gets to know that your content did change
I copied your code and ran it in Android Studio. It seems NO problem.
Here is my activity code(same xml code), the only difference is that I use an Activity, you may use a Fragment.
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ListView listView = (ListView) findViewById(R.id.templateFilesList);
ArrayList<String> templateList = new ArrayList<>();
File myDir = getFilesDir();
File[] templateFiles = myDir.listFiles();
int length = templateFiles.length;
//case 1: in my project, length is 1, show only one element, see case2.
// for (int i = 0; i < length; i++) {
// templateList.add(templateFiles[i].getName());
// }
//case 2: try to add simple strings,
//because you said put simple strings in the list could cause the problem, too.
templateList.add("a1");
templateList.add("a2");
templateList.add("a3");
ArrayAdapter arrayAdapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1,
templateList);
listView.setAdapter(arrayAdapter);
}
}
I shared my code on GitHub.I strongly recommend you post your fully code on Github, let's review your code.
Try to convert it to an array of Strings:
String [] templateListArray = templateList.toArray(new String[templateList.size()]);
and then do the assignment as follows:
ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(getContext(), android.R.layout.simple_list_item_1,
templateListArray);
Hope this will fix it.
Can you post the simple list item 1 XML file too. This issue is not Java code related it has something to do with XML and size of elements. Try deleting that text view on top of the list view and your simple list item view should have wrap content as layout height.
And if you still want to use the textview you can use weight attribute. Give your textview a weight of 0 and the list view a weight of 1.
It may be possible that your array adapter is not setting properly and therefore it is not working properly, so try to replace your array adapter by below code and check if it is working,
ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, android.R.id.text1, templateList);

Categories