2

I am trying to load data into my recyclerview via url okhttp calls, and I am receiving the following error:

"java.lang.ClassCastException: SearchActivity cannot be cast to SearchForBarbershop$BarbershopRequesterResponse"

Here are the classes.

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();
            }
        }
    });
}

}

Barbershop:

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());
    }
}

}

teej2542
  • 577
  • 3
  • 10
  • 27

2 Answers2

1

It can't be cast because it doesn't implement that interface. Your commented-out @override statement should be a clue for you here.

Change it to:

public class SearchActivity extends AppCompatActivity 
    implements SearchForBarbershop.BarbershopRequesterResponse {

....

And don't forget to remove the comment for method you implemented:

@Override
public void receivedNewShop(final Barbershop newBarbershop) { 
....
323go
  • 14,143
  • 6
  • 33
  • 41
  • Thanks, that got rid of the crash. But no data is showing in the activity. Not sure why, api url is good, it parses correctly. – teej2542 Sep 23 '16 at 03:55
0

I think you need change constructor SearchForBarbershop

BarbershopRequesterResponse listener;
Context context
public SearchForBarbershop (Context context,BarbershopRequesterResponse listener) {
    client = new OkHttpClient();
    isLoadingData = false;
    this.context = context;
    this.listener = listener;
}

And change in SearchActivity

searchForBarbershop = new SearchForBarbershop(SearchActivity.this, this);
RoShan Shan
  • 2,924
  • 1
  • 18
  • 38