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.TextView;
public class StudentAdapter extends ArrayAdapter<Student> {
Context context;
int layoutResourseId;
Student data[] = null;
public StudentAdapter(Context context, int layoutResourceId, Student[] data) {
super(context, layoutResourceId, data);
this.context = context;
this.layoutResourseId = layoutResourceId;
this.data = data;
// TODO Auto-generated constructor stub
}
@Override
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(layoutResourseId, parent, false);
holder = new StudentHolder();
//Associate the Items in the holder with a view in the XML file.
holder.tvStNumber = (TextView)row.findViewById(R.id.tvStNumber);
row.setTag(holder);
} else
{
holder = (StudentHolder) row.getTag();
}
//Setting the text for the items
Student item =data[position];
holder.tvStNumber.setText(item.GetStudentNumber());
return row;
}
//Student Holder
static class StudentHolder
{
TextView tvStNumber;
TextView tvStDescription;
}
}
listView1.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position,long id) {
try{
String listValue = ((TextView)view).getText().toString(); //This is where the exception is happening. The below text should have been commented out.
// String text = (String) listView1.getItemAtPosition(position); // this is where the excexption is happening.
Toast.makeText(getApplicationContext(), "You click" + position, Toast.LENGTH_LONG).show();
String d = "Test toast.";
}catch(Exception e)
{
Log.e("Error Message", e.getMessage());
}
}
});
<?xml version="1.0" encoding="utf-8"?><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"
tools:ignore="HardcodedText"
>
<ScrollView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
>
<LinearLayout
android:id="@+id/linearylayout1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
>
<ListView
android:id="@+id/lvStudent"
android:layout_width="match_parent"
android:layout_height="144dp"
android:descendantFocusability="beforeDescendants"
>
</ListView>
</LinearLayout>
</ScrollView>
</LinearLayout>
?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:focusable="false"
>
<TextView
android:id="@+id/tvStNumber"
android:layout_width="100sp"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="10sp"
android:layout_marginTop="5sp"
android:text="EE#" />
<TextView
android:id="@+id/tvStDescription"
android:layout_width="100sp"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_marginLeft="10sp"
android:layout_toRightOf="@+id/tvStNumber"
android:text="Item Description" />
</RelativeLayout>
String listValue = (((TextView) ((RelativeLayout) v).getChildAt(0)).getText().toString());
String listValue = ((TextView)view).getText().toString();
listView1.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position,long id) {
try{
String listValue = parent.getItemAtPosition(position).GetStudentNumber(); //This is where the exception is happening. The below text should have been commented out.
// String text = (String) listView1.getItemAtPosition(position); // this is where the excexption is happening.
Toast.makeText(getApplicationContext(), "You click" + position, Toast.LENGTH_LONG).show();
String d = "Test toast.";
}catch(Exception e)
{
Log.e("Error Message", e.getMessage());
}
}
});