嗨,我正在尝试通过解析mysql数据库中的json在android的recyclerview中显示数据。但是它一直只显示一些数据,我似乎找不到问题。任何帮助将不胜感激。
这是适配器:
public class Adapter extends RecyclerView.Adapter<Adapter.MyViewHolder> {
List<Course> data;
private Context context;
public Adapter(Context context,List<Course> data){
this.data=data;
this.context=context;
}
@NonNull
@Override
public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view=LayoutInflater.from(parent.getContext()).inflate(R.layout.course_list,parent,false);
return new MyViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull MyViewHolder holder, int position) {
DecimalFormat form = new DecimalFormat("0.00");
Course current=data.get(position);
holder.tCoursecode.setText(current.getmCoursecode());
holder.tCoursetitle.setText(current.getmCoursetitle());
holder.tCredit.setText(String.valueOf(current.getmCredit()));
holder.tMarks.setText(form.format (current.getmMarks()));
holder.tGrade.setText(current.getmGrade());
}
@Override
public int getItemCount() {
return data.size();
}
class MyViewHolder extends RecyclerView.ViewHolder {
TextView tCoursecode;
TextView tCoursetitle;
TextView tCredit;
TextView tMarks;
TextView tGrade;
public MyViewHolder(View itemView) {
super(itemView);
tCoursecode=itemView.findViewById(R.id.tcc);
tCoursetitle=itemView.findViewById(R.id.tct);
tCredit=itemView.findViewById(R.id.tc);
tMarks=itemView.findViewById(R.id.tm);
tGrade=itemView.findViewById(R.id.tg);
}
}
此布局:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:orientation="horizontal"
android:layout_height="wrap_content"
android:baselineAligned="false"
android:padding="16dp">
<LinearLayout
android:layout_weight="2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:orientation="vertical"
android:id="@+id/right">
<TextView
android:id="@+id/tcc"
android:layout_width="match_parent"
android:textSize="18sp"
android:layout_height="wrap_content"
tools:text="CSC 460" />
<TextView
android:id="@+id/tct"
android:layout_width="match_parent"
android:textSize="18sp"
android:layout_height="wrap_content"
tools:text= "Information Security"/>
</LinearLayout>
<LinearLayout
android:layout_weight="3"
android:layout_width="0dp"
android:layout_height="match_parent"
android:orientation="horizontal">
<TextView
android:id="@+id/tc"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAlignment="center"
android:padding="25dp"
android:textSize="18sp"
tools:text="1"/>
<TextView
android:id="@+id/tm"
android:layout_width="wrap_content"
android:padding="25dp"
android:textSize="18sp"
android:layout_height="wrap_content"
tools:text="78.4"/>
<TextView
android:id="@+id/tg"
android:layout_width="wrap_content"
android:padding="25dp"
android:textSize="18sp"
android:layout_height="wrap_content"
tools:text="A"/>
</LinearLayout>
屏幕截图:等级“ A”未显示
答案 0 :(得分:0)
尝试这种方式
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:baselineAligned="false"
android:orientation="horizontal"
android:padding="16dp"
>
<LinearLayout
android:id="@+id/right"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="2"
android:orientation="vertical"
>
<TextView
android:id="@+id/tcc"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="18sp"
tools:text="CSC 460"
/>
<TextView
android:id="@+id/tct"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="18sp"
tools:text="Information Security"
/>
</LinearLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="3"
android:gravity="center"
android:orientation="horizontal"
>
<TextView
android:id="@+id/tc"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:textAlignment="center"
android:textSize="18sp"
tools:text="1"
/>
<TextView
android:id="@+id/tm"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:textSize="18sp"
tools:text="78.4"
/>
<TextView
android:id="@+id/tg"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:textSize="18sp"
tools:text="A"
/>
</LinearLayout>
</LinearLayout>
希望这会有所帮助