2014-12-04 175 views
0

我正在尝试更改listview文本大小(默认情况下它看起来非常大)并且正在运行到错误中。我认为这个想法是给列表视图分配一个文本视图并更改该文本视图的属性,但我开始认为我正在以这种错误的方式进行。如果有人不介意看看我做错了什么,我将不胜感激。提前致谢!在android中更改列表视图的文字大小

以下是错误:

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) 

这里是填充在那里我试图改变大小和颜色的列表视图代码:

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(); 
    } 
+1

您正在使用的适配器? – 2014-12-04 04:03:04

+0

其中:public ArrayAdapter mylistAdapter;我需要这个来填充字符串的ListView。 – Shawn 2014-12-04 04:03:58

回答

4

行:

TextView tv = (TextView)mylistView.getAdapter(); 

正在导致此问题。您不能将Adapter转换为TextView。相反,您需要为您的ListView创建一个自定义布局,并在那里更改您在布局上使用的TextView的属性。

This tutorial涵盖了创建一个简单的自定义布局的细节。

+0

我当然希望当我第一次开始大声笑时看到本教程,谢谢先生。 – Shawn 2014-12-04 04:12:24

2

只需更改ListView控件的默认布局,自定义布局

而不是使用像这样的:

adapter = new ArrayAdapter<String>(this, android.R.layout.simple_listview_item, list); 

您可以使用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" /> 

,或者你可以使用这样的:

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; 
} 

});

+0

我试过这个......每当我做出我的自定义布局时,当我键入android.R.layout时它不显示。它似乎只显示simple_listview_item和其他人。是否有某个文件夹用于识别它? – Shawn 2014-12-04 04:11:49

+1

你可以使用第二个例子 – Ram 2014-12-04 04:17:58

+0

我会在调用setllapter()后,在filllist方法中调用该方法吗? – Shawn 2014-12-04 04:20:55

1

如果你想chnage列表项的视图大小。您可以在listitem的xml文件中执行此操作,并将其传递到适配器。

注: TextView的ID应该分配android.R.id.text1

0

你在正确的道路上。

mylistAdapter = new ArrayAdapter<String> (this, android.R.layout, list); 

也可以尝试

mylistAdapter = new ArrayAdapter<String> (this, android.R.layout.simple_listview_item, android.R.id.listTextView , list); 
相关问题