I got runtime error exception:
java.lang.ClassCastException: android.widget.TwoLineListItem cannot be cast to android.widget.TextView
My Activity extends Activity NOT ListActivity and here is my layout construction:
<LinearLayout ...> <ListView ...></ListView> </LinearLayout>
Java:
ListView lv1 = (ListView) findViewById(R.id.listViewXMLdata);
ArrayAdapter<String> arrAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_2,
android.R.id.text2, getResources().getStringArray(R.array.countries));
lv1.setAdapter(arrAdapter);
lv1.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
lView.getItemAtPosition(position);
String itemSelected = ((TextView) view).getText().toString();
Toast.makeText(getApplicationContext(), "Clicked Position Number " + position + ": " + itemSelected ,
Toast.LENGTH_SHORT)
.show();
}
});
My only main concern is just to get the string (item selected) on my list.
NOTE 1: I am not working on any database.
NOTE 2: I already tried casting it to CharSequence itemSelected = ((TextView) view).getText().toString() but still got runtime error.
The runtime error occurs only when I started to select item on the list.