【问题标题】:IllegalStateException using a custom listView and custom adapter on alert dialog在警报对话框上使用自定义 listView 和自定义适配器的 IllegalStateException
【发布时间】: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


【解决方案1】:

这就是解决方案。

现在我需要在 CheckBox 被选中并单击 OK 按钮时检索 EditText 值...但这是另一个问题。

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();
}

【讨论】:

    猜你喜欢
    • 2021-12-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-04-13
    • 1970-01-01
    相关资源
    最近更新 更多