Far as I understand what the error say is it expects me to provide a List
instead of a ListView
but casting to java.util.List does not solve the problem, instead it just creates more errors.
what should I do in this case?
My end goal is to make a ListView that shows the result of users query with each iteration any help to point out what is actually goin on and what do I have to do is very appreciated
Here is the relevant code :
Varible :
private List<Absensi> list_absensi = new ArrayList<Absensi>();
Initialization :
list_absensi = (ListView) findViewById(R.id.ListAbsensi);
//the problem is in this line of code
XML ID for ListView :
<ListView
android:id="@ id/ListAbsensi"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
I also have adapter for this :
public class AbsensiAdapter extends ArrayAdapter<Absensi> {
private Context context;
private List<Absensi> absensis;
Picasso mypicasso;
public AbsensiAdapter(@NonNull Context context, @LayoutRes int resource, @NonNull List<Absensi> objects, Picasso myotherpicasso) {
super(context, resource, objects);
this.context = context;
this.absensis = objects;
this.mypicasso = myotherpicasso;
}
@Override
public View getView(final int pos, View convertview, ViewGroup parent){
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View rowView = inflater.inflate(R.layout.list_absensi, parent, false);
TextView txtabsenID = (TextView) rowView.findViewById(R.id.txtAViewId);
TextView txtasbsenTanggal = (TextView) rowView.findViewById(R.id.txtAViewTanggal);
TextView txtasbsenKelas = (TextView) rowView.findViewById(R.id.txtAViewKelas);
TextView txtasbsenGuru = (TextView) rowView.findViewById(R.id.txtAViewGuru);
txtabsenID.setText(String.format("id : %s", absensis.get(pos).getID()));
txtasbsenTanggal.setText(String.format("Tanggal : %s",absensis.get(pos).getID()));
txtasbsenKelas.setText(String.format("Kelas : %s",absensis.get(pos).getID()));
txtasbsenGuru.setText(String.format("Guru : %s",absensis.get(pos).getID()));
rowView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//start activity AbsensiDetailActivity
Intent intent = new Intent(context, AbsensiDetailActivity.class);
intent.putExtra("absensi_id", String.valueOf(absensis.get(pos).getID()));
intent.putExtra("absensi_tanggal", absensis.get(pos).getTanggal());
intent.putExtra("absensi_kelas", absensis.get(pos).getKelasName());
intent.putExtra("absensi_guru", absensis.get(pos).getGuruName());
context.startActivity(intent);
}
});
return rowView;
}
}
list_absensi.xml for showing the result
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="@ id/txtAViewId"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="3dp"
tools:text="ID"
android:visibility="gone" />
<TextView
android:id="@ id/txtAViewTanggal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="3dp"
tools:text="Tanggal" />
<TextView
android:id="@ id/txtAViewKelas"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="3dp"
tools:text="Kelas" />
<TextView
android:id="@ id/txtAViewGuru"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="3dp"
tools:text="Guru" />
<TextView
android:id="@ id/txtAViewPelajaran"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="3dp"
tools:text="Pelajaran" />
</LinearLayout>
CodePudding user response:
This line is your root of the problems:
list_absensi = (ListView) findViewById(R.id.ListAbsensi);
Function findViewById
returns object type of View. list_absensi
is a view which at this case it is a ListView
(you declared it in xml).
So the top declaration for your ListView should be:
private ListView list_absensi;
I have a strong feeling that you mess view with a container for storing data. ListView
is for displaying the items. If you want to store data - you must use a list. At this case your should at top of your declarations add a new variable for storing data should be:
private List<Absensi> absensis = new ArrayList<Absensi>();