我有一個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以成爲可以調用重新加載引腳的方法。 這是一個優雅的方法嗎? 如何檢查我的所有複選框的狀態(選中/未選中)?
謝謝
這是一個很好的方法,但是我不知道這是否適合我的情況:我有3個複選框充當過濾器。當用戶點擊一個按鈕時,我想獲得所有選中的複選框並應用過濾器。是否有可能以某種方式獲取選中的複選框? – JSBach
在您點擊按鈕時,您可以使用以下代碼獲取複選框的狀態: final CheckBox checkBox =(CheckBox)findViewById(R.id.checkbox_id); if(checkBox.isChecked()){ } –
是的,但是這是使用適配器和inflater生成的(對不起,如果我搞砸了術語)。所以我會列出一些項目,併爲每個項目生成相同的佈局(因此,相同的複選框)。我如何獲得每個項目的複選框? – JSBach