访问 ListView 中的元素
Access elements in ListView
我已经开发了一个 ListView
到弹出对话框中,一切正常 - Spinner
和 CheckBox
的监听器。我可以访问哪个元素已更改值:
微调器:
@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));
}
对于复选框:
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
Log.d("MyLog", "changed value - is checked: " + isChecked);
}
但是如何 我可以访问每个 ListView
元素的标题 - TextView
,例如设置如果微调器更改元素或复选框更改状态会出错?
视图如下所示:
我的全部代码:
xml 基本布局
<?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>
自定义元素 - 可以是 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>
我的对话框创建代码很长,但我的想法是使用 JSON obj 设置 BaseAdapter
我在 ListView
中选择我需要添加哪种视图。我有 CheckBox
和 Spinner
.
的适配器
选中复选框时:
@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");
}
对 Spinner 做同样的事情
@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");
}
试试这个..
您可以使用一些全局的东西来访问您的子视图,然后您可以处理 ListView
行,但要小心不要访问不可见的元素 - 它会抛出 NullPointerException
。一种方法是在设置适配器后获取 listView 行数:
int cout = listView.getChildCount();
然后你可以使用如下方法:
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);
}
}
那么,现在你有了整行,但你只需要行中的一个元素,对吧? :)
View wantedViewRow = listView.getChildAt(1);
TextView txt = (TextView) wantedViewRow .findViewById(R.id.yourTitleTextView);
txt.setError("Sweet error!");
大功告成!测试解决方案...
我已经开发了一个 ListView
到弹出对话框中,一切正常 - Spinner
和 CheckBox
的监听器。我可以访问哪个元素已更改值:
微调器:
@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));
}
对于复选框:
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
Log.d("MyLog", "changed value - is checked: " + isChecked);
}
但是如何 我可以访问每个 ListView
元素的标题 - TextView
,例如设置如果微调器更改元素或复选框更改状态会出错?
视图如下所示:
我的全部代码: xml 基本布局
<?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>
自定义元素 - 可以是 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>
我的对话框创建代码很长,但我的想法是使用 JSON obj 设置 BaseAdapter
我在 ListView
中选择我需要添加哪种视图。我有 CheckBox
和 Spinner
.
选中复选框时:
@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");
}
对 Spinner 做同样的事情
@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");
}
试试这个..
您可以使用一些全局的东西来访问您的子视图,然后您可以处理 ListView
行,但要小心不要访问不可见的元素 - 它会抛出 NullPointerException
。一种方法是在设置适配器后获取 listView 行数:
int cout = listView.getChildCount();
然后你可以使用如下方法:
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);
}
}
那么,现在你有了整行,但你只需要行中的一个元素,对吧? :)
View wantedViewRow = listView.getChildAt(1);
TextView txt = (TextView) wantedViewRow .findViewById(R.id.yourTitleTextView);
txt.setError("Sweet error!");
大功告成!测试解决方案...