问题:我想在我的主要活动中实现RecyclerView
,但是当我想为此设置适配器时,编译器向我提供红线错误。我无法在android的主要活动中访问另一个类:
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<android.support.v7.widget.RecyclerView
android:id="@+id/recycler"
android:layout_width="match_parent"
android:layout_height="match_parent">
</android.support.v7.widget.RecyclerView>
</LinearLayout>
MainActivity.java
package com.example.mypc.recyclerview;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.widget.*;
public class MainActivity extends AppCompatActivity {
RecyclerView rv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
rv=(RecyclerView)findViewById(R.id.recycler);
rv.setLayoutManager(new LinearLayoutManager(this));
String [] lang=
{"Hindi","English","Gujarati","Marathi","Punjabi","Tamil","Telgu"};
rv.setAdapter(new ***RecyclerAdapter***(lang)); //Here comes the
//error
}
}
layout.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
android:orientation="horizontal"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageView
android:id="@+id/img"
android:layout_width="82dp"
android:layout_height="match_parent"
android:src="@mipmap/ic_launcher"
tools:layout_editor_absoluteY="35dp" />
<TextView
android:id="@+id/tv"
android:layout_width="627dp"
android:layout_height="match_parent"
android:text="@string/app_name" />
</LinearLayout>
RecyclerAdapter.java
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import com.example.mypc.recyclerview.MainActivity;
import com.example.mypc.recyclerview.R;
import java.util.zip.Inflater;
public class RecyclerAdapter extends
RecyclerView.Adapter<RecyclerAdapter.MyClass>{
String[] arr;
public RecyclerAdapter(String []arr) {
this.arr=arr;
}
@NonNull
@Override
public MyClass onCreateViewHolder( ViewGroup viewGroup, int i) {
LayoutInflater inflater=LayoutInflater.from(viewGroup.getContext());
View view=inflater.inflate(R.layout.list,viewGroup,false);
return new MyClass(view);
}
@Override
public void onBindViewHolder(@NonNull MyClass viewHolder, int i) {
String title=arr[i];
viewHolder.tv.setText(title);
}
@Override
public int getItemCount() {
return arr.length;
}
public class MyClass extends RecyclerView.ViewHolder{
ImageView img;
TextView tv;
public MyClass(@NonNull View itemView) {
super(itemView);
img=(ImageView) itemView.findViewById(R.id.img);
tv=(TextView) itemView.findViewById(R.id.tv);
}
}
}