[英]How can I access my main activity from an adapter and how to get the state of elements in views?
I have an Android activity which is a map and an ExpandableListView. 我有一个Android活动,它是一个地图和一个ExpandableListView。 This ExpandableListView is populated using a collection built in run time and an adapter.
使用在运行时内置的集合和适配器来填充此ExpandableListView。 This list view contains a label, an image and a checkbox.
该列表视图包含一个标签,一个图像和一个复选框。 They should work as filters: if the checkbox is checked, the items related to that label should appear in my map.
它们应充当过滤器:如果选中此复选框,则与该标签相关的项目应出现在我的地图中。 If the checkbox is not checked.
如果未选中该复选框。 These items should disappear.
这些项目应消失。
This is the code for my activity and adapter (most important parts) 这是我的活动和适配器的代码(最重要的部分)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/list_item_child"
android:gravity="center_vertical"
android:background="@android:color/white">
<CheckBox
android:id="@+id/filtro_checkbox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="true"/>
<ImageView
android:id="@+id/filtro_imgView"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/list_item_text_child"
android:textSize="20sp"
android:padding="10dp"
android:layout_marginLeft="5dp"/>
</LinearLayout>
Code: 码:
@Override
//in this method you must set the text to see the children on the list
public View getChildView(int i, int i1, boolean b, View view, final ViewGroup viewGroup) {
if (view == null) {
view = inflater.inflate(R.layout.list_item_child, viewGroup,false);
}
TextView textView = (TextView) view.findViewById(R.id.list_item_text_child);
//"i" is the position of the parent/group in the list and
//"i1" is the position of the child
Filtro child = mParent.get(i).getArrayChildren().get(i1);
textView.setText(child.getTitle());
ImageView imageView = (ImageView) view.findViewById(R.id.filtro_imgView);
//"i" is the position of the parent/group in the list
Drawable drawable = view.getResources().getDrawable(child.getImage());
imageView.setImageDrawable(drawable);
CheckBox chkbox = (CheckBox) view.findViewById(R.id.filtro_checkbox);
chkbox.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
View view2 = inflater.inflate(R.layout.list_item_child, viewGroup,false);
// TODO Auto-generated method stub
if (buttonView.isChecked()) {
Toast.makeText(view2.getContext(), "Checked",
Toast.LENGTH_SHORT).show();
}
else
{
Toast.makeText(view2.getContext(), "UnChecked",
Toast.LENGTH_SHORT).show();
}
}
});
//return the entire view
return view;
}
I am new to Android development, but as far as I understood, this method code will "instantiate" the activity layout for each item. 我是Android开发的新手,但据我了解,此方法代码将“实例化”每个项目的活动布局。 This is working and the items are being populated in the emulator.
这正在工作,并且项目正在模拟器中填充。 Then, I added the onCheckedChanged listener.
然后,我添加了onCheckedChanged侦听器。 It works, the toast is shown.
它有效,显示了吐司。 But now I would like to make the pins show/hide depending on the checkbox selection.
但是现在,我想根据复选框选择显示/隐藏图钉。
To do that, I would like to 为此,我想
I do not know how to perform these steps, I thought about using a singleton or an observer design pattern to reach the MainActivity in order to be able to call the methods to reload the pins. 我不知道如何执行这些步骤,我考虑过使用单例或观察者设计模式来到达MainActivity,以便能够调用方法来重新加载引脚。 Would that be an elegant approach?
那会是一个优雅的方法吗? How could I check the state (checked/not checked) for all of my checkboxes?
如何检查所有复选框的状态(已选中/未选中)?
Thanks 谢谢
You can use preferences to store the value of checked or unchecked items. 您可以使用首选项来存储选中或未选中项目的值。 Also you can register a listener when the preferences are changed.
您还可以在更改首选项时注册一个侦听器。 Otherwise, you can read the preferences in the activity where you want to display the maps.
否则,您可以在要显示地图的活动中阅读首选项。
https://developer.android.com/reference/android/preference/Preference.html https://developer.android.com/reference/android/preference/Preference.html
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.