i want to use card view inside my list view which is inside a fragment to show 2 variable, i already can connect to database and show only one variable "nama_matkul" from json into my list view, but i need to show "nama_matkul" and "sks" so i am creating a cardview but i am confused to make costum adapter for it
this is my json response
{
"error": false,
"matkul_mhs": [{
"nama_matkul": "matkul_3",
"sks": 3
}]}
this is my method inside fragment to initialize list view
public void initListView1(){
Call<MatkulMhs> getMatkulMhs = mApiService.getMatkulMhs(
mPrefs.getUserID());
getMatkulMhs.enqueue(new Callback<MatkulMhs>() {
@Override
public void onResponse(Call<MatkulMhs> call, Response<MatkulMhs> response) {
boolean iserror_ = response.body().getError();
if (iserror_ == false) {
List<Matkul_Mhs> list = new ArrayList<>();
list = response.body().getMatkulMhs();
nama_matkul = new String[list.size()];
for (int i =0;i<list.size();i++) {
nama_matkul[i] = list.get(i).getNamaMatkul();
}
ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1, nama_matkul);
listview1.setAdapter(adapter);
}
}
this is my cardview (form_mhs_02_cardview.xml)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<androidx.cardview.widget.CardView
android:id="@+id/card_view"
android:layout_width="fill_parent"
android:layout_height="100dp"
android:layout_margin="5dp"
card_view:cardCornerRadius="2dp"
card_view:contentPadding="10dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="@drawable/rounded_sqrwht"
android:padding="10dp"
>
<TextView
android:id="@+id/tv_matkul"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Mata Kuliah 1 "
android:textSize="25dp"
android:textColor="@color/colorPrimaryDark"
android:textStyle=""
android:singleLine="true"
/>
<TextView
android:id="@+id/tv_sks"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="3 sks"
android:textSize="20dp"
android:textColor="@color/black"
android:singleLine="true"
/>
</LinearLayout>
</androidx.cardview.widget.CardView>
</LinearLayout>
i found this answer from another post but still confuse to change into my needs ,post can be found at custom adapter for listview in fragment Android
public class CustomAdapter extends ArrayAdapter<HashMap<String,String>>
{
private ArrayList<HashMap<String,String>> callLogData;
private Context context;
private int resource;
private View view;
private Holder holder;
private HashMap<String,String> hashMap;
public CustomAdapter(Context context, int resource,ArrayList<HashMap<String, String>> objects)
{
super(context, resource, objects);
this.context=context;
this.resource=resource;
this.callLogData=objects;
}
@Override
public View getView(int position, View convertView, ViewGroup parent)
{
LayoutInflater inflater=(LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view=inflater.inflate(resource, parent,false);
holder=new Holder();
holder.text_matkul=(TextView)view.findViewById(R.id.tv_matkul);
holder.text_sks=(TextView)view.findViewById(R.id.tv_sks);
hashMap=callLogData.get(position);
Date date=new Date(Long.parseLong(hashMap.get(CallLog.Calls.DATE)));
java.text.DateFormat dateFormat=DateFormat.getDateFormat(context);
java.text.DateFormat timeformat=DateFormat.getTimeFormat(context);
holder.text_matkul.setText(hashMap.get(CallLog.Calls.NUMBER));
holder.text_sks.setText(timeformat.format(date));
return view;
}
public class Holder
{
TextView text_matkul;
TextView text_sks;
}
}
@togan-jr 请在此链接https://medium.com/@Pang_Yao/android-fragment-use-recyclerview-cardview-4bc10beac446上查看此示例,并尝试按照演示示例实施。
public class CustomAdapter extends ArrayAdapter<HashMap<String, String>> {
private final ArrayList<HashMap<String, String>> callLogData;
private final Context context;
private final int resource;
/*
This TODO code won't cause any issue even if not done.
*/
//TODO : Check below comment
private View view;
//TODO :: NEED TO REMOVE as adapter handle multiple items therefore no meaning in having single field of holder object
private Holder holder;
//TODO :: NEED TO REMOVE same as above. you should get item from based on position provided in getView method no need to keep reference to it
private HashMap<String, String> hashMap;
public CustomAdapter(Context context, int resource, ArrayList<HashMap<String, String>> objects) {
super(context, resource, objects);
this.context = context;
this.resource = resource;
callLogData = objects;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
//Added local instance of holder
View view = inflater.inflate(resource, parent, false);
//Added local instance of holder
Holder holder = new Holder();
holder.text_matkul = (TextView) view.findViewById(R.id.tv_matkul);
holder.text_sks = (TextView) view.findViewById(R.id.tv_sks);
//Added local instance of holder
HashMap<String, String> hashMap = callLogData.get(position);
//Commenting below code
/*
Date date=new Date(Long.parseLong(hashMap.get(CallLog.Calls.DATE)));
java.text.DateFormat dateFormat=DateFormat.getDateFormat(context);
java.text.DateFormat timeformat=DateFormat.getTimeFormat(context);
*/
holder.text_matkul.setText(hashMap.get("nama_matkul"));
holder.text_sks.setText("sks");
return view;
}
public class Holder {
TextView text_matkul;
TextView text_sks;
}
}
public void initListView1() {
Call<MatkulMhs> getMatkulMhs = mApiService.getMatkulMhs(
mPrefs.getUserID());
getMatkulMhs.enqueue(new Callback<MatkulMhs>() {
@Override
public void onResponse(Call<MatkulMhs> call, Response<MatkulMhs> response) {
boolean iserror_ = response.body().getError();
if (iserror_ == false) {
List<Matkul_Mhs> list = new ArrayList<>();
list = response.body().getMatkulMhs();
ArrayList<HashMap<String, String>> nama_matkul = new ArrayList<>();
for (int i = 0; i < list.size(); i++) {
HashMap<String, String> item = new HashMap<String, String>();
item.put("nama_matkul", list.get(i).getNamaMatkul());
item.put("sks", list.get(i).getSKS());
nama_matkul.add(item);
}
//Note: layout resource changed to your card view and ArrayAdapter to CustomAdapter
CustomAdapter adapter = new CustomAdapter(getActivity(), R.layout.form_mhs_02_cardview, nama_matkul);
listview1.setAdapter(adapter);
}
}
}
}
Use your Custom Adapter Class File Before SetAdapter.
CustomAdapter adapter = new CustomAdapter(getActivity(), android.R.layout.simple_list_item_1, nama_matkul);
public void initListView1(){
Call<MatkulMhs> getMatkulMhs = mApiService.getMatkulMhs(
mPrefs.getUserID());
getMatkulMhs.enqueue(new Callback<MatkulMhs>() {
@Override
public void onResponse(Call<MatkulMhs> call, Response<MatkulMhs> response) {
boolean iserror_ = response.body().getError();
if (iserror_ == false) {
List<Matkul_Mhs> list = new ArrayList<>();
list = response.body().getMatkulMhs();
nama_matkul = new String[list.size()];
for (int i =0;i<list.size();i++) {
nama_matkul[i] = list.get(i).getNamaMatkul();
}
CustomAdapter <String> adapter = new CustomAdapter<String>(getActivity(), android.R.layout.simple_list_item_1, nama_matkul);
listview1.setAdapter(adapter);
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.