如何创建自定义适配器以将 cardview 用于片段内的列表视图
how to create custom adapter to use cardview for list view inside fragment
我想在片段内的列表视图中使用卡片视图来显示 2 个变量,我已经可以连接到数据库并仅在列表中显示 json 中的一个变量 "nama_matkul"视图,但我需要显示 "nama_matkul" 和 "sks" 所以我正在创建一个 cardview 但我很困惑为它制作服装适配器
这是我的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>
我从另一个 post 那里找到了这个答案,但仍然很困惑,无法满足我的需求
,post 可以在
找到
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-j-r 请查看此 link 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);
}
}
}
}
在 SetAdapter 之前使用自定义适配器 Class 文件。
CustomAdapter 适配器 = 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);
}
}
我想在片段内的列表视图中使用卡片视图来显示 2 个变量,我已经可以连接到数据库并仅在列表中显示 json 中的一个变量 "nama_matkul"视图,但我需要显示 "nama_matkul" 和 "sks" 所以我正在创建一个 cardview 但我很困惑为它制作服装适配器
这是我的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>
我从另一个 post 那里找到了这个答案,但仍然很困惑,无法满足我的需求
,post 可以在
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-j-r 请查看此 link 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);
}
}
}
}
在 SetAdapter 之前使用自定义适配器 Class 文件。
CustomAdapter 适配器 = 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);
}
}