我已经查看了各种与复选框相关的问题,但仍在努力寻找适合我的问题的答案。 我有一个正常的复选框要求,供用户检查和取消选中。 '图层'由。检查 用户将在背景地图中可见,未选中的部分将不可见。
我的问题是我无法正确维护状态,以确定何时必须检查哪些复选框 每次都会打开对话框。目前,假设我检查第1层和第5层并将它们设置为可见 然后我取消选中后面的第1层,以便消失1; 1和5都消失了。 我觉得这可能是由于我用来设置布尔值的全局变量。如果有人能帮助我会很棒。感谢。
布局layer_checkbox.xml:
<?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="wrap_content" >
<TextView
android:id="@+id/label"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@+id/check"
android:text="@+id/label"
android:textSize="20sp"
android:padding="8dp"
android:textColor="#0d00ff" >
</TextView>
<CheckBox
android:id="@+id/check"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_marginLeft="4px"
android:layout_marginRight="10px"
>
</CheckBox>
</RelativeLayout>
以下是我的方法: 1.方法initLayerManagementDialog():
//These two are global variables.
public CharSequence[] charSequenceItems;
public boolean checked1[];
//This method is called when the Layers option is clicked.
public void initLayerManagementDialog()
{
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Layers");
AlertDialog dialog;
charSequenceItems = map.getLayerList().toArray(new CharSequence[map.getLayerList().size()]);
final boolean [] checked = new boolean[charSequenceItems.length];
checked1 = new boolean[charSequenceItems.length];
LayerAdapter ladapter = new LayerAdapter(myInstance.mApplicationContext, map.getLayerList());
builder.setAdapter(ladapter,new DialogInterface.OnClickListener() {
//This is an empty onClick method. Looking for a cleaner way to avoid this.
@Override
public void onClick(DialogInterface dialog, int which) {
System.out.println("CheckBox button Clicked>>>");
}
});
builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int id) {
for(int i = 0; i < map.getLayerList().size();i++){
map.setLayerVisibility( map.getLayerByName(map.getLayerList().get(i)).getName(), checked1[i]);
}
mapView.invalidate();
}
});
builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int id) {
}
});
dialog=builder.create();
dialog.show();
dialog.setCanceledOnTouchOutside(true);
}
自定义适配器:
public class LayerAdapter extends ArrayAdapter {
private Context context;
private List<String> SymbolList;
boolean isChecked = false;
public LayerAdapter(Context pContext, List<String> pSymbolList)
{
super(pContext, R.layout.layer_checkbox, pSymbolList);
this.context = pContext;
this.SymbolList = pSymbolList;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View rowView = inflater.inflate(R.layout.layer_checkbox, parent, false);
final CheckBox cb = (CheckBox) rowView.findViewById(R.id.check);
cb.setChecked(map.getLayerByName(map.getLayerList().get(position)).getVisibility());
TextView textView = (TextView) rowView.findViewById(R.id.label);
textView.setText(SymbolList.get(position));
final int posit = position;
cb.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if (((CheckBox) v).isChecked()) {
checked1[posit] = true;
} else {
checked1[posit] = false;
}
}
});
return rowView;
}
}
答案 0 :(得分:0)
您不需要使用适配器,因为“对话框”构建器可以自动为您创建复选框列表。您也不需要为数组使用成员变量。
public void initLayerManagementDialog()
{
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Layers");
final CharSequence[] charSequenceItems = map.getLayerList().toArray(new CharSequence[map.getLayerList().size()]);
final boolean [] checkedItems = new boolean[charSequenceItems.length];
for (int i=0; i<checkedItems.length; i++){
checkedItems[i] = map.getLayerByName(map.getLayerList().get(i).getVisibility());
}
builder.setMultiChoiceItems(charSequenceItems, checkedItems, new OnMultiChoiceClickListener(){
@Override
public void onClick(DialogInterface dialog, int indexSelected,
boolean isChecked) {
checkedItems[indexSelected] = isChecked;
}
});
builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int id) {
for(int i = 0; i < map.getLayerList().size();i++){
map.setLayerVisibility( map.getLayerByName(map.getLayerList().get(i)).getName(), checkedItems[i]);
}
mapView.invalidate();
}
});
builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int id) {
}
});
AlertDialog dialog=builder.create();
dialog.show();
dialog.setCanceledOnTouchOutside(true);
}
如果你很好奇为什么你的方法不起作用,也许是因为在你的getView
方法中,你每次都直接从地图获得状态,但是你没有立即更新地图状态单击复选框。因此,如果在包含复选框的视图上调用getView
,它将重置为打开对话框之前的值。旋转屏幕时会出现问题,或者如果您有足够的图层,则必须滚动列表以查看所有图层。