Я получаю следующую ошибку
java.lang.ClassCastException: com.saahil.smehrashop.ProductListActivity cannot be cast to
com.saahil.smehrashop.ProductAdapter$ItemClicked
at com.saahil.smehrashop.ProductAdapter.<init>(ProductAdapter.java:30)
at com.saahil.smehrashop.HomeFragment$1.onResponse(HomeFragment.java:81)
Код для ProductListActivity следующий -
package com.saahil.smehrashop;
import android.os.Bundle;
import android.view.View;
import android.view.Menu;
import com.google.android.material.floatingactionbutton.FloatingActionButton;
import com.google.android.material.snackbar.Snackbar;
import com.google.android.material.navigation.NavigationView;
import androidx.navigation.NavController;
import androidx.navigation.Navigation;
import androidx.navigation.ui.AppBarConfiguration;
import androidx.navigation.ui.NavigationUI;
import androidx.drawerlayout.widget.DrawerLayout;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
public class ProductListActivity extends AppCompatActivity {
private AppBarConfiguration mAppBarConfiguration;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_product_list);
Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
DrawerLayout drawer = findViewById(R.id.drawer_layout);
NavigationView navigationView = findViewById(R.id.nav_view);
// Passing each menu ID as a set of Ids because each
// menu should be considered as top level destinations.
mAppBarConfiguration = new AppBarConfiguration.Builder(
R.id.nav_home, R.id.nav_gallery, R.id.nav_slideshow)
.setDrawerLayout(drawer)
.build();
NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment);
NavigationUI.setupActionBarWithNavController(this, navController, mAppBarConfiguration);
NavigationUI.setupWithNavController(navigationView, navController);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
return true;
}
@Override
public boolean onSupportNavigateUp() {
NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment);
return NavigationUI.navigateUp(navController, mAppBarConfiguration)
|| super.onSupportNavigateUp();
}
}
Код для HomeFragment. java выглядит следующим образом -
package com.saahil.smehrashop;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import com.saahil.smehrashop.Model.Products;
import java.util.ArrayList;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;
public class HomeFragment extends Fragment implements ProductAdapter.ItemClicked {
View root;
RecyclerView rvProductList;
RecyclerView.Adapter productAdapter;
RecyclerView.LayoutManager layoutManager;
ArrayList<Products> productList;
JsonPlaceHolderApi jsonPlaceHolderApi;
Context context;
public HomeFragment() {
}
public View onCreateView(@NonNull LayoutInflater inflater,
ViewGroup container, Bundle savedInstanceState) {
root = inflater.inflate(R.layout.fragment_home, container, false);
return root;
}
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
context=this.getActivity();
Retrofit retrofit=new Retrofit.Builder()
.baseUrl("http://192.168.29.214:8000/")
.addConverterFactory(GsonConverterFactory.create())
.build();
jsonPlaceHolderApi=retrofit.create(JsonPlaceHolderApi.class);
rvProductList=root.findViewById(R.id.rvProductList);
rvProductList.setHasFixedSize(true);
productList=new ArrayList<>();
layoutManager=new LinearLayoutManager(context);
rvProductList.setLayoutManager(layoutManager);
getProducts();
}
private void getProducts() {
Call<ArrayList<Products>> call=jsonPlaceHolderApi.getProducts();
call.enqueue(new Callback<ArrayList<Products>>() {
@Override
public void onResponse(Call<ArrayList<Products>> call, Response<ArrayList<Products>> response) {
if(!response.isSuccessful()){
Toast.makeText(getActivity(), "Code: "+ response.code(), Toast.LENGTH_LONG).show();
return;
}
productList=response.body();
productAdapter=new ProductAdapter(getActivity(), productList);
rvProductList.setAdapter(productAdapter);
}
@Override
public void onFailure(Call<ArrayList<Products>> call, Throwable t) {
Toast.makeText(getActivity(), t.getMessage(), Toast.LENGTH_LONG).show();
}
});
}
@Override
public void onItemClicked(int index) {
}
}
Код для ProductAdapter. java следующий -
package com.saahil.smehrashop;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import com.saahil.smehrashop.Model.Products;
import com.squareup.picasso.Picasso;
import java.util.ArrayList;
public class ProductAdapter extends RecyclerView.Adapter<ProductAdapter.ViewHolder> {
ArrayList<Products> products;
ItemClicked activity;
public interface ItemClicked{
void onItemClicked(int index);
}
public ProductAdapter(Context context, ArrayList<Products> list){
products=list;
activity=(ItemClicked) context;
}
public class ViewHolder extends RecyclerView.ViewHolder{
TextView tvName, tvPrice, tvDescription;
ImageView ivProductImage;
public ViewHolder(@NonNull View itemView) {
super(itemView);
tvName=itemView.findViewById(R.id.tvName);
tvPrice=itemView.findViewById(R.id.tvPrice);
ivProductImage=itemView.findViewById(R.id.ivProductImage);
itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
activity.onItemClicked(products.indexOf((Products) view.getTag()));
}
});
}
}
@NonNull
@Override
public ProductAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
View view= LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.product_list_items, viewGroup, false);
return new ViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
holder.itemView.setTag(products.get(position));
holder.tvName.setText(products.get(position).getName());
holder.tvDescription.setText(products.get(position).getDescription());
holder.tvPrice.setText("$"+products.get(position).getPrice());
Picasso.get().load(products.get(position).getImage()).placeholder(R.drawable.no_image).into(holder.ivProductImage);
}
@Override
public int getItemCount() {
return products.size();
}
}
Код для JsonPlaceHolderApi. java следующий -
package com.saahil.smehrashop;
import com.saahil.smehrashop.Model.Products;
import java.util.ArrayList;
import retrofit2.Call;
import retrofit2.http.GET;
public interface JsonPlaceHolderApi {
@GET("api/")
Call<ArrayList<Products>>getProducts();
}
Приложение начало выдавать эту ошибку после добавления Interface ItemClicked ... Android Studio показывает ошибку в logcat в этой строке -
activity=(ItemClicked) context;