这个想法是让Firebase中的列表在
RecyclerView
中填充时可点击,但出现此错误:错误:类型不兼容:无法将ArrayList转换为ArrayList”
在包含“ adapter = new TargetDataAdapter(list,this);”的行中在showstudentdetails活动中。
这是
showstudentdetails
代码:
package com.example.helpresponse;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
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 java.util.ArrayList;
import java.util.List;
//Note that you can implement more than one interfaces, that's one of its beauty.
public class ShowStudentDetailsActivity extends AppCompatActivity implements HandleClick {
private DatabaseReference databaseReference;
ProgressDialog progressDialog;
private ArrayList<StudentDetails> list = new ArrayList<>();
private RecyclerView recyclerView;
private TargetDataAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_show_student_details);
initRecyclerView();
initProgressDialog("Loading Data from Firebase Database");
progressDialog.show();
databaseReference = FirebaseDatabase.getInstance().getReference("Police").child("Chats");
databaseReference.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot snapshot) {
for (DataSnapshot dataSnapshot : snapshot.getChildren()) {
StudentDetails studentDetails = dataSnapshot.getValue(StudentDetails.class);
list.add(studentDetails);
}
adapter = new TargetDataAdapter(list,this);
recyclerView.setAdapter(adapter);
progressDialog.dismiss();
}
@Override
public void onCancelled(DatabaseError databaseError) {
progressDialog.dismiss();
}
});
}
private void initProgressDialog(String msg){
progressDialog = new ProgressDialog(this);
progressDialog.setMessage(msg);
}
private void initRecyclerView(){
recyclerView = findViewById(R.id.recyclerView);
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false));
}
@Override
public void onItemClick(int index){
Intent intent = new Intent(this, MapsActivity.class);
int myData = index;
intent.putExtra("myDataKey", myData);
startActivity(intent);
}
}
这是android目标
package com.example.helpresponse;
public class AndroidTargets {
public String FIELD1;
public Long FIELD2;
public String FIELD3;
}
这是适配器
package com.example.helpresponse;
import android.content.Context;
import android.content.Intent;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.Locale;
public class TargetDataAdapter extends RecyclerView.Adapter<TargetDataAdapter.TargetViewHolder> {
private ArrayList<AndroidTargets> targetsArrayList;
private HandleClick mHandleClick;
/*Note that the constructor now take two params which helps us get ride of setmHandleClick() method.*/
public TargetDataAdapter(ArrayList<AndroidTargets> mTargetData, HandleClick handleClick) {
targetsArrayList = mTargetData;
mHandleClick = handleClick;
}
@NonNull
@Override
public TargetViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
View v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.target_row, viewGroup, false);
return new TargetViewHolder(v);
}
/* There is an interesting way to make below code neater by abstracting it back to the Activity incharge, let me know if you want that...priority now is to focus on fixing your issue. */
@Override
public void onBindViewHolder(@NonNull TargetViewHolder viewHolder, int i) {
viewHolder.androidTargetName.setText(targetsArrayList.get(i).FIELD1);
viewHolder.androidTargetNumber.setText(String.format(Locale.getDefault(), "API Level: %d", targetsArrayList.get(i).FIELD2));
viewHolder.androidTargetShortName.setText(targetsArrayList.get(i).FIELD3);
viewHolder.myClickableView.setClickable(true);
viewHolder.myClickableView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
mHandleClick.onItemClick(i);
}
});
}
@Override
public int getItemCount() {
if (targetsArrayList == null)
return 0;
return targetsArrayList.size();
}
public static class TargetViewHolder extends RecyclerView.ViewHolder {
protected TextView androidTargetName;
protected TextView androidTargetNumber;
protected TextView androidTargetShortName;
protected LinearLayout myClickableView;
public TargetViewHolder(@NonNull View itemView) {
super(itemView);
myClickableView = itemView.findViewById(R.id.linearLayout);
androidTargetShortName = itemView.findViewById(R.id.textView2);
androidTargetName = itemView.findViewById(R.id.textView3);
androidTargetNumber = itemView.findViewById(R.id.textView4);
}
public interface IHomeSelector {
void onCategorySelected(int postion);
}
}
}
这是学生详细信息
package com.example.helpresponse;
/**
* Created by Juned on 7/25/2017.
*/
public class StudentDetails {
private String name;
public String message;
public String latitude;
public String longitude;
public StudentDetails() {
// This is default constructor.
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
public String getLatitude() {
return latitude;
}
public void setLatitude(String latitude) {
this.latitude = latitude;
}
public String getLongitude() {
return longitude;
}
public void setLongitude(String longitude) {
this.longitude = longitude;
}
public String getStudentName() {
return name;
}
public void setStudentName(String name) {
this.name = name;
}
}
答案 0 :(得分:1)
由于传递了以下代码行,因此出现了该错误:
adapter = new TargetDataAdapter(list,this);
对于TargetDataAdapter
构造函数,一个list
声明为ArrayList<StudentDetails>
类型,在您的适配器中,该列表的类型为ArrayList<AndroidTargets>
,因此出现错误。为了解决这个问题,您应该传递一个具有相同数据类型的列表,更确切地说,是类型为ArrayList<AndroidTargets>
的列表。因此,列表中的所有对象必须为AndroidTargets
类型,并且不是类型为StudentDetails
。
如果要显示StudentDetails
对象的列表,则需要创建另一个接受类型为StudentDetails
的列表的适配器。