Reputation: 17
I have a problem with a ListView and i constantly get an unexpected error :( when i am trying to run it on my SamsungGalaxy s4 mini i get an error and it crashes and after several seconds it turns off. what am i doing wrong? this is my code:
public class FirstActivity extends Activity {
private ListView listview01;
private TextView TextView01;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_first);
Student students_data[] = new Student[]
{
new Student("Nikos", "Apostolakis", "2012", "03112023"),
new Student("Stavros", "Birbilis", "2012", "03112116")
};
StudentAdapter adapter = new StudentAdapter(this, R.layout.studentitemlayout, students_data);
listview01 = (ListView) findViewById(R.id.ListView01);
TextView01 = (TextView) findViewById(R.id.HelloWorld);
TextView01.setText("BreakPointReached!");
listview01.setAdapter(adapter);
}
}
and my classes are
public class Student {
public String FirstName;
public String LastName;
public String JoinedYear;
public int JoinedRank;
public Student() {
super();
}
public Student(String FirstName , String LastName , String JoinedYear, String JoinedRank){
this.FirstName = FirstName;
this.LastName = LastName;
this.JoinedYear = JoinedYear;
this.JoinedRank = Integer.parseInt(JoinedRank);
}
}
and
public class StudentAdapter extends ArrayAdapter {
Context context;
int layoutResourceId;
Student data[] = null;
public StudentAdapter(Context context, int layoutResourceId, Student[] data) {
super(context, layoutResourceId, data);
this.layoutResourceId = layoutResourceId;
this.context = context;
this.data = data;
}
public View getView(int position, View convertView, ViewGroup parent) {
View row = convertView;
StudentHolder holder = null;
if (row == null) {
LayoutInflater inflater = ((Activity) context).getLayoutInflater();
row = inflater.inflate(layoutResourceId, parent, false);
holder = new StudentHolder();
holder.FirstName = (TextView) row.findViewById(R.id.FirstName);
holder.LastName = (TextView) row.findViewById(R.id.LastName);
holder.JoinedYear = (TextView) row.findViewById(R.id.JoinedYear);
holder.JoinedRank = (TextView) row.findViewById(R.id.JoinedRank);
row.setTag(holder);
} else {
holder = (StudentHolder) row.getTag();
}
Student student = data[position];
holder.FirstName.setText(student.FirstName);
holder.LastName.setText(student.LastName);
holder.JoinedYear.setText(student.JoinedYear);
holder.JoinedRank.setText(student.JoinedRank);
return row;
}
static class StudentHolder {
TextView FirstName;
TextView LastName;
TextView JoinedYear;
TextView JoinedRank;
}
}
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="10dp">
<TextView
android:id="@+id/FirstName"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textStyle="bold"
android:textSize="22dp"
android:textColor="#000000" />
<TextView
android:id="@+id/LastName"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/JoinedYear"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/JoinedRank"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<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="match_parent"
android:orientation="vertical"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context=".FirstActivity">
<TextView
android:id="@+id/HelloWorld"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="HelloWorld"/>
<ListView
android:id="@+id/ListView01"
android:layout_width="fill_parent"
android:layout_height="fill_parent"></ListView>
Upvotes: 0
Views: 80
Reputation: 23655
Student student = data[position];
holder.FirstName.setText(student.FirstName);
holder.LastName.setText(student.LastName);
holder.JoinedYear.setText(student.JoinedYear);
holder.JoinedRank.setText(student.JoinedRank);
I suppose your student.JoinedRank is an integer. Which invokes the setText(int resId)
method and that leads to the Exception, as there will not be a string constant in the R
class with value 2010 or the like.
Change the line to
holder.JoinedRank.setText("" + student.JoinedRank);
and it should work.
Upvotes: 0
Reputation: 1060
Make sure that all resources exists in their corresponding layout XMLs.
R.id.FirstName
R.id.LastName
R.id.JoinedYear
R.id.JoinedRank
R.id.HelloWorld
Edit:
You have set the text of a text view from Integer.
Replace
holder.JoinedRank.setText(student.JoinedRank);
with this
holder.JoinedRank.setText(student.JoinedRank+"");
Good Luck. :)
Upvotes: 1