我正在尝试通过url okhttp调用将数据加载到我的recyclerview中,并且收到以下错误:

“ java.lang.ClassCastException:SearchActivity无法转换为SearchForBarbershop $ BarbershopRequesterResponse”

这是课程。

SearchActivity:

public class SearchActivity extends AppCompatActivity {

private RecyclerAdapter mAdapter;
private RecyclerView mRecyclerView;
private LinearLayoutManager mLinearLayoutManager;

private ArrayList<Barbershop> barbershopList;
private SearchForBarbershop searchForBarbershop;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_search);

    mRecyclerView = (RecyclerView) findViewById(R.id.my_recycler_view);
    mLinearLayoutManager = new LinearLayoutManager(this);
    mRecyclerView.setLayoutManager(mLinearLayoutManager);

    barbershopList = new ArrayList<>();
    mAdapter = new RecyclerAdapter(barbershopList);
    mRecyclerView.setAdapter(mAdapter);
    setRecyclerViewScrollListener();
    searchForBarbershop = new SearchForBarbershop(this);
}

@Override
protected void onStart() {
    super.onStart();
    if (barbershopList.size() == 0) {
        requestShop();
    }
}

private int getLastVisibleItemPosition() {
    return mLinearLayoutManager.findLastVisibleItemPosition();
}

private void setRecyclerViewScrollListener() {
    mRecyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
        @Override
        public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
            super.onScrollStateChanged(recyclerView, newState);
            int totalItemCount = mRecyclerView.getLayoutManager().getItemCount();
            if (!searchForBarbershop.isLoadingData() && totalItemCount == getLastVisibleItemPosition() + 1) {
                requestShop();
            }
        }
    });
}

private void requestShop() {

    try {
        searchForBarbershop.getBarbershop();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

//@Override
public void receivedNewShop(final Barbershop newBarbershop) {

    runOnUiThread(new Runnable() {
        @Override
        public void run() {
            barbershopList.add(newBarbershop);
            mAdapter.notifyItemInserted(barbershopList.size());
        }
    });
}


}

SearchForBarbershop:

public class SearchForBarbershop {

public interface BarbershopRequesterResponse {
    void receivedNewBarbershop(Barbershop barbershop);
}

private BarbershopRequesterResponse barbershopRequesterResponse;
private OkHttpClient client;
private boolean isLoadingData;

public boolean isLoadingData() { return isLoadingData; }

public SearchForBarbershop (Activity listeningActivity) {
    client = new OkHttpClient();
    isLoadingData = false;
    barbershopRequesterResponse = (BarbershopRequesterResponse) listeningActivity;
}

public void getBarbershop() throws IOException {
    String url = "http://pubapi.yp.com/search-api/search/devapi/search?searchloc=30043&term=barbers&format=json&sort=distance&radius=50&listingcount=20&key=gmj3x7mhsh";
    Request request = new Request.Builder().url(url).build();
    isLoadingData = true;

    client.newCall(request).enqueue(new Callback() {
        @Override
        public void onFailure(Call call, IOException e) {
            isLoadingData = false;
            e.printStackTrace();
        }

        @Override
        public void onResponse(Call call, Response response) throws IOException {
            try {
                JSONObject object = new JSONObject(response.body().string());

                    Barbershop receivedShop = new Barbershop(object);
                    barbershopRequesterResponse.receivedNewBarbershop(receivedShop);
                    isLoadingData = false;

            } catch (JSONException e) {
                isLoadingData = false;
                e.printStackTrace();
            }
        }
    });
}


}

理发店:

public class Barbershop{

private String name;
private String city;

public Barbershop(JSONObject barbershopJSON) {

    try {
        JSONArray businessArray = barbershopJSON.getJSONObject("searchResult").getJSONObject("searchListings").getJSONArray("searchListing");

        for(int i = 0; i < businessArray.length(); i++) {

            //Parse JSON and assign to variables
            JSONObject business_object_1 = businessArray.getJSONObject(i);
            String primaryc = business_object_1.getString("primaryCategory");
            //Check to make sure the main category is "Barbers"
            if((primaryc).equals("Barbers")) {

                name = business_object_1.getString("businessName");
                city = business_object_1.getString("city");

            }
        }
    } catch (JSONException e) {
        e.printStackTrace();
    }
}

public String getCity() {
    return city;
}

public String getName() {
    return name;
}


}

RecyclerAdapter:

public class RecyclerAdapter extends RecyclerView.Adapter<RecyclerAdapter.ShopHolder> {

private ArrayList<Barbershop> mbarbershops;

public RecyclerAdapter(ArrayList<Barbershop> barbershops) {
    mbarbershops = barbershops;
}

@Override
public RecyclerAdapter.ShopHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View inflatedView = LayoutInflater.from(parent.getContext())
            .inflate(R.layout.cardview_barber_search, parent, false);
    return new ShopHolder(inflatedView);
}

@Override
public void onBindViewHolder(RecyclerAdapter.ShopHolder holder, int position) {
    Barbershop itemBarbershop = mbarbershops.get(position);
    holder.bindShop(itemBarbershop);
}

@Override
public int getItemCount() {
    return 0;
}

public static class ShopHolder extends RecyclerView.ViewHolder implements View.OnClickListener {

    private TextView mName;
    private TextView mCity;

    public ShopHolder(View view) {
        super(view);

        mName = (TextView) view.findViewById(R.id.textViewName);
        mCity = (TextView) view.findViewById(R.id.textViewCity);
        view.setOnClickListener(this);

    }

    @Override
    public void onClick(View v) {

    }

    public void bindShop(Barbershop barbershop) {
        mName.setText(barbershop.getName());
        mCity.setText(barbershop.getCity());
    }
}


}

最佳答案

由于无法实现该接口,因此无法进行投射。在这里,您注释掉的@override语句应该为您提供一条线索。

更改为:

public class SearchActivity extends AppCompatActivity
    implements SearchForBarbershop.BarbershopRequesterResponse {

....


并且不要忘记删除您实现的方法的注释:

@Override
public void receivedNewShop(final Barbershop newBarbershop) {
....

08-03 22:07