这是我的适配器类代码-
package com.referral.abhin.myapplication;
import android.app.Activity;
import android.content.ClipData;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.graphics.Typeface;
import android.preference.Preference;
import android.provider.ContactsContract;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.widget.PopupMenu;
import android.support.v7.widget.RecyclerView;
import android.text.method.SingleLineTransformationMethod;
import android.util.Base64;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ExpandableListView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.database.ChildEventListener;
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.Query;
import com.google.firebase.database.ValueEventListener;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import java.util.Map;
import static android.content.Context.MODE_PRIVATE;
import static android.view.View.GONE;
public class postAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder>{
List<post> objPost;
private FirebaseDatabase mFirebaseDatabaseCheck;
private DatabaseReference mMessagesDatabaseReferenceCheck;
private DatabaseReference mMessagesDatabaseReference_notification;
private FirebaseDatabase mFirebaseDatabaseComment;
private DatabaseReference mMessagesDatabaseReferenceComment;
private FirebaseDatabase mFirebaseDatabaseLikes;
private DatabaseReference mMessagesDatabaseReferenceLikes;
//Constructor - Get data passed in local list
public postAdapter(List<post> objectPost) {
objPost = objectPost;
}
//Inititalizing Shared Preference
SharedPreferences sharedPreferences;
private static final String SHARED_PREF = "sharedPref";
// @Override
// public post getItem(int position) {
// return super.getItem(getCount() - position - 1);
// }
// private final View.OnClickListener mOnClickListener = new MyOnClickListener();
// @Override
// public class ViewHolder extends RecyclerView.ViewHolder{
//
// LayoutInflater inflater = LayoutInflater.from(parent.getContext());
// View view = inflater.inflate(R.layout.itemsinpost, parent, false);
// view.setOnClickListener(mOnClickListener);
// return new postViewHolder(view);
//
// public ViewHolder(View itemView) {
// super(itemView);
// }
// }
//nested class with holder
public class postViewHolder extends RecyclerView.ViewHolder{
TextView companyObj;
TextView locationObj;
TextView categoryObj;
TextView salaryObj;
TextView dateView;
LinearLayout itemsInRecyclerView;
public postViewHolder(View listItemView){
super(listItemView);
companyObj = listItemView.findViewById(R.id.company);
locationObj = listItemView.findViewById(R.id.location);
categoryObj = listItemView.findViewById(R.id.category);
salaryObj = listItemView.findViewById(R.id.salary);
dateView = listItemView.findViewById(R.id.dateTime);
itemsInRecyclerView = listItemView.findViewById(R.id.listViewLayout);
}
}
@NonNull
@Override
public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.itemsinpost,parent,false);
postViewHolder holder = new postViewHolder(view);
return null;
}
@Override
public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
final post objectFile = objPost.get(position);
//Firebase to update Like, Report value and also get called on delete button
mFirebaseDatabaseLikes = FirebaseDatabase.getInstance();
mMessagesDatabaseReferenceLikes = mFirebaseDatabaseLikes.getReference().child("counter");
//Firebase reference called only to update number of comments
mFirebaseDatabaseComment = FirebaseDatabase.getInstance();
mMessagesDatabaseReferenceComment = mFirebaseDatabaseComment.getReference().child("comments");
//Firebase reference to check if the user has already liked the post or not
mFirebaseDatabaseCheck = FirebaseDatabase.getInstance();
mMessagesDatabaseReferenceCheck = mFirebaseDatabaseCheck.getReference().child("check_counter");
//segregate email to userID
String[] emailSegregate = sharedPreferences.getString("email", "anonymous").split("@");
String emailID = emailSegregate[0];
mMessagesDatabaseReference_notification = mFirebaseDatabaseCheck
.getReference()
.child("notification_list")
.child(emailID)
.child(objectFile.getnodeId());
if (objectFile.getType().equals("post"))
{
holder.companyObj.setText(objectFile.getCompany());
holder.companyObj.setVisibility(View.VISIBLE);
holder.locationObj.setText("Location : " + objectFile.getLocation());
holder.locationObj.setVisibility(View.VISIBLE);
holder.categoryObj.setText(objectFile.getField());
holder.categoryObj.setVisibility(View.VISIBLE);
//if package is not empty
if(!objectFile.getminPack().isEmpty() && !objectFile.getminPack().equals("0")) {
//if work exp is not empty
if (!objectFile.getworkExp().isEmpty()) {
holder.salaryObj.setText("₹ " + objectFile.getminPack() + " LPA, " + objectFile.getworkExp() + "+ Years of Work Exp.");
}
//if work exp is empty
else {
holder.salaryObj.setText("₹ " + objectFile.getminPack() + " LPA, " + " Freshers Allowed");
}
}
//if package is empty
else{
//if work exp is not empty
if (!objectFile.getworkExp().isEmpty()) {
holder.salaryObj.setText(objectFile.getworkExp() + "+ Years of Work Exp.");
}
//if work exp is empty
else {
holder.salaryObj.setText("Freshers Allowed");
}
}
holder.salaryObj.setVisibility(View.VISIBLE);
} else
{
holder.companyObj.setText("");
holder.companyObj.setVisibility(GONE);
holder.salaryObj.setText("");
holder.salaryObj.setVisibility(GONE);
holder.locationObj.setText("");
holder.locationObj.setVisibility(GONE);
holder.categoryObj.setText(objectFile.getPost());
holder.categoryObj.setVisibility(View.VISIBLE);
}
// TextView usernameObj = listItemView.findViewById(R.id.username);
// usernameObj.setText(objectFile.getName()); //displays name while username is email address saved in database
final String[] data = objectFile.getDate().split(" ");
String dateReceived = data[0];
String timeReceived = data[1] + " " + data[2];
String[] dateSegregate = dateReceived.split("/");
int d = Integer.parseInt(dateSegregate[0]);
int m = Integer.parseInt(dateSegregate[1]);
int y = Integer.parseInt(dateSegregate[2]);
Calendar c = Calendar.getInstance();
int year = c.get(Calendar.YEAR);
int month = c.get(Calendar.MONTH) + 1;
int date = c.get(Calendar.DATE);
if (date == d && month == m && year == y)
{
holder.dateView.setText(timeReceived);
} else if (d == (date - 1) && month == m && year == y)
{
holder.dateView.setText("Yesterday");
} else
{
holder.dateView.setText(dateReceived);
}
}
@Override
public int getItemCount() {
return objPost.size();
}
}
这是错误-
如何解决?我已经根据this教程添加了代码,可以正常工作。
为什么我已经声明View Holder时为什么不选择companyObj?
答案 0 :(得分:0)
您的持有人应转换为您的类型 postViewHolder
查看onBindViewHolder方法的参数类型。
这只是RecyclerView.ViewHolder类型。
您可以通过更改通用部分中Adapter类的定义来更改此参数类型
<RecyclerView.ViewHolder> -> <postViewHolder>
答案 1 :(得分:0)
将此行更改为此
public class postAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder>{
到
public class postAdapter extends RecyclerView.Adapter<postAdapter. postViewHolder>{
然后更新此代码
@NonNull
@Override
public postAdapter.postViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.itemsinpost,parent,false);
// postViewHolder holder = new postViewHolder(view);
return new postViewHolder(view);;
}
@Override
public void onBindViewHolder(@NonNull postAdapter.postViewHolder holder, int position) {
希望这将有助于解决您的问题。