I have an error in which when a checkbox/row in the listview is selected, it points out to error android.widget.RelativeLayout cannot be cast to android.widget.TextView.
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
String selectedBet = ((TextView)view).getText().toString();
if (selectedBets.contains(selectedBet)){
selectedBets.remove(selectedBet); //uncheck
}else{
selectedBets.add(selectedBet);
}
}
});
Also, here's my XML Relativelayout.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="75dp">
<CheckedTextView
android:layout_width="wrap_content"
android:layout_height="50dp"
android:gravity="center_vertical"
android:checkMark="?android:attr/listChoiceIndicatorMultiple"
android:padding="5dp"
android:id="@+id/checkmark" />
</RelativeLayout>
I can't seem to it working, I have no idea how this works.
The answer given by Nilesh Rathod here will work for you, but it's a short term solution to your problem.
Whenever you add an OnItemClickListener
to a ListView
, the function will always give you the root view/parent view. If you add more stuff, like a EditText
inside the RelativeLayout
, you'll have to find it as follows:
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
TextView textView = view.findViewById(R.id.textViewId);
EditText editText = view.findViewById(R.id.editTextId);
String selectedBet = textView.getText().toString();
}
});
You'll have to find a particular View
from the container layout that you'll get from onItemClick
function, and then operate on it.