我想在列表视图中使用卡片视图,该视图在一个片段中以显示2个变量,我已经可以连接到数据库,并且仅将json中的一个变量“ nama_matkul”显示到我的列表视图中,但是我需要显示“ nama_matkul”和“ sks”,因此我正在创建cardview,但我很困惑为此制作Costum适配器
这是我的json响应
{
"error": false,
"matkul_mhs": [{
"nama_matkul": "matkul_3",
"sks": 3
}]}
这是我在片段内初始化列表视图的方法
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);
}
}
这是我的名片视图(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>
我从另一篇文章中找到了这个答案,但仍然对改变自己的需求感到困惑 ,帖子可以在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;
}
}
答案 0 :(得分:0)
@ togan-j-r,请在链接https://medium.com/@Pang_Yao/android-fragment-use-recyclerview-cardview-4bc10beac446上查看此示例,并尝试按照演示示例进行实施。
答案 1 :(得分:0)
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);
}
}
}
}
答案 2 :(得分:0)
在SetAdapter之前使用自定义适配器类文件。
CustomAdapter适配器=新的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);
}
}