【问题标题】:Changing from ArrayAdapter<String> with a textView to ArrayAdapter<String> with LinearLayout. Class cast exception从带有 textView 的 ArrayAdapter<String> 更改为带有 LinearLayout 的 ArrayAdapter<String>。类转换异常
【发布时间】:2014-04-15 07:23:53
【问题描述】:

我的代码目前在布局文件中使用带有单个 TextView 的 ArrayAdapter,并且一切正常。

我目前的布局如下(R.layout.list_header):

<TextView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/list_header_title"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:paddingTop="2dip"
android:paddingBottom="2dip"
android:paddingLeft="5dip"
android:textSize="16sp"
android:textColor="#ffffff"
style="?android:attr/listSeparatorTextViewStyle" />

我以这种方式创建我的适配器:

new ArrayAdapter<String>(context, R.layout.list_header);

现在,我想在我的适配器中添加一张图片,在我的 textView 的左侧。我首先查看了这个线程,它帮助我使用的不仅仅是 textView: "ArrayAdapter requires the resource ID to be a TextView" xml problems

我的新布局现在如下:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >    

<ImageView
    android:id="@+id/list_header_picture"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:contentDescription="line_logo" />

<TextView
    android:id="@+id/list_header_title"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:paddingTop="2dip"
    android:paddingBottom="2dip"
    android:paddingLeft="5dip"
    android:textSize="16sp"
    android:textColor="#ffffff"
    style="?android:attr/listSeparatorTextViewStyle" />

</LinearLayout>

适配器是这样创建的:

new ArrayAdapter<String>(context, R.layout.list_header, R.id.list_header_title);

这就是我得到错误 java.lang.ClassCastException:android.widget.LinearLayout$Params 不能转换为 android.widget.AbsListView$LayoutParams 的地方。 我看不出这里有什么问题。我试图删除 imageView,但这并没有改变任何东西。

知道什么可能导致问题吗?

编辑:

这是我的适配器类:

public class SeparatedListAdapter extends BaseAdapter
{
public final Map<String, Adapter> sections = new LinkedHashMap<String, Adapter>();
public final ArrayAdapter<String> headers;
public final static int TYPE_SECTION_HEADER = 0;

public SeparatedListAdapter(Context context) {
    headers = new ArrayAdapter<String>(context, R.layout.list_header);//, R.id.list_header_title);
}

public void addSection(String section, Adapter adapter) {
    this.headers.add(section);
    this.sections.put(section, adapter);
}

public Object getItem(int position) {
    for (Object section : this.sections.keySet())
    {
        Adapter adapter = sections.get(section);
        int size = adapter.getCount() + 1;

        // check if position inside this section
        if (position == 0) return section;
        if (position < size) return adapter.getItem(position - 1);

        // otherwise jump into next section
        position -= size;
    }
    return null;
}

public int getCount() {
    // total together all sections, plus one for each section header
    int total = 0;
    for (Adapter adapter : this.sections.values())
        total += adapter.getCount() + 1;
    return total;
}

@Override
public int getViewTypeCount() {
    // assume that headers count as one, then total all sections
    int total = 1;
    for (Adapter adapter : this.sections.values())
        total += adapter.getViewTypeCount();
    return total;
}

@Override
public int getItemViewType(int position) {
    int type = 1;
    for (Object section : this.sections.keySet())
    {
        Adapter adapter = sections.get(section);
        int size = adapter.getCount() + 1;

        // check if position inside this section
        if (position == 0) return TYPE_SECTION_HEADER;
        if (position < size) return type + adapter.getItemViewType(position - 1);

        // otherwise jump into next section
        position -= size;
        type += adapter.getViewTypeCount();
    }
    return -1;
}

public boolean areAllItemsSelectable() {
    return false;
}

@Override
public boolean isEnabled(int position) {
    //return (getItemViewType(position) != TYPE_SECTION_HEADER);
    return false;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    int sectionnum = 0;
    for (Object section : this.sections.keySet())
    {
        Adapter adapter = sections.get(section);
        int size = adapter.getCount() + 1;

        // check if position inside this section
        if (position == 0) {
            //CREATE HEADER VIEW
            View lHeaderView = headers.getView(sectionnum, convertView, parent);
            String headerText = ((TextView) lHeaderView.findViewById(R.id.list_header_title)).getText().toString();
            String currentLine = "highway";
            String currentDirection = "upwards";



            if (currentLine.startsWith("N")) {
                //((TextView) lHeaderView.findViewById(R.id.list_header_title)).setTextColor(Color.rgb(10, 10, 23));
                ((TextView) lHeaderView).setTextColor(Color.rgb(10, 10, 23));
                lHeaderView.setBackgroundColor(Color.rgb(42, 73, 144));
            } else {
                //((TextView) lHeaderView.findViewById(R.id.list_header_title)).setTextColor(Color.rgb(255, 255, 255));
                ((TextView) lHeaderView).setTextColor(Color.rgb(255, 255, 255));
                lHeaderView.setBackgroundColor(Color.rgb(10, 10, 23));
            }

            //return (TextView)lHeaderView.findViewById(R.id.list_header_title);
            return lHeaderView;


        } else if (position < size) {
            //CREATE DETAILS VIEW
            View lHeaderView = headers.getView(sectionnum, convertView, parent);
            String headerText = ((TextView) lHeaderView).getText().toString();

            String currentLine = "route";

            View lView = adapter.getView(position - 1, convertView, parent);
            lView.setBackgroundColor(Color.rgb(10, 10, 23));


            if (currentLine.startsWith("N")) {
                ((TextView) lView).setTextColor(Color.rgb(10, 10, 23));
                lView.setBackgroundColor(Color.rgb(42, 73, 144));
            } else {
                ((TextView) lView).setTextColor(Color.rgb(0, 0, 0));
                lView.setBackgroundColor(Color.rgb(10, 10, 23));
            }

            return lView;
        }

        // otherwise jump into next section
        position -= size;
        sectionnum++;
    }
    return null;
}


@Override
public long getItemId(int position) {
    return position;
}
}

【问题讨论】:

  • 为此,您必须制作自定义适配器...
  • 我有一个,只是将它的代码添加到我的问题中。
  • 为什么适配器内部有一个arrayadapter?你不应该这样做,你也不需要这样做。
  • 因为我一直在寻找一种在列表中包含标题的方法,所以我找到了本教程,我遵循了该教程。 jsharkey.org/blog/2008/08/18/…
  • 到目前为止运行良好。如果我做错了,您是否有另一种方法来创建带有标题的列表,就像我正在尝试做的那样?

标签: android android-layout textview android-arrayadapter layoutparams


【解决方案1】:

我使用这个库解决了我的问题: https://github.com/emilsjolander/StickyListHeaders

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-03-10
    • 2012-03-11
    • 1970-01-01
    相关资源
    最近更新 更多