Estoy intentando cargar los datos de una consulta SQLite en un recyclerView. Por mas que lo intento no consigo hacerlo y la app se detiene.
Al momento de retornar
View rootView = inflater.inflate(R.layout.fragment_inicio, container, false);
La app se detiene, en return rootView en el public View onCreateView del Fragment.
public class InicioFragment extends Fragment {
public RecyclerView recyclerView;
public RecyclerView.Adapter adapter;
public RecyclerView.LayoutManager layoutManager;
public SearchView searchView;
public Context context;
public ArrayList<InicioItem> arrayList;
Utilidades utilidades;
private conexion con;
SQLiteDatabase bd;
String id;
public InicioFragment() { }
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_inicio, container, false);
id=getArguments().getString("idU");
utilidades= new Utilidades();
recyclerView = (RecyclerView) rootView.findViewById(R.id.rclvInicio);
recyclerView.setHasFixedSize(true);
searchView = (SearchView) rootView.findViewById(R.id.searchInicio);
this.context = this.getContext();
arrayList = new ArrayList<InicioItem>();
//Obtener las listas
if (!GetList())
adapter = new InicioRecyclerAdapter(context, arrayList);
recyclerView.setAdapter(adapter);
layoutManager = new LinearLayoutManager(getActivity());
recyclerView.setLayoutManager(layoutManager);
return rootView;
}
// OBTENER LISTAS
public boolean GetListCreditos() {
con = new conexion(this.context, "bd_C", null, 1);
bd=con.getReadableDatabase();
try {
String query="Select * from " + utilidades.Tnicio +" where Id= '"+id+"'";
Cursor cursor = bd.rawQuery(query, null);
if (cursor != null) {
if (cursor.moveToFirst() == true) {
do {
for (int i = 0; i < cursor.getCount(); i++) {
arrayList.add(new InicioItem(inicio));
}
} while (cursor.moveToNext());
}
}
cursor.close();
}
catch (Exception e) {
e.toString();
}
bd.close();
return true;
}
}
Adapter
public class InicioRecyclerAdapter extends RecyclerView.Adapter<InicioRecyclerAdapter.InicioRecyclerViewHolder> implements Filterable {
ArrayList<InicioItem> arrayList;
private static ArrayList<InicioItem> arrayListInicio = new ArrayList<InicioItem>();
private LayoutInflater inflater;
private Context context;
public InicioRecyclerAdapter(Context context, ArrayList<InicioItem> arrayList) {
this.context = context;
inflater = LayoutInflater.from(context);
this.arrayList = arrayList;
this.arrayListInicio = arrayList;
}
@Override
public InicioRecyclerViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = inflater.inflate(R.layout.item_inicio_layout, parent, false);
InicioRecyclerViewHolder inicioRecyclerViewHolder = new InicioRecyclerViewHolder(view);
return inicioRecyclerViewHolder;
}
@Override
public void onBindViewHolder(InicioRecyclerViewHolder holder, int position) {
InicioItem inicioItem = arrayList.get(position);
holder.imgvInicioImgBackground.setImageResource(inicioItem.imgBack ground());
holder.imgvInicioImg.setImageResource(inicioItem.img());
holder.txtvInicioNombre.setText(inicioItem.nombre());
}
public void setArrayList(ArrayList<InicioItem> arrayList) {
this.arrayListInicio = arrayList;
}
public class InicioRecyclerViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
ImageView imgvInicioImgBackground, imgvInicioImg;
TextView txtvInicioNombre;
public InicioRecyclerViewHolder(View view) {
super(view);
view.setOnClickListener(this);
imgvInicioImgBackground = (ImageView) view.findViewById(R.id.inicio_imgBackground);
imgvInicioImg = (ImageView) view.findViewById(R.id.inicio_img);
txtvInicioNombre = (TextView) view.findViewById(R.id.inicio_nombre)
}
}
Clase InicioItem
public class InicioItem {
public InicioItem(Utilidades inicio) {
this._inicio = inicio;
}
private Utilidades _inicio;
public Utilidades inicio(){
return _inicio;
}
public int imgBackground(){
int imgbackground = R.drawable.background_item_red;
if(_inicio != null){
switch (Integer.parseInt(_inicio.Estatus)){
case 0:
imgbackground = R.drawable.background_item_red;
break;
case 1:
imgbackground = R.drawable.background_item_teal;
break;
case 2:
imgbackground = R.drawable.background_item_blue;
break;
}
}
return imgbackground;
}
public int img(){
int img = R.drawable.ic_item_no_asignado;
try{ if(_inicio != null){
switch (Integer.parseInt(_inicio.Estatus)){
case 0:
img = R.drawable.ic_item_no_asignado;
break;
case 1:
img = R.drawable.ic_item_activo;
break;
case 2:
img = R.drawable.ic_item_convenio_promesa_pago;
break;
}
}catch (Exception e){
e.printStackTrace();
}
return img;
}
public String nombre(){
if(_inicio != null)
return _inicio.Nombre.toUpperCase();
return "No hay registro";
}
}