我已经用RecyclerView创建了一个应用程序,但是在装有4.1 Android OS的设备中它崩溃了。 有RecyclerView的替代方法,还是我可以解决问题?
谢谢。
更新
活动
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
片段
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
}
}
}
此行中的片段中有错误:
recyclerAdapter.notifyDataSetChanged();
我仅在旧OS Android上有此错误 示例:4.1版本
答案 0 :(得分:0)
recyclerview是向后兼容的,前提是您在应用程序的build.gradle文件中包括了RecyclerView v7的支持库:
compile "com.android.support:recyclerview-v7:21.0.0.+"
该版本将在Android 2.1(API级别7)下运行,因此命名为v7
因此,您可以在运行速度低于Android 2.1(API级别7)的设备上使用recyclerview,因此名称为v7
尝试添加上述库,如果仍然崩溃,则再次运行应用程序 使用应用程序的错误日志更新问题。
答案 1 :(得分:0)
RecyclerView是支持库的一部分,您也可以在较旧的设备上使用它。尝试使用AndroidX软件包<HttpPost()>
而不是支持库。
androidx.recyclerview.widget.RecyclerView
有关AndroidX的更多详细信息,请访问以下地址: https://developer.android.com/jetpack/androidx/