0

我有一个 Android 活动,它是一个地图和一个 ExpandableListView。此 ExpandableListView 使用运行时内置的集合和适配器填充。这个列表视图包含一个标签、一个图像和一个复选框。它们应该用作过滤器:如果选中该复选框,则与该标签相关的项目应该出现在我的地图中。如果未选中复选框。这些项目应该消失。

这是我的活动和适配器的代码(最重要的部分)

<?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>

代码:

@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;
    }

我是 Android 开发新手,但据我了解,此方法代码将“实例化”每个项目的活动布局。这是有效的,项目正在模拟器中填充。然后,我添加了 onCheckedChanged 监听器。它起作用了,吐司显示出来了。但现在我想根据复选框的选择来显示/隐藏图钉。

为此,我想

  • 获取选中的复选框
  • 擦除地图上的所有图钉(为此,我需要以某种方式到达 main_activity)
  • 显示与选中的复选框相关的引脚

我不知道如何执行这些步骤,我考虑过使用单例或观察者设计模式来访问 MainActivity,以便能够调用方法来重新加载引脚。那会是一种优雅的方法吗?我如何检查所有复选框的状态(选中/未选中)?

谢谢

4

1 回答 1

1

您可以使用首选项来存储选中或未选中项目的值。您还可以在首选项更改时注册一个侦听器。否则,您可以在要显示地图的活动中读取首选项。

  1. 阅读首选项,并更新您的复选框。当用户更改复选框时,更新首选项
  2. 在您的地图显示活动中,阅读首选项和相应的 PIN。此外,当用户固定/取消固定时,您可以更新首选项。

https://developer.android.com/reference/android/preference/Preference.html

于 2012-12-11T00:08:33.127 回答