简体   繁体   English

如何从适配器访问我的主要活动以及如何获取视图中元素的状态?

[英]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 为此,我想

  • Get the checkboxes which are checked 获取选中的复选框
  • Erase all the pin from the maps (To do that, I need to reach the main_activity somehow) 从地图上删除所有图钉(为此,我需要以某种方式到达main_activity)
  • Show the pins related to the checked checkboxes 显示与选中的复选框相关的图钉

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. 否则,您可以在要显示地图的活动中阅读首选项。

  1. Read the preferences, and update your checkboxes. 阅读首选项,然后更新您的复选框。 When the user changes the checkbox, update the preferences 当用户更改复选框时,更新首选项
  2. In your map display activity, read the preferences, and PIN accordingly. 在地图显示活动中,阅读首选项,并相应地输入PIN码。 Also when the user pins/unpins, you can update the preferences. 同样,当用户固定/取消固定时,您可以更新首选项。

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.

相关问题 如何从适配器访问外部视图 - How can I get access to outside views from an Adapter 如何从主要活动类中的自定义列表视图适配器管理onClick函数 - How can I manage onClick function from my custom listview adapter in main activity class 如何从Android中的活动访问自定义适配器内部片段的UI元素 - How can I access UI Elements of a fragment inside of a Custom Adapter From an Activity in Android 如何从“设置”活动中的“主要活动”访问方法? - How can I access methods from my Main Activity in my Settings activity? 从自定义适配器(而不是从主要活动)开始意图 - 如何实现活动结果以重新加载适配器中的视图? - Starting an intent from custom adapter (not from the main activity) - how to implement on activity result to reload views in adapter? 如何在自定义适配器中使用我的方法中的视图? - how can i use views from my method in custom adapter? 如何从recyclerview适配器内部访问Activity上的外部VIEWS - How to access outside VIEWS on Activity from inside recyclerview adapter 如何从其 Recycler View Adapter 调用 Activity 的方法? 或者更好,如何从适配器访问活动的 UI 元素? - How do i call a method of an Activity from its Recycler View Adapter? Or better,How to access the UI elements of an activity from the adapter? 如何将用户 ID 从我的适配器传递到活动 - How can i pass user id from my adapter to activity 如何从适配器获取数据并在Android中的Activity中显示 - how can i get data from adapter and show in Activity in Android
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM