Alternative to RecyclerView for older Sdk












-1















I have created an App with RecyclerView but it’s crash in the devices with 4.1 Android OS.
There is an alternative to RecyclerView or i can risolve the problem?



Thanks.



UPDATE



Activity



import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentTransaction;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.pm.PackageManager;
import android.os.Build;
import android.support.v4.app.ActivityCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

public class Dashboard extends AppCompatActivity {

private Activity activity;

private SharedPreferences pref;
SharedPreferences shared;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
pref = getPreferences(0);

Fragment fragment;
fragment = new DashboardFragment();
FragmentTransaction ft = getFragmentManager().beginTransaction();
ft.replace(R.id.fragment_frame, fragment);
ft.commit();

}//fine OnCreate


}// FINE CLASS


Fragment



import android.app.ProgressDialog;
import android.content.Context;
import android.content.SharedPreferences;
import android.graphics.Color;
import android.os.AsyncTask;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.Nullable;
import android.support.design.widget.Snackbar;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
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.ListView;

import com.xxx.parser.JSONParser;
import com.xxx.utils.InternetConnection;
import com.xxx.utils.Keys;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

public class FragmentCards extends android.app.Fragment {

View v;
private RecyclerView myrecyclerview;
private List<Cards> lstCards;
//

private SharedPreferences pref;
SharedPreferences shared;
String unique_id;

// Getting application context
Context context = getActivity();

//JSON
private ListView listView;
RecyclerViewAdapter recyclerAdapter;


public FragmentCards() {
}

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
v = inflater.inflate(R.layout.cards_fragment, container, false);
myrecyclerview = (RecyclerView) v.findViewById(R.id.card_rv);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
recyclerAdapter = new RecyclerViewAdapter(getContext(),lstCards);
}
myrecyclerview.setLayoutManager(new LinearLayoutManager(getActivity()));
myrecyclerview.setAdapter(recyclerAdapter);
return v;
// return null;
}

@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

//per visualizzare codice
pref = getActivity().getPreferences(0);
shared = getActivity().getSharedPreferences("A", Context.MODE_PRIVATE); // get the sharedpreference set named "A"
unique_id= shared.getString("unique_id","");
//
//
lstCards = new ArrayList<>();
//
goToList();

} // fine ON CREATE

public void check_connectivity(){
View view = getActivity().findViewById(android.R.id.content);
// Initialize a new Snackbar
Snackbar snackbar = Snackbar.make(view, "Connessione assente ... riprovo fra 5 secondi", Snackbar.LENGTH_INDEFINITE);
// Change the Snackbar default background color
View snackbarView = snackbar.getView();
snackbarView.setBackgroundColor(Color.parseColor("#FF0000"));
snackbar.show();

new Handler().postDelayed(new Runnable() {
@Override
public void run() {
View view = getActivity().findViewById(android.R.id.content);
Snackbar snackbar = Snackbar.make(view, "Connessione in corso ...", Snackbar.LENGTH_SHORT);
// Change the Snackbar default background color
View snackbarView = snackbar.getView();
snackbarView.setBackgroundColor(Color.parseColor("#F9A603"));
snackbar.show();
new Handler().postDelayed(new Runnable() {
@Override
public void run() {

goToList();

}
}, 1500);

}
}, 5000);
}

public void goToList() {
if (InternetConnection.checkConnection(getActivity().getApplicationContext())) {

new GetDataTask().execute();
View view = getActivity().findViewById(android.R.id.content);
Snackbar snackbar = Snackbar.make(view, "Connesso", Snackbar.LENGTH_SHORT);
// Change the Snackbar default background color
View snackbarView = snackbar.getView();
snackbarView.setBackgroundColor(Color.parseColor("#258039"));
snackbar.show();

} else {
check_connectivity();

}
}

class GetDataTask extends AsyncTask<Void, Void, Void> {

ProgressDialog dialog;

@Override
protected void onPreExecute() {
super.onPreExecute();
dialog = new ProgressDialog(getActivity());
dialog.setTitle("Un attimo di pazienza...");
dialog.setMessage("Carico le tue card");
dialog.show();
}

@Nullable
@Override
protected Void doInBackground(Void... params) {

JSONObject jsonObject = JSONParser.getDataById(unique_id);


try {


JSONArray array = jsonObject.getJSONArray(Keys.KEY_CONTACTS);


int lenArray = array.length();
if(lenArray > 0) {
for(int jIndex = 0; jIndex < lenArray; jIndex++) {


JSONObject innerObject = array.getJSONObject(jIndex);
String desc_dist = innerObject.getString(Keys.KEY_DIST);
String numero_card = innerObject.getString(Keys.KEY_NUM_CARD);
String totpunti_card = innerObject.getString(Keys.KEY_TOT_PUNTI);
String id_cd = innerObject.getString(Keys.KEY_ID_CD);


/**
* Adding name and phone concatenation in List...
*/
lstCards.add(new Cards( numero_card,desc_dist,totpunti_card,id_cd) );
} //fine FOR

}
}
} else {

}
} catch (JSONException je) {
Log.i(JSONParser.TAG, "" + je.getLocalizedMessage());
}
return null;
} // FINE doBackground

@Override
protected void onPostExecute(Void aVoid) {
super.onPostExecute(aVoid);
dialog.dismiss();
/**
* Checking if List size if more than zero then
* Update ListView
*/
if(lstCards.size() > 0) {
recyclerAdapter.notifyDataSetChanged();
} else {
View view = getActivity().findViewById(android.R.id.content);
Snackbar snackbar = Snackbar.make(view, "No Data Found", Snackbar.LENGTH_SHORT);
}
}
} // fine GetDataTask
}


RecyclerView



    import android.app.Activity;
import android.app.Dialog;
import android.app.Fragment;
import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
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.List;

public class RecycleTransCardAdapter extends RecyclerView.Adapter<RecycleTransCardAdapter.MyViewHolder> {


Context mContext;
List<InfoCard> mData;
Dialog myDialog;

public RecycleTransCardAdapter(Context mContext, List<InfoCard> mData) {
this.mContext = mContext;
this.mData = mData;
}

@NonNull
@Override
public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {

View v;
v = LayoutInflater.from(mContext).inflate(R.layout.item_trans_card, parent, false);
final MyViewHolder vHolder = new MyViewHolder(v);


return vHolder;
}

@Override
public void onBindViewHolder(@NonNull MyViewHolder holder, int position) {

// some code
}

@Override
public int getItemCount() {
return mData.size();
}

public static class MyViewHolder extends RecyclerView.ViewHolder {

// some code


public MyViewHolder(View itemView) {
super(itemView) ;
// some code

}
}

}


The error is in Fragment at this line:



recyclerAdapter.notifyDataSetChanged();



I have this error only on old OS Android
Example: 4.1 version










share|improve this question




















  • 5





    What is crash report? Where is stacktrace? Where is code? Atleast post something.

    – Piyush
    Jan 2 at 6:17








  • 1





    Post your stacktrace here so we can help.

    – Mahmoud Elshamy
    Jan 2 at 6:24






  • 2





    RecyclerView is part of v7 support libs and it works well with till api level 7. there may be network and thread issue causing error .

    – Saurabh Bhandari
    Jan 2 at 6:25
















-1















I have created an App with RecyclerView but it’s crash in the devices with 4.1 Android OS.
There is an alternative to RecyclerView or i can risolve the problem?



Thanks.



UPDATE



Activity



import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentTransaction;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.pm.PackageManager;
import android.os.Build;
import android.support.v4.app.ActivityCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

public class Dashboard extends AppCompatActivity {

private Activity activity;

private SharedPreferences pref;
SharedPreferences shared;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
pref = getPreferences(0);

Fragment fragment;
fragment = new DashboardFragment();
FragmentTransaction ft = getFragmentManager().beginTransaction();
ft.replace(R.id.fragment_frame, fragment);
ft.commit();

}//fine OnCreate


}// FINE CLASS


Fragment



import android.app.ProgressDialog;
import android.content.Context;
import android.content.SharedPreferences;
import android.graphics.Color;
import android.os.AsyncTask;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.Nullable;
import android.support.design.widget.Snackbar;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
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.ListView;

import com.xxx.parser.JSONParser;
import com.xxx.utils.InternetConnection;
import com.xxx.utils.Keys;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

public class FragmentCards extends android.app.Fragment {

View v;
private RecyclerView myrecyclerview;
private List<Cards> lstCards;
//

private SharedPreferences pref;
SharedPreferences shared;
String unique_id;

// Getting application context
Context context = getActivity();

//JSON
private ListView listView;
RecyclerViewAdapter recyclerAdapter;


public FragmentCards() {
}

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
v = inflater.inflate(R.layout.cards_fragment, container, false);
myrecyclerview = (RecyclerView) v.findViewById(R.id.card_rv);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
recyclerAdapter = new RecyclerViewAdapter(getContext(),lstCards);
}
myrecyclerview.setLayoutManager(new LinearLayoutManager(getActivity()));
myrecyclerview.setAdapter(recyclerAdapter);
return v;
// return null;
}

@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

//per visualizzare codice
pref = getActivity().getPreferences(0);
shared = getActivity().getSharedPreferences("A", Context.MODE_PRIVATE); // get the sharedpreference set named "A"
unique_id= shared.getString("unique_id","");
//
//
lstCards = new ArrayList<>();
//
goToList();

} // fine ON CREATE

public void check_connectivity(){
View view = getActivity().findViewById(android.R.id.content);
// Initialize a new Snackbar
Snackbar snackbar = Snackbar.make(view, "Connessione assente ... riprovo fra 5 secondi", Snackbar.LENGTH_INDEFINITE);
// Change the Snackbar default background color
View snackbarView = snackbar.getView();
snackbarView.setBackgroundColor(Color.parseColor("#FF0000"));
snackbar.show();

new Handler().postDelayed(new Runnable() {
@Override
public void run() {
View view = getActivity().findViewById(android.R.id.content);
Snackbar snackbar = Snackbar.make(view, "Connessione in corso ...", Snackbar.LENGTH_SHORT);
// Change the Snackbar default background color
View snackbarView = snackbar.getView();
snackbarView.setBackgroundColor(Color.parseColor("#F9A603"));
snackbar.show();
new Handler().postDelayed(new Runnable() {
@Override
public void run() {

goToList();

}
}, 1500);

}
}, 5000);
}

public void goToList() {
if (InternetConnection.checkConnection(getActivity().getApplicationContext())) {

new GetDataTask().execute();
View view = getActivity().findViewById(android.R.id.content);
Snackbar snackbar = Snackbar.make(view, "Connesso", Snackbar.LENGTH_SHORT);
// Change the Snackbar default background color
View snackbarView = snackbar.getView();
snackbarView.setBackgroundColor(Color.parseColor("#258039"));
snackbar.show();

} else {
check_connectivity();

}
}

class GetDataTask extends AsyncTask<Void, Void, Void> {

ProgressDialog dialog;

@Override
protected void onPreExecute() {
super.onPreExecute();
dialog = new ProgressDialog(getActivity());
dialog.setTitle("Un attimo di pazienza...");
dialog.setMessage("Carico le tue card");
dialog.show();
}

@Nullable
@Override
protected Void doInBackground(Void... params) {

JSONObject jsonObject = JSONParser.getDataById(unique_id);


try {


JSONArray array = jsonObject.getJSONArray(Keys.KEY_CONTACTS);


int lenArray = array.length();
if(lenArray > 0) {
for(int jIndex = 0; jIndex < lenArray; jIndex++) {


JSONObject innerObject = array.getJSONObject(jIndex);
String desc_dist = innerObject.getString(Keys.KEY_DIST);
String numero_card = innerObject.getString(Keys.KEY_NUM_CARD);
String totpunti_card = innerObject.getString(Keys.KEY_TOT_PUNTI);
String id_cd = innerObject.getString(Keys.KEY_ID_CD);


/**
* Adding name and phone concatenation in List...
*/
lstCards.add(new Cards( numero_card,desc_dist,totpunti_card,id_cd) );
} //fine FOR

}
}
} else {

}
} catch (JSONException je) {
Log.i(JSONParser.TAG, "" + je.getLocalizedMessage());
}
return null;
} // FINE doBackground

@Override
protected void onPostExecute(Void aVoid) {
super.onPostExecute(aVoid);
dialog.dismiss();
/**
* Checking if List size if more than zero then
* Update ListView
*/
if(lstCards.size() > 0) {
recyclerAdapter.notifyDataSetChanged();
} else {
View view = getActivity().findViewById(android.R.id.content);
Snackbar snackbar = Snackbar.make(view, "No Data Found", Snackbar.LENGTH_SHORT);
}
}
} // fine GetDataTask
}


RecyclerView



    import android.app.Activity;
import android.app.Dialog;
import android.app.Fragment;
import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
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.List;

public class RecycleTransCardAdapter extends RecyclerView.Adapter<RecycleTransCardAdapter.MyViewHolder> {


Context mContext;
List<InfoCard> mData;
Dialog myDialog;

public RecycleTransCardAdapter(Context mContext, List<InfoCard> mData) {
this.mContext = mContext;
this.mData = mData;
}

@NonNull
@Override
public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {

View v;
v = LayoutInflater.from(mContext).inflate(R.layout.item_trans_card, parent, false);
final MyViewHolder vHolder = new MyViewHolder(v);


return vHolder;
}

@Override
public void onBindViewHolder(@NonNull MyViewHolder holder, int position) {

// some code
}

@Override
public int getItemCount() {
return mData.size();
}

public static class MyViewHolder extends RecyclerView.ViewHolder {

// some code


public MyViewHolder(View itemView) {
super(itemView) ;
// some code

}
}

}


The error is in Fragment at this line:



recyclerAdapter.notifyDataSetChanged();



I have this error only on old OS Android
Example: 4.1 version










share|improve this question




















  • 5





    What is crash report? Where is stacktrace? Where is code? Atleast post something.

    – Piyush
    Jan 2 at 6:17








  • 1





    Post your stacktrace here so we can help.

    – Mahmoud Elshamy
    Jan 2 at 6:24






  • 2





    RecyclerView is part of v7 support libs and it works well with till api level 7. there may be network and thread issue causing error .

    – Saurabh Bhandari
    Jan 2 at 6:25














-1












-1








-1








I have created an App with RecyclerView but it’s crash in the devices with 4.1 Android OS.
There is an alternative to RecyclerView or i can risolve the problem?



Thanks.



UPDATE



Activity



import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentTransaction;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.pm.PackageManager;
import android.os.Build;
import android.support.v4.app.ActivityCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

public class Dashboard extends AppCompatActivity {

private Activity activity;

private SharedPreferences pref;
SharedPreferences shared;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
pref = getPreferences(0);

Fragment fragment;
fragment = new DashboardFragment();
FragmentTransaction ft = getFragmentManager().beginTransaction();
ft.replace(R.id.fragment_frame, fragment);
ft.commit();

}//fine OnCreate


}// FINE CLASS


Fragment



import android.app.ProgressDialog;
import android.content.Context;
import android.content.SharedPreferences;
import android.graphics.Color;
import android.os.AsyncTask;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.Nullable;
import android.support.design.widget.Snackbar;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
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.ListView;

import com.xxx.parser.JSONParser;
import com.xxx.utils.InternetConnection;
import com.xxx.utils.Keys;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

public class FragmentCards extends android.app.Fragment {

View v;
private RecyclerView myrecyclerview;
private List<Cards> lstCards;
//

private SharedPreferences pref;
SharedPreferences shared;
String unique_id;

// Getting application context
Context context = getActivity();

//JSON
private ListView listView;
RecyclerViewAdapter recyclerAdapter;


public FragmentCards() {
}

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
v = inflater.inflate(R.layout.cards_fragment, container, false);
myrecyclerview = (RecyclerView) v.findViewById(R.id.card_rv);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
recyclerAdapter = new RecyclerViewAdapter(getContext(),lstCards);
}
myrecyclerview.setLayoutManager(new LinearLayoutManager(getActivity()));
myrecyclerview.setAdapter(recyclerAdapter);
return v;
// return null;
}

@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

//per visualizzare codice
pref = getActivity().getPreferences(0);
shared = getActivity().getSharedPreferences("A", Context.MODE_PRIVATE); // get the sharedpreference set named "A"
unique_id= shared.getString("unique_id","");
//
//
lstCards = new ArrayList<>();
//
goToList();

} // fine ON CREATE

public void check_connectivity(){
View view = getActivity().findViewById(android.R.id.content);
// Initialize a new Snackbar
Snackbar snackbar = Snackbar.make(view, "Connessione assente ... riprovo fra 5 secondi", Snackbar.LENGTH_INDEFINITE);
// Change the Snackbar default background color
View snackbarView = snackbar.getView();
snackbarView.setBackgroundColor(Color.parseColor("#FF0000"));
snackbar.show();

new Handler().postDelayed(new Runnable() {
@Override
public void run() {
View view = getActivity().findViewById(android.R.id.content);
Snackbar snackbar = Snackbar.make(view, "Connessione in corso ...", Snackbar.LENGTH_SHORT);
// Change the Snackbar default background color
View snackbarView = snackbar.getView();
snackbarView.setBackgroundColor(Color.parseColor("#F9A603"));
snackbar.show();
new Handler().postDelayed(new Runnable() {
@Override
public void run() {

goToList();

}
}, 1500);

}
}, 5000);
}

public void goToList() {
if (InternetConnection.checkConnection(getActivity().getApplicationContext())) {

new GetDataTask().execute();
View view = getActivity().findViewById(android.R.id.content);
Snackbar snackbar = Snackbar.make(view, "Connesso", Snackbar.LENGTH_SHORT);
// Change the Snackbar default background color
View snackbarView = snackbar.getView();
snackbarView.setBackgroundColor(Color.parseColor("#258039"));
snackbar.show();

} else {
check_connectivity();

}
}

class GetDataTask extends AsyncTask<Void, Void, Void> {

ProgressDialog dialog;

@Override
protected void onPreExecute() {
super.onPreExecute();
dialog = new ProgressDialog(getActivity());
dialog.setTitle("Un attimo di pazienza...");
dialog.setMessage("Carico le tue card");
dialog.show();
}

@Nullable
@Override
protected Void doInBackground(Void... params) {

JSONObject jsonObject = JSONParser.getDataById(unique_id);


try {


JSONArray array = jsonObject.getJSONArray(Keys.KEY_CONTACTS);


int lenArray = array.length();
if(lenArray > 0) {
for(int jIndex = 0; jIndex < lenArray; jIndex++) {


JSONObject innerObject = array.getJSONObject(jIndex);
String desc_dist = innerObject.getString(Keys.KEY_DIST);
String numero_card = innerObject.getString(Keys.KEY_NUM_CARD);
String totpunti_card = innerObject.getString(Keys.KEY_TOT_PUNTI);
String id_cd = innerObject.getString(Keys.KEY_ID_CD);


/**
* Adding name and phone concatenation in List...
*/
lstCards.add(new Cards( numero_card,desc_dist,totpunti_card,id_cd) );
} //fine FOR

}
}
} else {

}
} catch (JSONException je) {
Log.i(JSONParser.TAG, "" + je.getLocalizedMessage());
}
return null;
} // FINE doBackground

@Override
protected void onPostExecute(Void aVoid) {
super.onPostExecute(aVoid);
dialog.dismiss();
/**
* Checking if List size if more than zero then
* Update ListView
*/
if(lstCards.size() > 0) {
recyclerAdapter.notifyDataSetChanged();
} else {
View view = getActivity().findViewById(android.R.id.content);
Snackbar snackbar = Snackbar.make(view, "No Data Found", Snackbar.LENGTH_SHORT);
}
}
} // fine GetDataTask
}


RecyclerView



    import android.app.Activity;
import android.app.Dialog;
import android.app.Fragment;
import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
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.List;

public class RecycleTransCardAdapter extends RecyclerView.Adapter<RecycleTransCardAdapter.MyViewHolder> {


Context mContext;
List<InfoCard> mData;
Dialog myDialog;

public RecycleTransCardAdapter(Context mContext, List<InfoCard> mData) {
this.mContext = mContext;
this.mData = mData;
}

@NonNull
@Override
public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {

View v;
v = LayoutInflater.from(mContext).inflate(R.layout.item_trans_card, parent, false);
final MyViewHolder vHolder = new MyViewHolder(v);


return vHolder;
}

@Override
public void onBindViewHolder(@NonNull MyViewHolder holder, int position) {

// some code
}

@Override
public int getItemCount() {
return mData.size();
}

public static class MyViewHolder extends RecyclerView.ViewHolder {

// some code


public MyViewHolder(View itemView) {
super(itemView) ;
// some code

}
}

}


The error is in Fragment at this line:



recyclerAdapter.notifyDataSetChanged();



I have this error only on old OS Android
Example: 4.1 version










share|improve this question
















I have created an App with RecyclerView but it’s crash in the devices with 4.1 Android OS.
There is an alternative to RecyclerView or i can risolve the problem?



Thanks.



UPDATE



Activity



import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentTransaction;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.pm.PackageManager;
import android.os.Build;
import android.support.v4.app.ActivityCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

public class Dashboard extends AppCompatActivity {

private Activity activity;

private SharedPreferences pref;
SharedPreferences shared;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
pref = getPreferences(0);

Fragment fragment;
fragment = new DashboardFragment();
FragmentTransaction ft = getFragmentManager().beginTransaction();
ft.replace(R.id.fragment_frame, fragment);
ft.commit();

}//fine OnCreate


}// FINE CLASS


Fragment



import android.app.ProgressDialog;
import android.content.Context;
import android.content.SharedPreferences;
import android.graphics.Color;
import android.os.AsyncTask;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.Nullable;
import android.support.design.widget.Snackbar;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
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.ListView;

import com.xxx.parser.JSONParser;
import com.xxx.utils.InternetConnection;
import com.xxx.utils.Keys;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

public class FragmentCards extends android.app.Fragment {

View v;
private RecyclerView myrecyclerview;
private List<Cards> lstCards;
//

private SharedPreferences pref;
SharedPreferences shared;
String unique_id;

// Getting application context
Context context = getActivity();

//JSON
private ListView listView;
RecyclerViewAdapter recyclerAdapter;


public FragmentCards() {
}

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
v = inflater.inflate(R.layout.cards_fragment, container, false);
myrecyclerview = (RecyclerView) v.findViewById(R.id.card_rv);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
recyclerAdapter = new RecyclerViewAdapter(getContext(),lstCards);
}
myrecyclerview.setLayoutManager(new LinearLayoutManager(getActivity()));
myrecyclerview.setAdapter(recyclerAdapter);
return v;
// return null;
}

@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

//per visualizzare codice
pref = getActivity().getPreferences(0);
shared = getActivity().getSharedPreferences("A", Context.MODE_PRIVATE); // get the sharedpreference set named "A"
unique_id= shared.getString("unique_id","");
//
//
lstCards = new ArrayList<>();
//
goToList();

} // fine ON CREATE

public void check_connectivity(){
View view = getActivity().findViewById(android.R.id.content);
// Initialize a new Snackbar
Snackbar snackbar = Snackbar.make(view, "Connessione assente ... riprovo fra 5 secondi", Snackbar.LENGTH_INDEFINITE);
// Change the Snackbar default background color
View snackbarView = snackbar.getView();
snackbarView.setBackgroundColor(Color.parseColor("#FF0000"));
snackbar.show();

new Handler().postDelayed(new Runnable() {
@Override
public void run() {
View view = getActivity().findViewById(android.R.id.content);
Snackbar snackbar = Snackbar.make(view, "Connessione in corso ...", Snackbar.LENGTH_SHORT);
// Change the Snackbar default background color
View snackbarView = snackbar.getView();
snackbarView.setBackgroundColor(Color.parseColor("#F9A603"));
snackbar.show();
new Handler().postDelayed(new Runnable() {
@Override
public void run() {

goToList();

}
}, 1500);

}
}, 5000);
}

public void goToList() {
if (InternetConnection.checkConnection(getActivity().getApplicationContext())) {

new GetDataTask().execute();
View view = getActivity().findViewById(android.R.id.content);
Snackbar snackbar = Snackbar.make(view, "Connesso", Snackbar.LENGTH_SHORT);
// Change the Snackbar default background color
View snackbarView = snackbar.getView();
snackbarView.setBackgroundColor(Color.parseColor("#258039"));
snackbar.show();

} else {
check_connectivity();

}
}

class GetDataTask extends AsyncTask<Void, Void, Void> {

ProgressDialog dialog;

@Override
protected void onPreExecute() {
super.onPreExecute();
dialog = new ProgressDialog(getActivity());
dialog.setTitle("Un attimo di pazienza...");
dialog.setMessage("Carico le tue card");
dialog.show();
}

@Nullable
@Override
protected Void doInBackground(Void... params) {

JSONObject jsonObject = JSONParser.getDataById(unique_id);


try {


JSONArray array = jsonObject.getJSONArray(Keys.KEY_CONTACTS);


int lenArray = array.length();
if(lenArray > 0) {
for(int jIndex = 0; jIndex < lenArray; jIndex++) {


JSONObject innerObject = array.getJSONObject(jIndex);
String desc_dist = innerObject.getString(Keys.KEY_DIST);
String numero_card = innerObject.getString(Keys.KEY_NUM_CARD);
String totpunti_card = innerObject.getString(Keys.KEY_TOT_PUNTI);
String id_cd = innerObject.getString(Keys.KEY_ID_CD);


/**
* Adding name and phone concatenation in List...
*/
lstCards.add(new Cards( numero_card,desc_dist,totpunti_card,id_cd) );
} //fine FOR

}
}
} else {

}
} catch (JSONException je) {
Log.i(JSONParser.TAG, "" + je.getLocalizedMessage());
}
return null;
} // FINE doBackground

@Override
protected void onPostExecute(Void aVoid) {
super.onPostExecute(aVoid);
dialog.dismiss();
/**
* Checking if List size if more than zero then
* Update ListView
*/
if(lstCards.size() > 0) {
recyclerAdapter.notifyDataSetChanged();
} else {
View view = getActivity().findViewById(android.R.id.content);
Snackbar snackbar = Snackbar.make(view, "No Data Found", Snackbar.LENGTH_SHORT);
}
}
} // fine GetDataTask
}


RecyclerView



    import android.app.Activity;
import android.app.Dialog;
import android.app.Fragment;
import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
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.List;

public class RecycleTransCardAdapter extends RecyclerView.Adapter<RecycleTransCardAdapter.MyViewHolder> {


Context mContext;
List<InfoCard> mData;
Dialog myDialog;

public RecycleTransCardAdapter(Context mContext, List<InfoCard> mData) {
this.mContext = mContext;
this.mData = mData;
}

@NonNull
@Override
public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {

View v;
v = LayoutInflater.from(mContext).inflate(R.layout.item_trans_card, parent, false);
final MyViewHolder vHolder = new MyViewHolder(v);


return vHolder;
}

@Override
public void onBindViewHolder(@NonNull MyViewHolder holder, int position) {

// some code
}

@Override
public int getItemCount() {
return mData.size();
}

public static class MyViewHolder extends RecyclerView.ViewHolder {

// some code


public MyViewHolder(View itemView) {
super(itemView) ;
// some code

}
}

}


The error is in Fragment at this line:



recyclerAdapter.notifyDataSetChanged();



I have this error only on old OS Android
Example: 4.1 version







android android-recyclerview






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jan 2 at 13:33







Simsar78

















asked Jan 2 at 6:16









Simsar78Simsar78

57




57








  • 5





    What is crash report? Where is stacktrace? Where is code? Atleast post something.

    – Piyush
    Jan 2 at 6:17








  • 1





    Post your stacktrace here so we can help.

    – Mahmoud Elshamy
    Jan 2 at 6:24






  • 2





    RecyclerView is part of v7 support libs and it works well with till api level 7. there may be network and thread issue causing error .

    – Saurabh Bhandari
    Jan 2 at 6:25














  • 5





    What is crash report? Where is stacktrace? Where is code? Atleast post something.

    – Piyush
    Jan 2 at 6:17








  • 1





    Post your stacktrace here so we can help.

    – Mahmoud Elshamy
    Jan 2 at 6:24






  • 2





    RecyclerView is part of v7 support libs and it works well with till api level 7. there may be network and thread issue causing error .

    – Saurabh Bhandari
    Jan 2 at 6:25








5




5





What is crash report? Where is stacktrace? Where is code? Atleast post something.

– Piyush
Jan 2 at 6:17







What is crash report? Where is stacktrace? Where is code? Atleast post something.

– Piyush
Jan 2 at 6:17






1




1





Post your stacktrace here so we can help.

– Mahmoud Elshamy
Jan 2 at 6:24





Post your stacktrace here so we can help.

– Mahmoud Elshamy
Jan 2 at 6:24




2




2





RecyclerView is part of v7 support libs and it works well with till api level 7. there may be network and thread issue causing error .

– Saurabh Bhandari
Jan 2 at 6:25





RecyclerView is part of v7 support libs and it works well with till api level 7. there may be network and thread issue causing error .

– Saurabh Bhandari
Jan 2 at 6:25












2 Answers
2






active

oldest

votes


















0














RecyclerView is part of Support Library which you can use it on older devices too. Try to use AndroidX package androidx.recyclerview.widget.RecyclerView
instead of support library.



implementation 'androidx.recyclerview:recyclerview:1.0.0'


For more detail about AndroidX explore this address:
https://developer.android.com/jetpack/androidx/






share|improve this answer































    0














    recyclerview is backwards-compatible, assuming you include the support library for RecyclerView v7 in your application's build.gradle file:



    compile "com.android.support:recyclerview-v7:21.0.0.+" 


    That one will work down as low as Android 2.1 (API level 7), hence the name v7



    So you can use recyclerview in device running lower as on Android 2.1 (API level 7), hence the name v7



    try adding above library and run app again if still crash happen
    update the question with error log of app.






    share|improve this answer

























      Your Answer






      StackExchange.ifUsing("editor", function () {
      StackExchange.using("externalEditor", function () {
      StackExchange.using("snippets", function () {
      StackExchange.snippets.init();
      });
      });
      }, "code-snippets");

      StackExchange.ready(function() {
      var channelOptions = {
      tags: "".split(" "),
      id: "1"
      };
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function() {
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled) {
      StackExchange.using("snippets", function() {
      createEditor();
      });
      }
      else {
      createEditor();
      }
      });

      function createEditor() {
      StackExchange.prepareEditor({
      heartbeatType: 'answer',
      autoActivateHeartbeat: false,
      convertImagesToLinks: true,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: 10,
      bindNavPrevention: true,
      postfix: "",
      imageUploader: {
      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
      allowUrls: true
      },
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      });


      }
      });














      draft saved

      draft discarded


















      StackExchange.ready(
      function () {
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54002029%2falternative-to-recyclerview-for-older-sdk%23new-answer', 'question_page');
      }
      );

      Post as a guest















      Required, but never shown

























      2 Answers
      2






      active

      oldest

      votes








      2 Answers
      2






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      0














      RecyclerView is part of Support Library which you can use it on older devices too. Try to use AndroidX package androidx.recyclerview.widget.RecyclerView
      instead of support library.



      implementation 'androidx.recyclerview:recyclerview:1.0.0'


      For more detail about AndroidX explore this address:
      https://developer.android.com/jetpack/androidx/






      share|improve this answer




























        0














        RecyclerView is part of Support Library which you can use it on older devices too. Try to use AndroidX package androidx.recyclerview.widget.RecyclerView
        instead of support library.



        implementation 'androidx.recyclerview:recyclerview:1.0.0'


        For more detail about AndroidX explore this address:
        https://developer.android.com/jetpack/androidx/






        share|improve this answer


























          0












          0








          0







          RecyclerView is part of Support Library which you can use it on older devices too. Try to use AndroidX package androidx.recyclerview.widget.RecyclerView
          instead of support library.



          implementation 'androidx.recyclerview:recyclerview:1.0.0'


          For more detail about AndroidX explore this address:
          https://developer.android.com/jetpack/androidx/






          share|improve this answer













          RecyclerView is part of Support Library which you can use it on older devices too. Try to use AndroidX package androidx.recyclerview.widget.RecyclerView
          instead of support library.



          implementation 'androidx.recyclerview:recyclerview:1.0.0'


          For more detail about AndroidX explore this address:
          https://developer.android.com/jetpack/androidx/







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Jan 2 at 6:48









          Ehsan MashhadiEhsan Mashhadi

          771619




          771619

























              0














              recyclerview is backwards-compatible, assuming you include the support library for RecyclerView v7 in your application's build.gradle file:



              compile "com.android.support:recyclerview-v7:21.0.0.+" 


              That one will work down as low as Android 2.1 (API level 7), hence the name v7



              So you can use recyclerview in device running lower as on Android 2.1 (API level 7), hence the name v7



              try adding above library and run app again if still crash happen
              update the question with error log of app.






              share|improve this answer






























                0














                recyclerview is backwards-compatible, assuming you include the support library for RecyclerView v7 in your application's build.gradle file:



                compile "com.android.support:recyclerview-v7:21.0.0.+" 


                That one will work down as low as Android 2.1 (API level 7), hence the name v7



                So you can use recyclerview in device running lower as on Android 2.1 (API level 7), hence the name v7



                try adding above library and run app again if still crash happen
                update the question with error log of app.






                share|improve this answer




























                  0












                  0








                  0







                  recyclerview is backwards-compatible, assuming you include the support library for RecyclerView v7 in your application's build.gradle file:



                  compile "com.android.support:recyclerview-v7:21.0.0.+" 


                  That one will work down as low as Android 2.1 (API level 7), hence the name v7



                  So you can use recyclerview in device running lower as on Android 2.1 (API level 7), hence the name v7



                  try adding above library and run app again if still crash happen
                  update the question with error log of app.






                  share|improve this answer















                  recyclerview is backwards-compatible, assuming you include the support library for RecyclerView v7 in your application's build.gradle file:



                  compile "com.android.support:recyclerview-v7:21.0.0.+" 


                  That one will work down as low as Android 2.1 (API level 7), hence the name v7



                  So you can use recyclerview in device running lower as on Android 2.1 (API level 7), hence the name v7



                  try adding above library and run app again if still crash happen
                  update the question with error log of app.







                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited Jan 2 at 13:07

























                  answered Jan 2 at 6:37









                  Rohit ChauhanRohit Chauhan

                  587417




                  587417






























                      draft saved

                      draft discarded




















































                      Thanks for contributing an answer to Stack Overflow!


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid



                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.


                      To learn more, see our tips on writing great answers.




                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function () {
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54002029%2falternative-to-recyclerview-for-older-sdk%23new-answer', 'question_page');
                      }
                      );

                      Post as a guest















                      Required, but never shown





















































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown

































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown







                      Popular posts from this blog

                      android studio warns about leanback feature tag usage required on manifest while using Unity exported app?

                      SQL update select statement

                      'app-layout' is not a known element: how to share Component with different Modules