Reputation: 1754
I have developed an ListView
into a dialog pop up, everything is working fine - listener for Spinner
and CheckBox
. I can access which element has changed value:
for spinner:
@Override
public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
Log.d("MyLog", "SPINNER at position: " + pos + " changed value to: " + parent.getItemAtPosition(pos));
}
and for check box:
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
Log.d("MyLog", "changed value - is checked: " + isChecked);
}
But how can I accews title - TextView
of each ListView
element, for example to set error if spinner changed element or checkBox changed state?
My whole code: xml base layout
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/dialog_base"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<RelativeLayout
android:id="@+id/title_dialog_zone"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#30883D">
<ImageView
android:id="@+id/dialog_imgView"
android:layout_width="32dp"
android:layout_height="32dp"
android:layout_margin="@dimen/material_margin_medium"
android:src="@android:drawable/ic_dialog_alert" />
<TextView
android:id="@+id/dialog_base_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/dialog_imgView"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_alignRight="@+id/dialog_imgView"
android:layout_toEndOf="@+id/dialog_imgView"
android:layout_toRightOf="@+id/dialog_imgView"
android:clickable="true"
android:focusable="true"
android:focusableInTouchMode="true"
android:singleLine="true"
android:text="@string/txt_sys_dialog_caution"
android:textAppearance="?android:attr/textAppearanceMedium" />
</RelativeLayout>
<ListView
android:id="@+id/dialog_listView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/view_delimiter"
android:layout_gravity="center_horizontal"
android:paddingBottom="@dimen/material_margin_medium" />
</RelativeLayout>
custom element - can be TextView+TextView / TextView+CheckBox / TextView+Spinner:
<?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="match_parent"
android:orientation="vertical">
<TextView
android:id="@+id/dialog_element_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="@dimen/material_margin"
android:layout_marginStart="@dimen/material_margin"
android:clickable="true"
android:focusable="true"
android:focusableInTouchMode="true"
android:text="Ttitle Text"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="@+id/dialog_txtView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/dialog_element_title"
android:layout_marginBottom="@dimen/material_margin_medium"
android:layout_marginEnd="@dimen/material_margin"
android:layout_marginLeft="@dimen/material_margin_large"
android:layout_marginRight="@dimen/material_margin"
android:layout_marginStart="@dimen/material_margin_large"
android:hint="Here it is" />
<CheckBox
android:id="@+id/dialog_checkBox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/dialog_element_title"
android:layout_marginBottom="@dimen/material_margin_medium"
android:layout_marginLeft="@dimen/material_margin_large"
android:layout_marginStart="@dimen/material_margin_large"
android:checked="false"
android:text="Do your choise" />
<Spinner
android:id="@+id/dialog_spinner"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/dialog_element_title"
android:layout_gravity="center_horizontal"
android:layout_marginBottom="@dimen/material_margin_medium"
android:layout_marginEnd="@dimen/material_margin"
android:layout_marginLeft="@dimen/material_margin_large"
android:layout_marginRight="@dimen/material_margin"
android:layout_marginStart="@dimen/material_margin_large"
android:background="#8E8276"
android:popupBackground="#8E8276"
android:spinnerMode="dropdown" />
</RelativeLayout>
My dialog creation code is very long, but idea is to set up BaseAdapter
using a JSON obj I choose what kind of view do I need to add in ListView
. And I have adapters for CheckBox
and Spinner
.
Upvotes: 1
Views: 112
Reputation: 7515
You can use something global to acces your child view, then you can handle that ListView
row , but be carefully to not acces an invisible element - it will throw NullPointerException
. One method can be to get your listView rows count after adapter is setted up:
int cout = listView.getChildCount();
Then you can use a mthod like:
public View getRowListViewByPosition(int itemPosition, ListView listView) {
final int firstListItemPosition = listView.getFirstVisiblePosition();
final int lastListItemPosition = firstListItemPosition + listView.getChildCount() - 1;
if (itemPosition < firstListItemPosition || itemPosition > lastListItemPosition ) {
return listView.getAdapter().getView(itemPosition, null, listView);
} else {
final int childIndex = itemPosition - firstListItemPosition;
return listView.getChildAt(childIndex);
}
}
So, now you have the whole row, but you need only one element of your row, right? :)
View wantedViewRow = listView.getChildAt(1);
TextView txt = (TextView) wantedViewRow .findViewById(R.id.yourTitleTextView);
txt.setError("Sweet error!");
You're done! Tested solution...
Upvotes: 1
Reputation: 85
@Override
public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
View parentRow = (View) view.getParent();
TextView title= (TextView) parentRow
.findViewById(R.id.title);
title.settext("android");
}
try this..
Upvotes: 0
Reputation: 197
On checking of checkbox :
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
Log.d("MyLog", "changed value - is checked: " + isChecked);
if(isChecked)
titlecheck.setText("Checked");
else
titlecheck.setText("Unchecked");
}
for Spinner do the same
Upvotes: 0