Reputation: 2435
I am trying to change the listview text size (it seems to be pretty large by default) and am running into an error. I think the idea is to assign a textview to the listview and change the properties of that textview, but I am beginning to think I am going about this the wrong way. It would be greatly appreciated if somebody wouldn't mind glancing over what I am doing wrong. Thanks in advance!
Here is the error:
12-03 21:59:26.894 24751-24751/my.obd2connector E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: my.obd2connector, PID: 24751
java.lang.RuntimeException: Unable to start activity ComponentInfo{my.obd2connector/my.obd2connector.MyCar}: java.lang.ClassCastException: android.widget.ArrayAdapter cannot be cast to android.widget.TextView
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2305)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2363)
at android.app.ActivityThread.access$900(ActivityThread.java:161)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1265)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:157)
at android.app.ActivityThread.main(ActivityThread.java:5356)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1265)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1081)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.ClassCastException: android.widget.ArrayAdapter cannot be cast to android.widget.TextView
at my.obd2connector.MyCar.fillList(MyCar.java:120)
at my.obd2connector.MyCar.onStart(MyCar.java:92)
at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1189)
at android.app.Activity.performStart(Activity.java:5436)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2278)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2363)
at android.app.ActivityThread.access$900(ActivityThread.java:161)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1265)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:157)
at android.app.ActivityThread.main(ActivityThread.java:5356)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1265)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1081)
at dalvik.system.NativeStart.main(Native Method)
Here is the code to populate the listview where I am trying to change the size and color:
public void fillList() {
carList.add(make + " " + model + "\n" + vin);
mylistAdapter = new ArrayAdapter<String>(MyCar.this, android.R.layout.simple_list_item_single_choice, carList);
mylistView.setAdapter(mylistAdapter);
TextView tv = (TextView)mylistView.getAdapter();
tv.setTextColor(Color.RED);
tv.setTextSize(12);
mylistAdapter.notifyDataSetChanged();
}
Upvotes: 0
Views: 8265
Reputation: 56
You are on the right path.
mylistAdapter = new ArrayAdapter<String> ( this, android.R.layout, list );
Can also try
mylistAdapter = new ArrayAdapter<String> (this, android.R.layout.simple_listview_item, android.R.id.listTextView , list);
Upvotes: 0
Reputation: 36
If you want chnage list item's view size. You can do it in your xml file of listitem, and pass it to your adapter.
Note : TextView id should assign android.R.id.text1
Upvotes: 1
Reputation: 1428
Just change the default layout of the ListView to custom layout
Instead of using like this:
adapter = new ArrayAdapter<String>(this, android.R.layout.simple_listview_item, list);
you can use customLayout.xml:
adapter = new ArrayAdapter<String>(this, R.layout.customLayout, list);
customLayout.xml
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/listTextView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textColor="@color/black"
android:textSize="20dp" />
or you can use like this:
listView.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, MobileMuni.getBookmarkStore().getRecentLocations()) {
@Override
public View getView(int position, View convertView, ViewGroup parent) {
TextView textView = (TextView) super.getView(position, convertView, parent);
textView.setTextSize(12);
return textView;
}
});
Upvotes: 2
Reputation: 103135
The line:
TextView tv = (TextView)mylistView.getAdapter();
is causing the problem. You cannot cast an Adapter to a TextView. Instead you need to create a custom layout for your ListView and in there change the properties of the TextView that you use on the layout.
This tutorial covers details of creating a simple custom layout.
Upvotes: 4