Firebase DB-linked Recycler 视图有错误
Firebase DB-linked Recycler view has an error
我是 Android 开发的初学者。 Firebase DB-linked Recycler 视图有错误。 Frag1.java
.
中有两个错误
Frag1.java
'LinearLayoutManager(android.content.Context)' in 'androidx.recyclerview.widget.LinearLayoutManager' cannot be applied to '(softcampus.co.myapplication.Frag1)'
'ContentAdapter(java.util.ArrayList<softcampus.co.myapplication.Content>, android.content.Context)' in 'softcampus.co.myapplication.ContentAdapter' cannot be applied to '(java.util.ArrayList<softcampus.co.myapplication.Content>, softcampus.co.myapplication.Frag1)'
我很难过,因为错误没有通过搜索解决...
Frag1.java
package softcampus.co.myapplication;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.ValueEventListener;
import org.w3c.dom.Text;
import java.util.ArrayList;
public class Frag1 extends Fragment {
private View view;
private RecyclerView recyclerView;
private RecyclerView.Adapter adapter;
private RecyclerView.LayoutManager layoutManager;
private ArrayList<Content> arrayList;
private FirebaseDatabase database;
private DatabaseReference databaseReference;
TextView main_name;
TextView main_board;
Button main_plus;
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
view = inflater.inflate(R.layout.frag1_home, container, false);
recyclerView = (RecyclerView)view.findViewById(R.id.recyclerView);
recyclerView.setHasFixedSize(true);
layoutManager = new LinearLayoutManager(this);
recyclerView.setLayoutManager(layoutManager);
arrayList = new ArrayList<>();
database = FirebaseDatabase.getInstance();
databaseReference = database.getReference("Content");
databaseReference.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
arrayList.clear();
for(DataSnapshot snapshot : dataSnapshot.getChildren()){
Content content = snapshot.getValue(Content.class);
arrayList.add(content);
}
adapter.notifyDataSetChanged();
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
Log.e("Frag1",String.valueOf(databaseError.toException()));
}
});
adapter = new ContentAdapter(arrayList, this);
recyclerView.setAdapter(adapter);
main_name = (TextView)view.findViewById(R.id.main_name);
main_board = (TextView) view.findViewById(R.id.main_board);
main_plus = (Button) view.findViewById(R.id.main_plus);
main_plus.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(view.getContext(), Campaign_registration.class);
startActivity(intent);
}
});
return view;
}
}
frag1_home.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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">
<TextView
android:id="@+id/main_name"
android:layout_width="365dp"
android:layout_height="wrap_content"
android:text="안녕하세요, OOO님!"
android:textSize="25sp"
android:textStyle="bold"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.1" />
<Button
android:id="@+id/main_plus"
android:layout_width="35dp"
android:layout_height="35dp"
android:background="@drawable/ic_baseline_add_circle_24"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.906"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.181" />
<TextView
android:id="@+id/main_board"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="게시판"
android:textSize="22sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.148"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.186" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="560dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="1.0">
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager" />
</ScrollView>
</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
Content.java
package softcampus.co.myapplication;
public class Content {
private String pro_img;
private String campagin_name;
private int token_goal;
private String text;
public Content(){};
public String getPro_img() {
return pro_img;
}
public void setPro_img(String pro_img) {
this.pro_img = pro_img;
}
public String getCampagin_name() {
return campagin_name;
}
public void setCampagin_name(String campagin_name) {
this.campagin_name = campagin_name;
}
public int getToken_goal() {
return token_goal;
}
public void setToken_goal(int token_goal) {
this.token_goal = token_goal;
}
public String getText() { return text;}
public void setText(String text) { this.text = text; }
}
ContentAdapter.java
package softcampus.co.myapplication;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import com.bumptech.glide.Glide;
import java.util.ArrayList;
public class ContentAdapter extends RecyclerView.Adapter<ContentAdapter.ContentViewHolder> {
private ArrayList<Content> arrayList;
private Context context;
public ContentAdapter(ArrayList<Content> arrayList, Context context) {
this.arrayList = arrayList;
this.context = context;
}
@NonNull
@Override
public ContentViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.content_item,parent,false);
ContentViewHolder holder = new ContentViewHolder(view);
return holder;
}
@Override
public void onBindViewHolder(@NonNull ContentViewHolder holder, int position) {
Glide.with(holder.itemView)
.load(arrayList.get(position).getPro_img())
.into(holder.iv_profile);
holder.tv_name.setText(arrayList.get(position).getCampagin_name());
holder.tv_goal.setText(arrayList.get(position).getToken_goal());
}
@Override
public int getItemCount() {
//삼항 연산자
return (arrayList != null ? arrayList.size() : 0);
}
public class ContentViewHolder extends RecyclerView.ViewHolder {
ImageView iv_profile;
TextView tv_name;
TextView tv_goal;
public ContentViewHolder(@NonNull View itemView) {
super(itemView);
this.iv_profile = itemView.findViewById(R.id.iv_profile);
this.tv_name = itemView.findViewById(R.id.tv_name);
this.tv_goal = itemView.findViewById(R.id.tv_goal);
}
}
}
content_item.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"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:id="@+id/iv_profile"
android:layout_width="80dp"
android:layout_height="80dp"
android:layout_margin="5dp"
app:srcCompat="@android:drawable/ic_menu_gallery" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_marginLeft="5dp">
<TextView
android:id="@+id/tv_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="캠페인명"
android:textSize="27sp"
android:layout_marginTop="14dp"/>
<TextView
android:id="@+id/tv_goal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="목표 토큰"
android:textSize="18sp"
android:layout_marginTop='3dp'/>
</LinearLayout>
</LinearLayout>
</LinearLayout>
这个问题已经困扰我好几个星期了,我似乎找不到解决办法。在此先感谢您的帮助。
对于第一个错误,要么以编程方式设置 LinearLayoutManager,要么在 xml 中设置,但不要同时使用两者,并在以编程方式设置时使用 requireContext() 而不是这个
layoutManager = new LinearLayoutManager(this);
layoutManager = new LinearLayoutManager(requireContext());
对于第二个你在片段中使用它作为上下文,使用 requireContext()
adapter = new ContentAdapter(arrayList, this);
adapter = new ContentAdapter(arrayList, requireContext());
第三,为什么你的recyclerview在scrollview里面,在linearlayout里面?考虑将其作为子项添加到 ConstraintLayout?
四、考虑去掉content_item
中嵌套的LinearLayout
我是 Android 开发的初学者。 Firebase DB-linked Recycler 视图有错误。 Frag1.java
.
Frag1.java
'LinearLayoutManager(android.content.Context)' in 'androidx.recyclerview.widget.LinearLayoutManager' cannot be applied to '(softcampus.co.myapplication.Frag1)'
'ContentAdapter(java.util.ArrayList<softcampus.co.myapplication.Content>, android.content.Context)' in 'softcampus.co.myapplication.ContentAdapter' cannot be applied to '(java.util.ArrayList<softcampus.co.myapplication.Content>, softcampus.co.myapplication.Frag1)'
我很难过,因为错误没有通过搜索解决...
Frag1.java
package softcampus.co.myapplication;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.ValueEventListener;
import org.w3c.dom.Text;
import java.util.ArrayList;
public class Frag1 extends Fragment {
private View view;
private RecyclerView recyclerView;
private RecyclerView.Adapter adapter;
private RecyclerView.LayoutManager layoutManager;
private ArrayList<Content> arrayList;
private FirebaseDatabase database;
private DatabaseReference databaseReference;
TextView main_name;
TextView main_board;
Button main_plus;
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
view = inflater.inflate(R.layout.frag1_home, container, false);
recyclerView = (RecyclerView)view.findViewById(R.id.recyclerView);
recyclerView.setHasFixedSize(true);
layoutManager = new LinearLayoutManager(this);
recyclerView.setLayoutManager(layoutManager);
arrayList = new ArrayList<>();
database = FirebaseDatabase.getInstance();
databaseReference = database.getReference("Content");
databaseReference.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
arrayList.clear();
for(DataSnapshot snapshot : dataSnapshot.getChildren()){
Content content = snapshot.getValue(Content.class);
arrayList.add(content);
}
adapter.notifyDataSetChanged();
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
Log.e("Frag1",String.valueOf(databaseError.toException()));
}
});
adapter = new ContentAdapter(arrayList, this);
recyclerView.setAdapter(adapter);
main_name = (TextView)view.findViewById(R.id.main_name);
main_board = (TextView) view.findViewById(R.id.main_board);
main_plus = (Button) view.findViewById(R.id.main_plus);
main_plus.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(view.getContext(), Campaign_registration.class);
startActivity(intent);
}
});
return view;
}
}
frag1_home.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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">
<TextView
android:id="@+id/main_name"
android:layout_width="365dp"
android:layout_height="wrap_content"
android:text="안녕하세요, OOO님!"
android:textSize="25sp"
android:textStyle="bold"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.1" />
<Button
android:id="@+id/main_plus"
android:layout_width="35dp"
android:layout_height="35dp"
android:background="@drawable/ic_baseline_add_circle_24"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.906"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.181" />
<TextView
android:id="@+id/main_board"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="게시판"
android:textSize="22sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.148"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.186" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="560dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="1.0">
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager" />
</ScrollView>
</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
Content.java
package softcampus.co.myapplication;
public class Content {
private String pro_img;
private String campagin_name;
private int token_goal;
private String text;
public Content(){};
public String getPro_img() {
return pro_img;
}
public void setPro_img(String pro_img) {
this.pro_img = pro_img;
}
public String getCampagin_name() {
return campagin_name;
}
public void setCampagin_name(String campagin_name) {
this.campagin_name = campagin_name;
}
public int getToken_goal() {
return token_goal;
}
public void setToken_goal(int token_goal) {
this.token_goal = token_goal;
}
public String getText() { return text;}
public void setText(String text) { this.text = text; }
}
ContentAdapter.java
package softcampus.co.myapplication;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import com.bumptech.glide.Glide;
import java.util.ArrayList;
public class ContentAdapter extends RecyclerView.Adapter<ContentAdapter.ContentViewHolder> {
private ArrayList<Content> arrayList;
private Context context;
public ContentAdapter(ArrayList<Content> arrayList, Context context) {
this.arrayList = arrayList;
this.context = context;
}
@NonNull
@Override
public ContentViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.content_item,parent,false);
ContentViewHolder holder = new ContentViewHolder(view);
return holder;
}
@Override
public void onBindViewHolder(@NonNull ContentViewHolder holder, int position) {
Glide.with(holder.itemView)
.load(arrayList.get(position).getPro_img())
.into(holder.iv_profile);
holder.tv_name.setText(arrayList.get(position).getCampagin_name());
holder.tv_goal.setText(arrayList.get(position).getToken_goal());
}
@Override
public int getItemCount() {
//삼항 연산자
return (arrayList != null ? arrayList.size() : 0);
}
public class ContentViewHolder extends RecyclerView.ViewHolder {
ImageView iv_profile;
TextView tv_name;
TextView tv_goal;
public ContentViewHolder(@NonNull View itemView) {
super(itemView);
this.iv_profile = itemView.findViewById(R.id.iv_profile);
this.tv_name = itemView.findViewById(R.id.tv_name);
this.tv_goal = itemView.findViewById(R.id.tv_goal);
}
}
}
content_item.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"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:id="@+id/iv_profile"
android:layout_width="80dp"
android:layout_height="80dp"
android:layout_margin="5dp"
app:srcCompat="@android:drawable/ic_menu_gallery" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_marginLeft="5dp">
<TextView
android:id="@+id/tv_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="캠페인명"
android:textSize="27sp"
android:layout_marginTop="14dp"/>
<TextView
android:id="@+id/tv_goal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="목표 토큰"
android:textSize="18sp"
android:layout_marginTop='3dp'/>
</LinearLayout>
</LinearLayout>
</LinearLayout>
这个问题已经困扰我好几个星期了,我似乎找不到解决办法。在此先感谢您的帮助。
对于第一个错误,要么以编程方式设置 LinearLayoutManager,要么在 xml 中设置,但不要同时使用两者,并在以编程方式设置时使用 requireContext() 而不是这个
layoutManager = new LinearLayoutManager(this); layoutManager = new LinearLayoutManager(requireContext());
对于第二个你在片段中使用它作为上下文,使用 requireContext()
adapter = new ContentAdapter(arrayList, this); adapter = new ContentAdapter(arrayList, requireContext());
第三,为什么你的recyclerview在scrollview里面,在linearlayout里面?考虑将其作为子项添加到 ConstraintLayout?
四、考虑去掉content_item
中嵌套的LinearLayout