I am using ListView
to dynamically add checkboxes in Android. I am using a ContextAdapter
class to add inflate the ListView
.
My error log is as follows:
09-23 13:44:45.000: E/AndroidRuntime(1192): FATAL EXCEPTION: main
09-23 13:44:45.000: E/AndroidRuntime(1192): Process: com.project.attendancemanager, PID: 1192
09-23 13:44:45.000: E/AndroidRuntime(1192): android.content.res.Resources$NotFoundException: String resource ID #0x1
09-23 13:44:45.000: E/AndroidRuntime(1192): at android.content.res.Resources.getText(Resources.java:244)
09-23 13:44:45.000: E/AndroidRuntime(1192): at android.widget.TextView.setText(TextView.java:3888)
09-23 13:44:45.000: E/AndroidRuntime(1192): at com.project.attendancemanager.CustomAdapter.getView(CustomAdapter.java:31)
09-23 13:44:45.000: E/AndroidRuntime(1192): at android.widget.AbsListView.obtainView(AbsListView.java:2263)
09-23 13:44:45.000: E/AndroidRuntime(1192): at android.widget.ListView.measureHeightOfChildren(ListView.java:1263)
09-23 13:44:45.000: E/AndroidRuntime(1192): at android.widget.ListView.onMeasure(ListView.java:1175)
09-23 13:44:45.000: E/AndroidRuntime(1192): at android.view.View.measure(View.java:16497)
09-23 13:44:45.000: E/AndroidRuntime(1192): at android.widget.RelativeLayout.measureChild(RelativeLayout.java:689)
09-23 13:44:45.000: E/AndroidRuntime(1192): at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:473)
09-23 13:44:45.000: E/AndroidRuntime(1192): at android.view.View.measure(View.java:16497)
09-23 13:44:45.000: E/AndroidRuntime(1192): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
09-23 13:44:45.000: E/AndroidRuntime(1192): at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
09-23 13:44:45.000: E/AndroidRuntime(1192): at android.view.View.measure(View.java:16497)
09-23 13:44:45.000: E/AndroidRuntime(1192): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
09-23 13:44:45.000: E/AndroidRuntime(1192): at com.android.internal.widget.ActionBarOverlayLayout.onMeasure(ActionBarOverlayLayout.java:327)
09-23 13:44:45.000: E/AndroidRuntime(1192): at android.view.View.measure(View.java:16497)
09-23 13:44:45.000: E/AndroidRuntime(1192): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
09-23 13:44:45.000: E/AndroidRuntime(1192): at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
09-23 13:44:45.000: E/AndroidRuntime(1192): at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2291)
09-23 13:44:45.000: E/AndroidRuntime(1192): at android.view.View.measure(View.java:16497)
09-23 13:44:45.000: E/AndroidRuntime(1192): at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:1916)
09-23 13:44:45.000: E/AndroidRuntime(1192): at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1113)
09-23 13:44:45.000: E/AndroidRuntime(1192): at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1295)
09-23 13:44:45.000: E/AndroidRuntime(1192): at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1000)
09-23 13:44:45.000: E/AndroidRuntime(1192): at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5670)
09-23 13:44:45.000: E/AndroidRuntime(1192): at android.view.Choreographer$CallbackRecord.run(Choreographer.java:761)
09-23 13:44:45.000: E/AndroidRuntime(1192): at android.view.Choreographer.doCallbacks(Choreographer.java:574)
09-23 13:44:45.000: E/AndroidRuntime(1192): at android.view.Choreographer.doFrame(Choreographer.java:544)
09-23 13:44:45.000: E/AndroidRuntime(1192): at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:747)
09-23 13:44:45.000: E/AndroidRuntime(1192): at android.os.Handler.handleCallback(Handler.java:733)
09-23 13:44:45.000: E/AndroidRuntime(1192): at android.os.Handler.dispatchMessage(Handler.java:95)
09-23 13:44:45.000: E/AndroidRuntime(1192): at android.os.Looper.loop(Looper.java:136)
09-23 13:44:45.000: E/AndroidRuntime(1192): at android.app.ActivityThread.main(ActivityThread.java:5017)
09-23 13:44:45.000: E/AndroidRuntime(1192): at java.lang.reflect.Method.invokeNative(Native Method)
09-23 13:44:45.000: E/AndroidRuntime(1192): at java.lang.reflect.Method.invoke(Method.java:515)
09-23 13:44:45.000: E/AndroidRuntime(1192): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
09-23 13:44:45.000: E/AndroidRuntime(1192): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
09-23 13:44:45.000: E/AndroidRuntime(1192): at dalvik.system.NativeStart.main(Native Method)
ContextAdapter
class is as follows:
import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.CheckBox;
import android.widget.TextView;
public class CustomAdapter extends ArrayAdapter<ListData>{
ListData[] items = null;
Context context;
public CustomAdapter(Context context, ListData[] resource) {
super(context, R.layout.activity_list_row, resource);
// TODO Auto-generated constructor stub
this.context = context;
this.items = resource;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
LayoutInflater inflator = ((Activity) context).getLayoutInflater();
convertView = inflator.inflate(R.layout.activity_list_row, parent, false);
TextView name = (TextView) convertView.findViewById(R.id.NameTV);
TextView rollNo = (TextView) convertView.findViewById(R.id.RollNoTV);
CheckBox cb = (CheckBox) convertView.findViewById(R.id.studentCB);
name.setText(items[position].getName());
rollNo.setText(items[position].getRollNo());
if(items[position].getValue() == 1)
cb.setChecked(true);
else
cb.setChecked(false);
return convertView;
}
}
My ListData
class:
package com.project.attendancemanager;
public class ListData {
String name;
int value, rollno;
ListData(String name, int rollno, int value){
this.name = name;
this.rollno = rollno;
this.value = value;
}
public String getName(){
return this.name;
}
public int getRollNo(){
return this.rollno;
}
public int getValue(){
return this.value;
}
}
Inside the onCreate
function in the Activity I am doing this:
Listview lv;
ListData[] items;
CustomAdapter adapter = new CustomAdapter(this, items);
lv.setAdapter(adapter);
It would be of great help is someone could point out the mistake in my code. Thanks! Cheers!
Instead of
rollNo.setText(items[position].getRollNo());
you should use
rollNo.setText(Integer.toString(items[position].getRollNo()));
If you try to set integer as text, you call method setText(int resID) and application try to set as text some string resource with this resID.
SetText doesnt allow integer values
Just Add ""+
for your code to look like this ""+rollNo.setText(items[position].getRollNo());
this error occurs because setText does not take Integers, it takes Strings
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With