Я пытаюсь реализовать переработчик View в android, но после добавления его в приложение он ничего не показывает. Я искал в Интернете, но не могу понять, что я делаю не так. Также, когда я объявляю dashpercent и dashsubject (упомянутые в адаптере) в классе RecyclerViewHolder расширяет RecyclerView.ViewHolder при записи:
@Override
public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
dashboard_recycler_components data = dashlist.get(position);
String sub = data.Subject;
holder.dashpercent.setText(data.percentage);
holder.dashsubject.setText(data.Subject);
}
, тогда dashpercent и dashsubject выдают ошибку, что они не объявлены.
Адаптер
package com.example.attendance;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ProgressBar;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import java.util.ArrayList;
public class RecyclerAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
private ArrayList<dashboard_recycler_components> dashlist;
RecyclerAdapter(ArrayList<dashboard_recycler_components> a) {
this.dashlist = a;
}
@NonNull
@Override
public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
LayoutInflater inflater = LayoutInflater.from((parent.getContext()));
View view = inflater.inflate(R.layout.attendace_card, parent, false);
return new RecyclerViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
dashboard_recycler_components data = dashlist.get(position);
String sub = data.Subject;
TextView dashpercent = (TextView) holder.itemView.findViewById(R.id.dashPercent);
TextView dashsubject = (TextView) holder.itemView.findViewById(R.id.dashSubject);
ProgressBar dashProgress = (ProgressBar) holder.itemView.findViewById(R.id.recycler_progress);
dashpercent.setText(data.percentage);
dashsubject.setText(data.Subject);
}
@Override
public int getItemCount() {
return dashlist.size();
}
public class RecyclerViewHolder extends RecyclerView.ViewHolder {
RecyclerViewHolder(@NonNull View itemView) {
super(itemView);
}
}
}
Фрагмент dashboad:
package com.example.attendance;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.ListAdapter;
import androidx.recyclerview.widget.RecyclerView;
import java.util.ArrayList;
public class dashboardfragment extends Fragment {
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
ArrayList<dashboard_recycler_components> subList = new ArrayList<>();
dashboard_recycler_components a = new dashboard_recycler_components("DSA",33);
dashboard_recycler_components b = new dashboard_recycler_components("DSA",33);
dashboard_recycler_components c = new dashboard_recycler_components("DSA",33);
dashboard_recycler_components d = new dashboard_recycler_components("DSA",33);
dashboard_recycler_components e = new dashboard_recycler_components("DSA",33);
dashboard_recycler_components f = new dashboard_recycler_components("DSA",33);
dashboard_recycler_components g = new dashboard_recycler_components("DSA",33);
dashboard_recycler_components h = new dashboard_recycler_components("DSA",33);
dashboard_recycler_components i = new dashboard_recycler_components("DSA",33);
dashboard_recycler_components j = new dashboard_recycler_components("DSA",33);
subList.add(a);
subList.add(b);
subList.add(c);
subList.add(d);
subList.add(e);
subList.add(f);
subList.add(g);
subList.add(h);
subList.add(i);
subList.add(j);
View v = inflater.inflate(R.layout.fragment_dashboard,container,false);
RecyclerView recycle = (RecyclerView)v.findViewById(R.id.recycler_view1);
recycle.setLayoutManager(new LinearLayoutManager(getActivity()));
recycle.setAdapter(new RecyclerAdapter(subList));
return v;
}
}
dashbord_recycler_componets:
package com.example.attendance;
public class dashboard_recycler_components {
int percentage;
String Subject;
public dashboard_recycler_components(String a , int b){
this.percentage = b;
this.Subject = a;
}
public int getPercentage() {
return percentage;
}
public String getSubject() {
return Subject;
}
public void setPercentage(int percentage) {
this.percentage = percentage;
}
public void setSubject(String subject) {
this.Subject = subject;
}
}
MainActivity:
package com.example.attendance;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import android.os.Bundle;
import android.view.MenuItem;
import android.widget.ProgressBar;
import com.google.android.material.bottomnavigation.BottomNavigationView;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
BottomNavigationView bottomNav = findViewById(R.id.bottom_nav_bar);
bottomNav.setOnNavigationItemSelectedListener(navListener);
getSupportFragmentManager().beginTransaction().replace(R.id.frame_container,
new homefragment()).commit();
}
private BottomNavigationView.OnNavigationItemSelectedListener navListener = new
BottomNavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem menuItem) {
Fragment frag = null;
switch (menuItem.getItemId()) {
case R.id.dashboard:
frag = new dashboardfragment();
break;
case R.id.profile:
frag = new profilefragment();
break;
case R.id.home:
frag = new homefragment();
break;
}
getSupportFragmentManager().beginTransaction().replace(R.id.frame_container,
frag).commit();
return true;
}
};
}