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

我一直犯错误。不兼容的类型:ArrayList无法转换为ArrayList

司空胤
2023-03-14
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);
    }
}
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;
    }

}

共有1个答案

祝宾白
2023-03-14

您得到该错误是因为您传入了以下代码行:

adapter = new TargetDataAdapter(list,this);

对于TargetDataAdapter构造函数来说,列表,它声明为ArrayList 类型,而在适配器中,该列表的类型为ArrayList ,因此出现错误。要解决这个问题,您应该传递一个相同数据类型的列表,更准确地说,是ArrayList 类型的列表。因此,列表中的所有对象都必须是androidtargets类型,而不是studentdetails类型。

如果要显示StudentDetails对象列表,则需要创建另一个接受StudentDetails类型列表的适配器

 类似资料:
  • 我收到一个错误,指出ArrayList不能在我的上转换为int 我正在编写一个程序,在这个程序中,我必须从一个文件中读取所有整数,并计算它们的总和。我还处理所有异常,因此如果抛出异常,函数应该返回0。 这是我的代码: 下面是我的单元测试,读取文件后应该通过: 感谢您的帮助。 谢谢!

  • 我正在尝试创建一个基本的谷歌地图android应用程序。就在我创建项目并获得API密钥之后,我就收到了这个错误。 “错误:不兼容类型:片段无法转换为SupportMapFragment” 行:SupportMapFragment mapFragment=(SupportMapFragment)getSupportFragmentManager().findFragmentByID(r.id.map

  • 问题内容: 错误使我发疯。 在我的应用中,我在MainActivity中创建了3个导航抽屉项,和。现在,我想添加两个与的项目。 MainActivity //用于导航抽屉 TabsFragmentPagerAdapter.java ViewView.java 问题: 错误:(49,81)错误:不兼容的类型:android.app.FragmentManager无法转换为android.suppor

  • 而当运行时,我收到的是: 错误:不兼容类型:int无法转换为客户端ClientDAO.Delete(clientId);

  • 我试图创建一个动态表单布局,当我构建我的项目时,我在代码的第37行得到以下错误“错误:不兼容的类型:NewSubscription不能转换为上下文”。是不是因为这是片段,不是Java课?如何解决此错误? NewSubscription.java fragment_new_subscription.xml

  • 我试图通过在一个单独的线程上进行工作并返回所需的对象来更改JavaFX中的GUI。然而,在完成工作和任务之后。setOnSucceeded()在尝试检索创建的对象时被触发,并出现错误“不兼容类型:对象无法转换为VideoScrollPane类型”。 我认为这与原始类型有关,因为这是在听众中发生的,但环顾四周后,我找不到我想要的建议。 任何可以散发的光芒都将不胜感激。