Note that there are some explanatory texts on larger screens.

plurals
  1. POListView setOnItemClickListener Throw Exception
    primarykey
    data
    text
    <p>I am trying to access the setOnItemClick listener, and it throwing an exception. I add a try catch in there, and there login in the logcat exception. Is there something I am doing that is causing it to throw the exception?</p> <p>Here is the custom Adapter:</p> <pre><code>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&lt;Student&gt; { 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; } } </code></pre> <p>Here is the listener method:</p> <pre><code>listView1.setOnItemClickListener(new OnItemClickListener() { @Override public void onItemClick(AdapterView&lt;?&gt; 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()); } } }); </code></pre> <p>Here is the XML file:</p> <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;&lt;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" &gt; &lt;ScrollView android:layout_width="fill_parent" android:layout_height="wrap_content" &gt; &lt;LinearLayout android:id="@+id/linearylayout1" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" &gt; &lt;ListView android:id="@+id/lvStudent" android:layout_width="match_parent" android:layout_height="144dp" android:descendantFocusability="beforeDescendants" &gt; &lt;/ListView&gt; &lt;/LinearLayout&gt; &lt;/ScrollView&gt; &lt;/LinearLayout&gt; </code></pre> <p>This is how the row looks.</p> <pre><code>?xml version="1.0" encoding="utf-8"?&gt; &lt;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" &gt; &lt;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#" /&gt; &lt;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" /&gt; &lt;/RelativeLayout&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload