I'm trying to use a custom listview in a fragment, I've already searched several pages and I've already searched in this forum, but I can not locate myself to adapt it to my own.
My problem is that in code I can not get the custom adapter to compile, the application "thunders" when the fragment is inflated, and with an added "(AppCompatActivity)" it does not "thunder" but nothing shows the listview.
Is there anything I can do? Should I try something else?
This is the code of the fragment in which I want to show the custom listview:
private ArrayList<ListaHistorial> listaCalificaciones;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_historial, container, false);
listaCalificaciones = new ArrayList<>();
listaCalificaciones.add(new ListaHistorial("Math", 10, "9/1/2017 13:45"));
listaCalificaciones.add(new ListaHistorial("Math", 8, "7/5/2017 10:50"));
listaCalificaciones.add(new ListaHistorial("Marh", 4, "7/7/2017 16:30"));
listaCalificaciones.add(new ListaHistorial("Math", 5, "4/15/2017 15:00"));
listaCalificaciones.add(new ListaHistorial("Math", 7, "3/10/2017 10:29"));
AdaptadorHistorial adaptador = new AdaptadorHistorial(HistorialFragment.this.getActivity(), android.R.layout.simple_list_item_1, listaCalificaciones);
//AdaptadorHistorial adaptador = new AdaptadorHistorial(, android.R.layout.simple_list_item_1, listaCalificaciones);
ListView list1 = (ListView)view.findViewById(R.id.list1);
list1.setAdapter(adaptador);
return view;
}
private class AdaptadorHistorial extends ArrayAdapter<ListaHistorial>{
AppCompatActivity appCompatActivity;
AdaptadorHistorial(AppCompatActivity context){
super(context, R.layout.lista_historial, listaCalificaciones);
appCompatActivity = context;
}
/*public AdaptadorHistorial(FragmentActivity activity, int simple_list_item_1, ArrayList<ListaHistorial> listaCalificaciones) {
super(activity, simple_list_item_1, listaCalificaciones);
}*/
public View getView(int position, View convertView, ViewGroup parent){
LayoutInflater inflater = appCompatActivity.getLayoutInflater();
View item = inflater.inflate(R.layout.lista_historial, null);
TextView tv1, tv2, tv3;
tv1 = (TextView)item.findViewById(R.id.tv1);
tv2 = (TextView)item.findViewById(R.id.tv2);
tv3 = (TextView)item.findViewById(R.id.tv3);
tv1.setText(listaCalificaciones.get(position).getExamen());
tv2.setText(listaCalificaciones.get(position).getCalificacion());
tv3.setText(listaCalificaciones.get(position).getFecha());
return item;
}
}
public interface OnFragmentInteractionListener {
void onFragmentInteraction(Uri uri);
}
This is the XML with the interface of each listview object
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/tv1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="" />
<TextView
android:id="@+id/tv2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="" />
<TextView
android:id="@+id/tv3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="" />
And this is the class you create to represent each object in the listview
class ListHistorial { private String exam, date; private int qualification;
public ListaHistorial(String examen, int calificacion, String fecha)
{
this.examen = examen;
this.calificacion = calificacion;
this.fecha = fecha;
}
public String getExamen() {
return examen;
}
public int getCalificacion(){
return calificacion;
}
public String getFecha(){
return fecha;
}
}
This is where it shows the error, what it does not compile:
AdaptadorHistorial adaptador = new AdaptadorHistorial(HistorialFragment.this.getActivity(), android.R.layout.simple_list_item_1, listaCalificaciones);
I loose this error when trying to run the program:
Error:(39, 40) error: constructor AdaptadorHistorial in class HistorialFragment.AdaptadorHistorial cannot be applied to given types;
required: AppCompatActivity found: FragmentActivity, int, ArrayList reason: current and formal argument lists differ in length
If I need to show something else, please tell me. In advance, thank you very much.