我正在尝试在android中实现recycler View,但是将其添加到应用程序后它没有显示任何内容。我已经在网上搜索过,但无法弄清楚自己在做什么错。
另外,当我在类RecyclerViewHolder中声明dashpercent和dashsubject(在适配器中提到)时,将在编写时扩展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);
}
}
}
仪表板片段:
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;
}
}
主要活动:
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;
}
};
}
最佳答案
如果要分配,您在percentage
中的dashboard_recycler_components
是int
您的percentage
是int
,如果要将其分配给dashpercent
,则必须首先将此int
设置为String
,例如:dashpercent.setText(String.valueOf(data.percentage));
PS:
您应该将类对象dashboard_recycler_components
重命名为dashboardRecyclerComponents
并使用camelCase
而不是snake_case