Android 从阵列适配器更改<;字符串>;将文本视图设置为ArrayAdapter<;字符串>;线性布局。类强制转换异常

Android 从阵列适配器更改<;字符串>;将文本视图设置为ArrayAdapter<;字符串>;线性布局。类强制转换异常,android,android-layout,textview,android-arrayadapter,layoutparams,Android,Android Layout,Textview,Android Arrayadapter,Layoutparams,我的代码目前正在使用一个ArrayAdapter,在布局文件中只有一个TextView,一切都很好 我当前的布局如下(R.layout.list\u标题): 我通过以下方式创建适配器: new ArrayAdapter<String>(context, R.layout.list_header); new ArrayAdapter<String>(context, R.layout.list_header, R.id.list_header_title); new

我的代码目前正在使用一个ArrayAdapter,在布局文件中只有一个TextView,一切都很好

我当前的布局如下(R.layout.list\u标题):


我通过以下方式创建适配器:

new ArrayAdapter<String>(context, R.layout.list_header);
new ArrayAdapter<String>(context, R.layout.list_header, R.id.list_header_title);
newarrayadapter(上下文,R.layout.list_标题);
现在,我想在我的适配器中,在我的文本视图的左侧添加一张图片。我首先看了这个线程,它帮助我使用的不仅仅是文本视图:

我的新布局如下:

<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);
new ArrayAdapter<String>(context, R.layout.list_header, R.id.list_header_title);
newarrayadapter(上下文、R.layout.list\u标题、R.id.list\u标题);
这就是我得到错误java.lang.ClassCastException:android.widget.LinearLayout$Params不能强制转换为android.widget.AblistView$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;
}
}
public类SeparatedListAdapter扩展了BaseAdapter
{
公共最终映射节=新建LinkedHashMap();
公共最终阵列适配器标头;
公共最终静态整型\节\头=0;
公共分隔的ListAdapter(上下文){
headers=newarrayadapter(context,R.layout.list_header);/,R.id.list_header_title);
}
public void addSection(字符串段、适配器){
此.headers.add(节);
此.sections.put(节,适配器);
}
公共对象getItem(int位置){
for(对象节:this.sections.keySet())
{
适配器=节。获取(节);
int size=adapter.getCount()+1;
//检查此部分内的位置是否正确
如果(位置==0)返回段;
if(位置<大小)返回适配器.getItem(位置-1);
//否则跳到下一节
位置-=大小;
}
返回null;
}
public int getCount(){
//所有章节合计,每个章节标题加上一个章节
int-total=0;
对于(适配器:this.sections.values())
总计+=适配器.getCount()+1;
返回总数;
}
@凌驾
public int getViewTypeCount(){
//假设头数为一,然后合计所有部分
整数合计=1;
对于(适配器:this.sections.values())
总计+=适配器。getViewTypeCount();
返回总数;
}
@凌驾
public int getItemViewType(int位置){
int型=1;
for(对象节:this.sections.keySet())
{
适配器=节。获取(节);
int size=adapter.getCount()+1;
//检查此部分内的位置是否正确
if(position==0)返回类型\u节\u头;
if(位置<大小)返回类型+适配器.getItemViewType(位置-1);
//否则跳到下一节
位置-=大小;
类型+=适配器。getViewTypeCount();
}
返回-1;
}
公共布尔值areAllItemsSelectable(){
返回false;
}
@凌驾
公共布尔值isEnabled(整型位置){
//返回(getItemViewType(位置)!=类型\节\标题);
返回false;
}
@凌驾
公共视图getView(int位置、视图转换视图、视图组父视图){
int sectionnum=0;
for(对象节:this.sections.keySet())
{
适配器=节。获取(节);
int size=adapter.getCount()+1;
//检查此部分内的位置是否正确
如果(位置==0){
//创建标题视图
View lHeaderView=headers.getView(sectionnum、convertView、父级);
字符串headerText=((TextView)lHeaderView.findViewById(R.id.list_header_title)).getText().toString();
字符串currentLine=“公路”;
字符串currentDirection=“向上”;
如果(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));
}否则{
//((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\u header\u title);
返回lHeaderView;
}否则如果(位置<尺寸){
//创建详细信息视图
View lHeaderView=headers.getView(sectionnum、convertView、父级);
字符串headerText=((TextView)lHeaderView.getText().toString();
字符串currentLine=“路由”;
View lView=adapter.getView(位置-1,convertView,父级);
lView.setBackgroundColor(Color.rgb(10,10,23));
如果(currentLine.startsWith(“N”)){
((TextView)lView.setTextColor(Color.rgb(10,10,23));
lView.setBackgroundColor(Color.rgb(42,73,144));
}否则{
((TextView)lView.setTextColor(Color.rgb(0,0,0));
lView.setBackgroundColor(Color.rgb(10,10,23));
}
返回lView;
}
//否则跳到下一节
位置-=大小;
sectionnum++;
}
返回null;
}
@凌驾
公共长getItemId(int位置){
返回位置;
}
}

我使用此库解决了我的问题:

为此,您必须制作自定义适配器…我有一个,只是在我的问题中添加了它的代码。为什么适配器中有arrayadapter?您不应该这样做,也不需要这样做。因为我正在寻找一种在列表中包含标题的方法,我找到了本教程,我遵循了本教程。它是w