I'm trying to create an image slider in my market app. My application has 3 fragments in MainActivity which are linked to BottomNavigationView.I tried to use adapterViewFlipper in order to create a slider in the first fragment.
here is the layout of the first fragment:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="10dp"
android:layout_marginBottom="35dp"
android:orientation="vertical">
<AdapterViewFlipper
android:id="@+id/adapterViewFlipper"
android:layout_width="match_parent"
android:layout_height="200dp">
</AdapterViewFlipper>
<android.support.v7.widget.RecyclerView
android:id="@+id/recylcerView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="@dimen/item_margin"
android:layout_marginTop="8dp"
tools:layout_editor_absoluteX="745dp"
tools:layout_editor_absoluteY="-51dp" />
</LinearLayout>
The method which gets data from the server: private void FeaturedProductlistReq(String baseURL) {
OAuthInterceptor oauth1Woocommerce = new OAuthInterceptor.Builder()
.consumerKey(getString(R.string.cunsomer_key))
.consumerSecret(getString(R.string.consumersecret))
.build();
HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();
interceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
OkHttpClient client = new OkHttpClient.Builder()
.connectTimeout(20, TimeUnit.SECONDS)
.writeTimeout(20, TimeUnit.SECONDS)
.readTimeout(30, TimeUnit.SECONDS)
.addInterceptor(interceptor)
.addInterceptor(oauth1Woocommerce)// Interceptor oauth1Woocommerce added
.build();
Retrofit mRetrofit = new Retrofit.Builder()
.baseUrl(baseURL).addConverterFactory(GsonConverterFactory.create())
.client(client)
.build();
Api api = mRetrofit.create(Api.class);
Call<List<Products>> call = api.getProducts(100);
call.enqueue(new Callback<List<Products>>() {
@Override
public void onResponse(Call<List<Products>> call, Response<List<Products>> response) {
try {
for (int i = 0; i < response.body().size(); i++) {
Products array = response.body().get(i);
//adding the product to the product list
try {
if (array.isFeatured()) {
featuredProductlist.add(new FeaturedProduct(
array.getName(),
array.getImages().get(0).getSrc()
));
}
} catch (Exception e) {
// Log.d("retrofit error", e.getMessage());
}
}
Log.d("featured getId", String.valueOf(featuredProductlist.get(0).getUrl()));
Log.d("featured getId", String.valueOf(featuredProductlist.size()));
featuredProductAdapter(featuredProductlist);
} catch (Exception e) {
Log.d("retrofit connection", String.valueOf(e.getMessage()));
Toast.makeText(getContext(), "خطا در اتصال header",
Toast.LENGTH_SHORT).show();
}
}
@Override
public void onFailure(Call<List<Products>> call, Throwable t) {
Toast.makeText(getContext(), "خطا در اتصال",
Toast.LENGTH_SHORT).show();
// Toast.makeText(getContext(), t.getMessage(),
Toast.LENGTH_SHORT).show();
// Log.d("retrofit error", t.getMessage());
}
});
}
The flipper adapter code:
public class FlipperAdapter extends BaseAdapter {
private Context mCtx;
private ArrayList<FeaturedProduct> featuredProducts;
public FlipperAdapter(Context mCtx, ArrayList<FeaturedProduct> featuredProducts) {
this.mCtx = mCtx;
this.featuredProducts = featuredProducts;
}
@Override
public int getCount() {
return featuredProducts.size();
}
@Override
public Object getItem(int position) {
return null;
}
@Override
public long getItemId(int position) {
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
FeaturedProduct featuredProduct = featuredProducts.get(position);
LayoutInflater inflater = LayoutInflater.from(mCtx);
View view = inflater.inflate(R.layout.flipper_items, null);
TextView textView = view.findViewById(R.id.textViewflipper);
ImageView imageView = view.findViewById(R.id.imageViewflipper);
textView.setText(featuredProduct.getName());
Glide.with(mCtx).load(featuredProduct.getUrl()).into(imageView);
return view;
}
}
and the adapter method in the fragment:
private void featuredProductAdapter(ArrayList<FeaturedProduct> featuredProductlist2) {
Log.d("adapter error", "00000000000000");
//creating adapter object
FlipperAdapter adapter = new FlipperAdapter(getContext(), featuredProductlist2);
Log.d("adapter error", adapter.getItem(0).toString());
//adding it to adapterview flipper
adapterViewFlipper.setAdapter(adapter);
adapterViewFlipper.setFlipInterval(1000);
adapterViewFlipper.startFlipping();
}
everthig is ok but when it try to create adapter in :
FlipperAdapter adapter = new FlipperAdapter(getContext(), featuredProductlist2);
it return this error in logcat:
D/retrofit connection: Attempt to invoke virtual method 'java.lang.String java.lang.Object.toString()' on a null object reference
I google it but i couldn't find a good solution for my problem
Could someone know what is the problem? or could you give a solution to create an image slider in a fragment?
Thanks in advance
That's because you did not implement getItem
in your adapter correctly, and so it fails when you call adapter.getItem(0).toString()
:
@Override
public Object getItem(int position) {
// return null; you probably do not want to return null here
return featuredProducts.get(position);
}