Reputation: 765
Hello i am trying to implement a listview which will display the contents of a class by TextView. However the adapter doesn't seem to take in the values of the objects as it throws a nullpointer exception.
this is my custom class
public class SubjectClass {
String sName;
String sCode;
String teacher;
}
Given Belows is the code for my CUstomarrayadapter
public CustomAdap(Activity context, ArrayList<SubjectClass> names) {
super(context, R.layout.listtempl, names);
this.context = context;
this.names = names;
}
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = context.getLayoutInflater();
View rowView = inflater.inflate(R.layout.listtempl, null, true);
TextView textView1 = (TextView) rowView.findViewById(R.id.label1);
TextView textView2 = (TextView) rowView.findViewById(R.id.label2);
TextView textView3 = (TextView) rowView.findViewById(R.id.label3);
SubjectClass sClass= (SubjectClass)getItem(position);
String s1 = sClass.sName;
String s2 = sClass.sCode;
String s3 = sClass.teacher;
Toast.makeText(getContext(), sClass.sCode,Toast.LENGTH_SHORT).show();
textView1.setText(s1);
textView2.setText(s2);
textView3.setText(s3);
return rowView;
}
this is the xml file used by the adapter
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<TextView android:text="@+id/label1" android:textColor="#F1235AFA" android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/label" android:layout_alignParentTop="true" android:textAppearance="?android:attr/textAppearanceMedium" android:layout_alignParentLeft="true"></TextView>
<TextView android:text="TextView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/textView2" android:textAppearance="?android:attr/textAppearanceSmall" android:layout_alignBaseline="@+id/textView1" android:layout_alignBottom="@+id/label2" android:layout_alignParentLeft="true"></TextView>
<TextView android:text="TextView" android:textColor="#2CD7E0" android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/textView1" android:textAppearance="?android:attr/textAppearanceSmall" android:layout_below="@+id/label" android:layout_toRightOf="@+id/label3" android:layout_marginLeft="15dp"></TextView>
</RelativeLayout>
This is my activity
public class SampleActivity extends Activity
{
ListView listview;
CustomAdap adapter;
ArrayList<SubjectClass> values = new ArrayList<SubjectClass>();
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.list1);
SubjectClass subjectClass = new SubjectClass();
subjectClass.sCode="cs9410";
subjectClass.sName="Principles of management";
subjectClass.teacher="xyz";
values.add(subjectClass);
SubjectClass subjectClass1 = new SubjectClass();
subjectClass1.sCode="cs9401";
subjectClass1.sName="Ethics";
subjectClass1.teacher="xxx";
values.add(subjectClass1);
listview =(ListView) findViewById(R.id.lv1);
adapter = new CustomAdap(this, values);
listview.setAdapter(adapter);
}
}//
Upvotes: 0
Views: 169
Reputation: 40416
In row layout file change android:text="@+id/label1"
to android:id="@+id/label1"
.
For all 3 TextView
:
SubjectClass sClass = (SubjectClass) names.get(position);
Upvotes: 1
Reputation: 33741
I believe you need to change your code to
SubjectClass sClass= names.get(position);
Upvotes: 0