Я учусь создавать приложения для Android, но при работе приложения я сталкиваюсь с некоторыми ошибками. Я использую recyclerView внутри фрагмента, потому что я использую меню навигации снизу.
Вот код.
CustomAdapterRecyclerView.java
package com.aphriellart.bri_kredit;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import androidx.recyclerview.widget.RecyclerView;
public class CustomAdapterRecycleView extends RecyclerView.Adapter<CustomAdapterRecycleView.ViewHolder> { //Error Logcat Here
private static final String TAG = "CustomAdapter";
private String[]mDataSet,mDataSet2,mdataSet3;
private int[]mDataSet4;
public static class ViewHolder extends RecyclerView.ViewHolder{
private final TextView textnasabah, textjob, textalamat;
private final ImageView photo;
public ViewHolder(View v){
super(v);
//Tentukan Click listener for the ViewHolder's View
v.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Log.d(TAG, "Element " + getPosition() + " clicked.");
}
});
textnasabah = (TextView) v.findViewById(R.id.nasabah);
textjob = (TextView) v.findViewById(R.id.pekerjaan);
textalamat = (TextView) v.findViewById(R.id.alamat);
photo = (ImageView) v.findViewById(R.id.photothumbnails);
}
public TextView getTextnasabah(){
return textnasabah;
}
public TextView getTextjob(){
return textjob;
}public TextView getTextalamat(){
return textalamat;
}public ImageView getPhoto(){
return photo;
}
}
//END_INCLUDE(RecyclerViewSampleViewHolder)
//Inisialisasi Data set
public CustomAdapterRecycleView(String[] dataSet, String[] dataSet2, String[] dataSet3, int[] dataSet4){
this.mDataSet = dataSet;
this.mDataSet2 = dataSet2;
this.mdataSet3 = dataSet3;
this.mDataSet4 = dataSet4;
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
//buat view baru
View v = LayoutInflater.from(viewGroup.getContext())
.inflate(R.layout.list_view, viewGroup,false);
return new ViewHolder(v);
}
@Override
public void onBindViewHolder(ViewHolder viewHolder, final int position) {
Log.d(TAG,"Element " + position + " set.");
//dapatkan elemnt dari dataset
viewHolder.getTextnasabah().setText(mDataSet[position]); //Error Logcat here
viewHolder.getTextjob().setText(mDataSet2[position]);
viewHolder.getTextalamat().setText(mdataSet3[position]);
viewHolder.getPhoto().setImageResource(mDataSet4[position]);
}
@Override
public int getItemCount() {
return mDataSet.length;
}
}
И это
BerandaFragment.java
package com.aphriellart.bri_kredit;
import android.os.Bundle;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
/**
* A simple {@link Fragment} subclass.
*/
public class BerandaFragment extends Fragment {
private static final String TAG = "RecyclerViewFragment";
private static final String KEY_LAYOUT_MANAGER = "layoutManager";
private static final int SPAN_COUNT = 2;
private static final int DATASET_COUNT = 60; //menampilkan data sebanyak value
private enum LayoutManagerType{
GRID_LAYOUT_MANAGER,
LINEAR_LAYOUT_MANAGER
}
protected LayoutManagerType mCurrentLayoutManagerType;
protected RecyclerView mRecyclerView;
protected CustomAdapterRecycleView mAdapter;
protected RecyclerView.LayoutManager mLayoutManager;
protected String[] mDataSet, mDataSet2, mDataSet3;
protected int[]mDataSet4;
int[]photo = {R.drawable.photothumbnails, R.drawable.photothumbnails, R.drawable.photothumbnails};
String [] nasabah = {"Michiko","Hell Ember","Blood Queen"};
String [] job = {"Pemandu Lagu","Tukang Boneka","Rias Pengantin"};
String [] alamat = {"White Sand Street Asylum","Moonlit River Park","Sacred Heart Hospital"};
@Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
//inisialisasi dataset
initDataset();
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_beranda, container, false);
rootView.setTag(TAG);
//BEGIN_INCLUDE inisialisasi recyclerView
mRecyclerView = (RecyclerView) rootView.findViewById(R.id.listpengajuanhariini);
mLayoutManager = new LinearLayoutManager(getActivity());
mCurrentLayoutManagerType = LayoutManagerType.LINEAR_LAYOUT_MANAGER;
if(savedInstanceState !=null){
//restore saved layout manager type
mCurrentLayoutManagerType = (LayoutManagerType) savedInstanceState
.getSerializable(KEY_LAYOUT_MANAGER);
}
setRecyclerViewLayoutManager(mCurrentLayoutManagerType);
mAdapter = new CustomAdapterRecycleView(mDataSet,mDataSet2,mDataSet3,mDataSet4);
//mengatur customadapter sebagai adapter untuk recyclerview
mRecyclerView.setAdapter(mAdapter);
// Inflate the layout for this fragment
return rootView;
}
public void setRecyclerViewLayoutManager(LayoutManagerType layoutManagerType) {
int scrollPosition = 0;
//jika layout manager sudah diatur, ambil scrool position saat ini
if (mRecyclerView.getLayoutManager()!=null){
scrollPosition = ((LinearLayoutManager)mRecyclerView.getLayoutManager())
.findFirstCompletelyVisibleItemPosition();
}
switch (layoutManagerType){
case GRID_LAYOUT_MANAGER:
mLayoutManager = new GridLayoutManager(getActivity(),SPAN_COUNT);
mCurrentLayoutManagerType = LayoutManagerType.GRID_LAYOUT_MANAGER;
break;
case LINEAR_LAYOUT_MANAGER:
mLayoutManager = new LinearLayoutManager(getActivity());
mCurrentLayoutManagerType = LayoutManagerType.LINEAR_LAYOUT_MANAGER;
break;
default:
mLayoutManager = new LinearLayoutManager(getActivity());
mCurrentLayoutManagerType = LayoutManagerType.LINEAR_LAYOUT_MANAGER;
}
mRecyclerView.setLayoutManager(mLayoutManager);
mRecyclerView.scrollToPosition(scrollPosition);
}
@Override
public void onSaveInstanceState(Bundle savedInstanceState){
//menyimpan layout manager terpilih saat ini
savedInstanceState.putSerializable(KEY_LAYOUT_MANAGER, mCurrentLayoutManagerType);
super.onSaveInstanceState(savedInstanceState);
}
private void initDataset() {
mDataSet = new String[nasabah.length];
mDataSet2 = new String[job.length];
mDataSet3 = new String[alamat.length];
mDataSet4 = new int[photo.length];
for (int i = 0; i < nasabah.length; i++){
mDataSet[i] = nasabah[i];
mDataSet2[i] = job[i];
mDataSet3[i] = alamat[i];
mDataSet4[i] = photo[i];
}
}
}
Надеюсь, кто-нибудь может мне помочь, пожалуйста
Спасибо