问题
This is my adapter class:-
public class adapter_cgpa extends RecyclerView.Adapter<adapter_cgpa.Viewholder> {
ArrayList<POJO> cgpaArrayList;
public adapter_cgpa(ArrayList<POJO> cgpaArrayList) {
this.cgpaArrayList = cgpaArrayList;
}
@NonNull
@Override
public adapter_cgpa.Viewholder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View listitem = LayoutInflater.from(parent.getContext()).inflate(R.layout.recycler_cgpa, parent, false);
return new Viewholder(listitem);
}
@Override
public void onBindViewHolder(@NonNull adapter_cgpa.Viewholder holder, int position) {
POJO cgpa= cgpaArrayList.get(position);
holder.cname.setText(cgpa.getCname());
holder.no_of_sems.setText(cgpa.getNo_of_sems());
holder.cgpa.setText(cgpa.getCgpa());
holder.percentage.setText(cgpa.getPercentage());
holder.schemec.setText(cgpa.getSchemec());
}
@Override
public int getItemCount() {
return cgpaArrayList.size();
}
public class Viewholder extends RecyclerView.ViewHolder {
TextView cname, no_of_sems, cgpa, percentage,schemec;
ImageButton btndelete2;
public Viewholder(@NonNull View itemView) {
super(itemView);
cname=(TextView)itemView.findViewById(R.id.name);
no_of_sems=(TextView)itemView.findViewById(R.id.no_of_sem);
cgpa=(TextView)itemView.findViewById(R.id.textView49);
percentage=(TextView)itemView.findViewById(R.id.textView55);
schemec=(TextView)itemView.findViewById(R.id.scheme2);
btndelete2=(ImageButton)itemView.findViewById(R.id.btndelete2);
btndelete2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
dbmanager db= new dbmanager(v.getContext());
db.delete2();
Toast.makeText(v.getContext(), "Data deleted successfully", Toast.LENGTH_SHORT).show();
}
});
}
}
}
This is my fragment which is like my main class:-
public class cgpa_frag extends Fragment {
RecyclerView recyclerview;
adapter_cgpa ac;
TextView else1;
ArrayList<POJO> cgpaArrayList = new ArrayList<>();
public cgpa_frag() {
// Required empty public constructor
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_cgpa_frag, container, false);
recyclerview=(RecyclerView) view.findViewById(R.id.rc2);
else1=(TextView) view.findViewById(R.id.else1);
cgpaArrayList= new ArrayList<>();
ac= new adapter_cgpa(cgpaArrayList);
recyclerview.setLayoutManager(new LinearLayoutManager(getContext(), RecyclerView.VERTICAL,true));
recyclerview.setAdapter(ac);
Fetchdata2();
return view;
}
private void Fetchdata2()
{
dbmanager db= new dbmanager(getContext());
Cursor cursor= db.fetch_data2();
if (cursor!= null){
cursor.moveToFirst();
while (cursor.moveToNext()){
//ModelDetail cd= new ModelDetail(cursor.getString(1),cursor.getString(2),cursor.getString(3),cursor.getString(4));
POJO pj= new POJO();
pj.setCname(cursor.getString(0));
pj.setNo_of_sems(cursor.getInt(1));
pj.setCgpa(cursor.getInt(2));
pj.setPercentage(cursor.getInt(3));
pj.setSchemec(cursor.getInt(4));
cgpaArrayList.add(pj);
}
ac= new adapter_cgpa(cgpaArrayList);
recyclerview.setAdapter(ac);
}
else{
else1.setVisibility(View.VISIBLE);
}
}
}
Below is my recycler_cgpa.xml file:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<androidx.cardview.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20sp"
android:textStyle="bold"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.2"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.3" />
<TextView
android:id="@+id/no_of_sem"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.15"
app:layout_constraintStart_toEndOf="@+id/name"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.3" />
<TextView
android:id="@+id/sgpae"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="CGPA"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.2"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.7" />
<TextView
android:id="@+id/perce"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Percentage"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.6"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.70" />
<TextView
android:id="@+id/textView49"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="@+id/sgpae"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.08"
app:layout_constraintStart_toEndOf="@+id/sgpae"
app:layout_constraintTop_toTopOf="@+id/sgpae"
app:layout_constraintVertical_bias="0.0" />
<TextView
android:id="@+id/textView55"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="@+id/perce"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.08"
app:layout_constraintStart_toEndOf="@+id/perce"
app:layout_constraintTop_toTopOf="@+id/perce" />
<TextView
android:id="@+id/scheme2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="10sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.95"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.95" />
<ImageButton
android:id="@+id/btndelete2"
android:layout_width="30dp"
android:layout_height="40dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.96"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.05"
app:srcCompat="@android:drawable/ic_menu_delete" />
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.cardview.widget.CardView>
</LinearLayout>
This is my POJO class:
public class POJO {
String sname, cname,semester ;
int sgpa ,percent, cgpa, percentage, schemes, schemec, no_of_sems ;
public POJO() {
this.sname = sname;
this.cname = cname;
this.semester = semester;
this.no_of_sems = no_of_sems;
this.sgpa = sgpa;
this.percent = percent;
this.cgpa = cgpa;
this.percentage = percentage;
this.schemes = schemes;
this.schemec = schemec;
}
public String getSname() {
return sname;
}
public String getCname() {
return cname;
}
public void setSname(String sname) {
this.sname = sname;
}
public void setCname(String cname) {
this.cname = cname;
}
public void setSemester(String semester) {
this.semester = semester;
}
public void setNo_of_sems(int no_of_sems) {
this.no_of_sems = no_of_sems;
}
public void setSgpa(int sgpa) {
this.sgpa = sgpa;
}
public void setPercent(int percent) {
this.percent = percent;
}
public void setCgpa(int cgpa) {
this.cgpa = cgpa;
}
public void setPercentage(int percentage) {
this.percentage = percentage;
}
public void setSchemes(int schemes) {
this.schemes = schemes;
}
public void setSchemec(int schemec) {
this.schemec = schemec;
}
public String getSemester() {
return semester;
}
public int getNo_of_sems() {
return no_of_sems;
}
public int getSgpa() {
return sgpa;
}
public int getPercent() {
return percent;
}
public int getCgpa() {
return cgpa;
}
public int getPercentage() {
return percentage;
}
public int getSchemes() {
return schemes;
}
public int getSchemec() {
return schemec;
}
}
This is my dbmanager class:
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import androidx.annotation.Nullable;
public class dbmanager extends SQLiteOpenHelper {
String sgpa_table="create table Sgpa (sname text, semester text, sgpa int,percent int, schemes int, primary key(sname,semester,schemes))";
String cgpa_table="create table Cgpa (cname text, no_of_sems int, cgpa int, percentage int, schemec int, primary key(cname,no_of_sems,schemec))";
public dbmanager(@Nullable Context context) {
super(context, "Student", null, 1);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(sgpa_table);
db.execSQL(cgpa_table);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
public void delete1(){
SQLiteDatabase db = this.getWritableDatabase();
String query = "delete FROM Sgpa " ;
onCreate(db);
db.execSQL(query);
// Cursor cursor = db.rawQuery(query,null);
db.close();
}
public void delete2(){
SQLiteDatabase db = this.getWritableDatabase();
String query = "delete FROM Cgpa " ;
onCreate(db);
db.execSQL(query);
// Cursor cursor = db.rawQuery(query,null);
db.close();
}
public Cursor fetch_data1() {
SQLiteDatabase db = this.getWritableDatabase();
String query = "Select*FROM Sgpa" ;
Cursor cursor = db.rawQuery(query,null);
// if (cursor != null) {
// cursor.moveToFirst();
// }
return cursor;
}
public Cursor fetch_data2() {
SQLiteDatabase db = this.getWritableDatabase();
String query = "Select*FROM Cgpa" ;
Cursor cursor = db.rawQuery(query,null);
// if (cursor != null) {
// cursor.moveToFirst();
// }
return cursor;
}
}
I cannot identify the error. The logcat points error in the adapter class in these two lines:
public class adapter_cgpa extends RecyclerView.Adapter<adapter_cgpa.Viewholder> {
and holder.no_of_sems.setText(cgpa.getNo_of_sems());
and writes android.content.res.Resources$NotFoundException: String resource ID #0x0
回答1:
You're accidentally calling the version of TextView.setText that takes an int resource ID.
You should explicitly convert it to a String
:
holder.no_of_sems.setText(Integer.toString(cgpa.getNo_of_sems()));
来源:https://stackoverflow.com/questions/60407016/error-in-displaying-data-with-textview-settext-resourcesnotfoundexception-str