-1

NFC タグを読み取り、文字列配列内の文字列に対してタグをチェックし、別のアクティビティにテキストを設定するアプリケーションを作成しようとしています。文字列が存在するかどうかをチェックして新しいアクティビティにテキストを設定するように動作させましたが、配列内でチェックする文字列を指定できるようにしたいと考えています。新しいアクティビティで表示したい NFC タグ。私はこれを試しました:

result == getResources().getString(R.string.test_dd)

関連するコードは次のとおりです。

String[] dd;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    dd = getResources().getStringArray(R.array.device_description);

}

@Override
    protected void onPostExecute(String result) {
        if (result != null) {
            if(doesArrayContain(dd, result)) {
            Vibrator v = (Vibrator)getSystemService(Context.VIBRATOR_SERVICE);
            v.vibrate(800);
                    Intent newIntent = new Intent(getApplicationContext(), TabsTest.class);
                    Bundle bundle1 = new Bundle();
                    bundle1.putString("key", result);
                    newIntent.putExtras(bundle1);
                    startActivity(newIntent);
                    Toast.makeText(getApplicationContext(), "NFC tag written successfully!", Toast.LENGTH_SHORT).show();

        }
            else{
                Toast.makeText(getApplicationContext(), result + " is not in the device description!", Toast.LENGTH_SHORT).show();
            }
    }
}

編集:

使用される方法は次のとおりです。この問題について誰か助けてください。

public static boolean doesArrayContain(String[] array, String text) {
    for (String element : array) {
        if(element != null && element.equalsIgnoreCase(text)) {
             return true;
        }
    }
    return false;
}
4

2 に答える 2

1

文字列 (およびその他のオブジェクト) の等価性を比較するには、equals()メソッドを使用します。==オブジェクトの同一性を比較します (同じ文字列オブジェクト)。

于 2013-07-23T10:16:10.570 に答える