这是我的代码:
Functionallity显示一个alertDialog,其中包含“Costs”列表。如果用户选中其中一个,将启用EditBox,然后他可以在其上插入金额。
我创建了一个方法来显示AlertDialog喜欢弹出窗口。
我认为AlertDialog需要使用在R.layout.cost_list上定义的ListView(它不是我的主要视图)。
然后,此ListView将使用each_cost.xml进行调整,其中包含CheckBox,TextView和EditText。我使用自定义适配器(CostAdapter)。
但是当我运行它时,会显示illegalStateException。为什么?非常感谢。
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();
}
}
答案 0 :(得分:0)
这是解决方案。
现在,我需要在选中CheckBox时检索EditText值,然后单击OK按钮......但这是另一个问题。
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];
}
}
ListAdapter adapter = new CostAdapter(this, cos);
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle(getString(R.string.label_costs));
builder.setAdapter(adapter, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
}
});
builder.setPositiveButton(getString(R.string.label_ok), new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int id) {
labelCosts.setText("Costs: ");
for (long x:arrayCostIdSelected) {
int i = Long.valueOf(x).intValue() - 1;
labelCosts.setText(labelCosts.getText() + cos.get(i).getCostDescription() + sepChar);
}
labelCosts.setText(labelCosts.getText().toString().substring(0,labelCosts.getText().length()-2));
previousArrayCostIdSelected = new ArrayList<Integer>();
for (int x:arrayCostIdSelected) {
previousArrayCostIdSelected.add(x);
}
previousArrayBooleanCostSelected = new boolean[arrayCostCode.size()];
for (int b = 0; b < arrayBooleanCostSelected.length; b++) {
previousArrayBooleanCostSelected[b] = arrayBooleanCostSelected[b];
}
}
});
builder.setNegativeButton(getString(R.string.label_cancel), new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int id) {
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];
}
}
});
builder.create();
builder.show();
}