当前位置: 首页 > 知识库问答 >
问题:

Android小装置。LinearLayout无法转换为android。小装置。回收视图中的文本视图?

蔡理
2023-03-14

我试图通过使用新的回收器视图列出数据

这是我的

MainActivity.java

package com.example.i.newworkspace;

import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;


public class MainActivity extends ActionBarActivity {

TextView tv;
RecyclerView recyclerView;
RecyclerView.Adapter mAdapter;
String[] myDataset = {"One","Two","Three","Four","Five"};

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    recyclerView = (RecyclerView) findViewById(R.id.recyclerview);
    recyclerView.setLayoutManager(new LinearLayoutManager(this));
    recyclerView.setHasFixedSize(true);
    mAdapter = new MyAdapter(myDataset);
    recyclerView.setAdapter(mAdapter);

}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}

}
 class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder>

{
    String[] mDataset;

    public static class ViewHolder extends RecyclerView.ViewHolder {

        public TextView mTextView;
        public ViewHolder(TextView v) {
            super(v);
            mTextView = v;
        }
    }

    @Override
    public MyAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

        View v = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.my_text_view, null, false);
        ViewHolder vh = new ViewHolder((TextView) v);
        return vh;
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {

        holder.mTextView.setText(mDataset[position]);

    }

    @Override
    public int getItemCount() {
        return mDataset.length;
    }

    MyAdapter(String[] myDataset) {
        mDataset = myDataset;
    }
}

主要活动。xml

<RelativeLayout 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" android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">

    <android.support.v7.widget.RecyclerView

        android:id="@+id/recyclerview"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

    </android.support.v7.widget.RecyclerView>

</RelativeLayout>

我的文本视图。xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:id="@+id/textView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

</LinearLayout>

Logcat:

 java.lang.ClassCastException: android.widget.LinearLayout cannot be cast to android.widget.TextView
                at com.example.i.newworkspace.MyAdapter.onCreateViewHolder(MainActivity.java:78)
                at com.example.i.nuewworkspace.MyAdapter.onCreateViewHolder(MainActivity.java:59)

第78行:

        ViewHolder vh = new ViewHolder((TextView) v);

共有3个答案

赵智
2023-03-14

您正在尝试向TextView投射线性布局。

更改此行:

ViewHolder vh=新的ViewHolder((TextView)v)

致:

ViewHolder vh=new ViewHolder((TextView)v.findViewById(R.id.textView));

葛承德
2023-03-14

膨胀的视图也包含根元素,所以你需要通过findviewbyid函数创建文本视图,然后将其传递给ViewHolder构造函数

@Override
    public MyAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

        View v = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.my_text_view, null, false);
        TextView tv=(TextView)v.findViewById(R.id.textView);
        ViewHolder vh = new ViewHolder(tv);
        return vh;
    }

祝你好运

戚建德
2023-03-14

你应该注意到你的my_text_视图。xml是一种线性布局,因此:

public static class ViewHolder extends RecyclerView.ViewHolder {

    public TextView mTextView;
    public ViewHolder(View v) {
        super(v);
        // Find the TextView in the LinearLayout
        mTextView = v.findViewById(R.id.textView));
    }
}

@Override
public MyAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

    View v = LayoutInflater.from(parent.getContext())
            .inflate(R.layout.my_text_view, parent, false);
    // Give the view as it is
    ViewHolder vh = new ViewHolder(v);
    return vh;
}
 类似资料: