我尝试构建一个没有片段的活动,但它不起作用,也没有显示任何错误。在Logcat中,只实例化了SemestreAdapter构造。没有例外。
我的代码:
SpriteBatch
上课:
Activity
适配器:
package passei.whereit.com.passei;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.View;
import android.widget.Toast;
import java.util.ArrayList;
import passei.whereit.com.passei.Adapters.SemestreAdapter;
import passei.whereit.com.passei.Classes.Semestre;
public class Semestres_Activity extends AppCompatActivity {
private RecyclerView semestreRV;
private SemestreAdapter semestreRA;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_semestres);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
toolbar.setTitle("Semestres");
toolbar.setTitleTextColor(getResources().getColor(R.color.colorIcons));
setSupportActionBar(toolbar);
semestreRV = (RecyclerView) findViewById(R.id.my_recycler_view);
semestreRV.setHasFixedSize(true);
semestreRA = new SemestreAdapter(this,setSemestreList());
semestreRV.setAdapter(semestreRA);
LinearLayoutManager llm = new LinearLayoutManager(this);
llm.setOrientation(LinearLayoutManager.VERTICAL);
semestreRV.setLayoutManager(llm);
}
public ArrayList<Semestre> setSemestreList(){
ArrayList<Semestre> list = new ArrayList<>();
int[] s = new int[]{1,2,3,4,5,6};
int[] m = new int[]{3,4,2,3,7,5};
float[] n = new float[]{9.7f,3.4f,5.6f,7.6f,4.3f,6.7f};
int[] f = new int[]{3,4,2,3,7,5};
for(int i = 0; i < 6; i++){
Semestre semestre = new Semestre(s[i],m[i], n[i], f[i]);
list.add(semestre);
}
return list;
}
}
ViewHolder
package passei.whereit.com.passei.Adapters;
import android.content.Context;
import android.graphics.Color;
import android.nfc.Tag;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.amulyakhare.textdrawable.TextDrawable;
import java.lang.String;
import java.util.ArrayList;
import passei.whereit.com.passei.Classes.Semestre;
import passei.whereit.com.passei.R;
import passei.whereit.com.passei.ViewHolders.SemestreViewHolder;
/**
* Created by Leaandro Araujo on 04/03/2016.
*/
public class SemestreAdapter extends RecyclerView.Adapter<SemestreViewHolder> {
private Context context;
private ArrayList<Semestre> semestres;
private LayoutInflater inflater;
public SemestreAdapter(Context context, ArrayList<Semestre> list){
this.context = context;
this.semestres = list;
inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
Log.i("LOG", "SemestreAdapter()");
}
@Override
public SemestreViewHolder onCreateViewHolder(ViewGroup parent, int viewType){
Log.i("LOG", "onCreateViewHolder()");
View view = inflater.inflate(R.layout.rv_semestre,parent,false);
SemestreViewHolder viewHolder = new SemestreViewHolder(context,view);
return viewHolder;
}
@Override
public void onBindViewHolder(SemestreViewHolder viewHolder, int position){
Semestre semestre = semestres.get(position);
Log.i("LOG", "onBindViewHolder()");
TextDrawable drawable = TextDrawable.builder().buildRound(semestre.toString(), Color.LTGRAY);
viewHolder.ivSemestre.setImageDrawable(drawable);
viewHolder.tvMaterias.setText(semestre.getMaterias());
viewHolder.tvNotas.setText(String.valueOf(semestre.getNota()));
viewHolder.tvFaltas.setText(semestre.getFaltas());
}
public int getItemCount(){
return semestres.size();
}
}
XML布局
package passei.whereit.com.passei.ViewHolders;
import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import passei.whereit.com.passei.R;
/**
* Created by Leaandro Araujo on 12/03/2016.
*/
public class SemestreViewHolder extends RecyclerView.ViewHolder {
private Context context;
public ImageView ivSemestre;
public TextView tvMaterias, tvNotas, tvFaltas;
public SemestreViewHolder(Context context, View itemView){
super(itemView);
Log.i("LOG", "SemestreViewHolder()");
this.context = context;
ivSemestre = (ImageView) itemView.findViewById(R.id.semestre_number);
tvMaterias = (TextView) itemView.findViewById(R.id.semestre_materias);
tvNotas = (TextView) itemView.findViewById(R.id.semestre_notas);
tvFaltas = (TextView) itemView.findViewById(R.id.semestre_faltas);
}
}
RecyclerView项目XML布局
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:fitsSystemWindows="true"
android:background="@color/colorIcons"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<include layout="@layout/app_bar" android:id="@+id/toolbar" />
<android.support.v7.widget.RecyclerView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/my_recycler_view"
android:scrollbars="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
</ScrollView>
LogCat打印
答案 0 :(得分:0)
我解决了。我在设置文本时将项目转换为字符串..
public void onBindViewHolder(SemestreViewHolder viewHolder, int position){
Semestre semestre = semestres.get(position);
Log.i("LOG", "onBindViewHolder()");
TextDrawable drawable = TextDrawable.builder().buildRound(semestre.toString(), Color.LTGRAY);
viewHolder.ivSemestre.setImageDrawable(drawable);
viewHolder.tvMaterias.setText(String.valueOf(semestre.getMaterias()));
viewHolder.tvNotas.setText(String.valueOf(semestre.getNota()));
viewHolder.tvFaltas.setText(String.valueOf(semestre.getFaltas()));
}