I'm trying to create a small calculator widget but I can not concatenate the input string using StringBuilder, because the widget at every click on the button only displays the string corresponding to the button clicked.

It seems that the append method in StringBuilder is not working. Could you give me some help?

Here are the relevant parts of the code:

StringBuilder sb  = new StringBuilder();
@Override
public void onReceive(Context context, Intent intent) {
    final String action = intent.getAction();

    Log.i(LOG_TAG, "onReceive(): " + action);
    if (ACTION_WIDGET_CONTROL.equals(action)) {
        final int appWidgetId = intent.getIntExtra(
                AppWidgetManager.EXTRA_APPWIDGET_ID,
                AppWidgetManager.INVALID_APPWIDGET_ID);
        if (appWidgetId != AppWidgetManager.INVALID_APPWIDGET_ID) {
            fnHandleCommand(context, intent, appWidgetId);
        }
    } else {
        super.onReceive(context, intent);
    }
}

private void fnHandleCommand(Context context, Intent intent, int appWidgetId) {
    int control_id = intent.getIntExtra(COMMAND, -1);

    switch (control_id) {
    case R.id.Button00:
        String zero = "0";
        sb.append(zero);
        setInputText(context, appWidgetId,sb.toString());
        break;
    case R.id.Button01:
        String uno = "1";
        sb.append(uno);
        setInputText(context, appWidgetId,sb.toString());
        break;
    case R.id.Button02:
        String due = "2";
        sb.append(due);
        setInputText(context, appWidgetId,sb.toString());
        break;
    case R.id.Button03:
        String tre = "3";
        sb.append(tre);
        setInputText(context, appWidgetId,sb.toString());
        break;
    case R.id.Button04:
        String quattro = "4";
        sb.append(quattro);
        setInputText(context, appWidgetId,sb.toString());
        break;
    case R.id.Button05:
        String cinque = "5";
        sb.append(cinque);
        setInputText(context, appWidgetId,sb.toString());
        break;
    case R.id.Button06:
        String sei = "6";
        sb.append(sei);
        setInputText(context, appWidgetId,sb.toString());
        break;
    case R.id.Button07:
        String sette = "7";
        sb.append(sette);
        setInputText(context, appWidgetId,sb.toString());
        break;
    case R.id.Button08:
        String otto = "8";
        sb.append(otto);
        setInputText(context, appWidgetId,sb.toString());
        break;
    case R.id.Button09:
        String nove = "9";
        sb.append(nove);
        setInputText(context, appWidgetId,sb.toString());
        break;
    case R.id.ButtonPlus:
        String piu = "+";
        sb.append(piu);
        setInputText(context, appWidgetId,sb.toString());
        break;
    case R.id.ButtonMinus:
        String meno = "-";
        sb.append(meno);
        setInputText(context, appWidgetId,sb.toString());
        break;
    case R.id.ButtonMutiple:
        String per = "*";
        sb.append(per);
        setInputText(context, appWidgetId,sb.toString());
        break;
    case R.id.ButtonDivide:
        String diviso = "/";
        sb.append(diviso);
        setInputText(context, appWidgetId,sb.toString());
        break;
    case R.id.ButtonNegative:
        String rad = "\u221a";
        sb.append(rad);
        setInputText(context, appWidgetId,sb.toString());
        break;
    case R.id.ButtonAC: 
        String vuota = "";
        sb.setLength(0);
        cancel(context, appWidgetId,sb.toString(),vuota);
        break;
    default:
        break;
    }
    fnVibrate(context);
}

private void setInputText(Context context, int appWidgetId, String value) {
    RemoteViews remoteView = new RemoteViews(context.getPackageName(),
            R.layout.simple_calculator);
    remoteView.setTextViewText(R.id.TextValue, value);
    AppWidgetManager.getInstance(context).updateAppWidget(appWidgetId,
            remoteView);
}

Thanks

有帮助吗?

解决方案

private static StringBuilder sb = new StringBuilder();

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top