【发布时间】:2013-12-26 10:25:16
【问题描述】:
这是我的代码:
Functionallity 显示一个带有“成本”列表的 alertDialog。如果用户选中其中一个,将启用一个 EditBox,然后他可以在其上插入金额。
我创建了一种方法来显示 AlertDialog 喜欢弹出窗口。
我认为 AlertDialog 需要使用在 R.layout.cost_list 上定义的 ListView(这不是我的主要观点)。
然后,这个 ListView 将与 each_cost.xml 进行适配,其中包含 CheckBox、TextView 和 EditText。我为此使用了自定义适配器 (CostAdapter)。
但是当我运行它时,会显示非法状态异常。为什么?非常感谢。
public void showCostsAdapter() {
final ArrayList<Costs> cos;
final ArrayList<String> arrayCostCode = new ArrayList<String>();
// Recuperamos todos
// Creamos un array de los textos.
// Convertimos el array a una secuencia de caracteres para mostrarlo en un AlertDialog
// arrayBooleanSelected: array de booleanos del tamaño del array de lenguages para los check del diálogo. Se inicializan por defecto a false.
// arrayCostIdSelected: array de IDs seleccionados. Se inicializa sólo cuando no se ha seleccionado nada antes
cos = GenericEventMethods.getAllCosts(Integer.valueOf(logInfoOrganizationID));
for (int i = 0; i < cos.size(); i++) {
arrayCostCode.add(cos.get(i).getCostDescription());
}
final CharSequence[] charSeqCostDesc = arrayCostCode.toArray(new CharSequence[arrayCostCode.size()]);
if (arrayCostIdSelected == null) {
previousArrayCostIdSelected = new ArrayList<Integer>();
previousArrayBooleanCostSelected = new boolean[arrayCostCode.size()];
arrayBooleanCostSelected = new boolean[arrayCostCode.size()];
arrayCostIdSelected = new ArrayList<Integer>();
} else {
arrayCostIdSelected = new ArrayList<Integer>();
for (int x:previousArrayCostIdSelected) {
arrayCostIdSelected.add(x);
}
arrayBooleanCostSelected = new boolean[arrayCostCode.size()];
for (int b = 0; b < previousArrayBooleanCostSelected.length; b++) {
arrayBooleanCostSelected[b] = previousArrayBooleanCostSelected[b];
}
}
View view = getLayoutInflater().inflate(R.layout.cost_list, null);
ListView lv = (ListView) view.findViewById(R.id.listCost);
ListAdapter adapter = new CostAdapter(this, cos);
lv.setAdapter(adapter);
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle(getString(R.string.label_costs));
builder.setView(lv);
builder.create();
builder.show();
}
private class CostAdapter extends BaseAdapter {
private ArrayList<Costs> costs;
private Activity activity;
public CostAdapter(Activity act, ArrayList<Costs> cos) {
this.activity = act;
this.costs = cos;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
Costs allCosts;
View v = convertView;
if (v == null) {
LayoutInflater vi = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = vi.inflate(R.layout.each_cost, null);
}
allCosts = costs.get(position);
if (allCosts != null) {
TextView tcostDescription = (TextView) v.findViewById(R.id.costDescription);
EditText tcostAmount = (EditText) v.findViewById(R.id.costAmount);
CheckBox tcostSelectedFlag = (CheckBox) v.findViewById(R.id.costSelection);
tcostDescription.setText(allCosts.getCostDescription());
}
return v;
}
@Override
public int getCount() {
return costs.size();
}
@Override
public Object getItem(int position) {
return costs.get(position);
}
@Override
public long getItemId(int position) {
return costs.get(position).getCostID();
}
}
【问题讨论】:
-
我找到了解决方案。只需要将 de adapter 设置为 builder。
标签: android listview android-custom-view android-alertdialog illegalstateexception