仅使用1个字符串生成QR码

时间:2015-07-19 00:56:43

标签: arrays android-intent qr-code

我正在创建一个用于填写表单的应用程序,然后按一个按钮从中生成QR代码。我的问题是我可以用1个字符串来完成它,但我需要7个字符串(表单中的7个字符串)。我试图把“[]”放在它要求的字符串,它没有错误或崩溃,但总是带有1。

 - MainActivity intent

        QR.setOnClickListener(new OnClickListener() {/*Ação do botão gerar código*/

            @Override
            public void onClick(View v) {


                Intent intentQR = new Intent(getApplicationContext(),
                        QRActivity.class);
                intentQR.putExtra("id", v.getId());
                startActivity(intentQR);

            }
        });
  • QRActivity(显示的代码,其示例为String [],在下面的代码中写为“array”,是“nameInput”)

    公共类QRActivity扩展了ActionBarActivity {

    View view;
    ShareScreen screen;
    private String LOG_TAG ="aqui";
    
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_qr);
        view = (View) findViewById(R.id.QrRelative);// your layout id
    
    
        int caller = getIntent().getIntExtra("id",0);
    
    
        switch (caller) {
            case R.id.generateCode:
                String array[] = new String[7];
                String nameInput = MainActivity.name.getText().toString();
                array[0]= nameInput;
                String con1Input = MainActivity.contact1.getText().toString();
                array[1]= con1Input;
                String con2Input = MainActivity.contact2.getText().toString();
                array[2]= con2Input;
                String con3Input = MainActivity.contact3.getText().toString();
                array[3]= con3Input;
                String bloodInput = MainActivity.blood.getText().toString();
                array[4]= bloodInput;
                String allergyInput = MainActivity.allergy.getText().toString();
                array[5]= allergyInput;
                String infoInput = MainActivity.info.getText().toString();
                array[6]= infoInput;
                Log.v(LOG_TAG,nameInput);
    
                WindowManager windowQR = (WindowManager) getSystemService(WINDOW_SERVICE);
                Display displayQR = windowQR.getDefaultDisplay();
                Point pointQR = new Point();
                displayQR.getSize(pointQR);
                int width = pointQR.x;
                int height = pointQR.y;
                int smallerDimension = width < height ? width : height;
                smallerDimension = smallerDimension * 3 / 4;
                QRCodeEncoder qrCodeEncoder = new QRCodeEncoder(array, null, Contents.Type.TEXT, BarcodeFormat.QR_CODE.toString(), smallerDimension);
                try {
    
                    Bitmap bitmap = qrCodeEncoder.encodeAsBitmap();
                    ImageView imageQR = (ImageView) findViewById(R.id.imageQR);
                    imageQR.setImageBitmap(bitmap);
                } catch (WriterException e) {
                    e.printStackTrace();
                }
                break;
        }
    
    
    }
    

1 个答案:

答案 0 :(得分:0)

我认为您需要创建一个java objectparcel。 E.g:

public class Category implements Parcelable {

    public static final String LOG_TAG = Category.class.getSimpleName();

    private String id;
    private String name;

    public Category() {
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Category(Parcel origin) {
        setId(origin.readString());
        setName(origin.readString());
    }

    @Override
    public int describeContents() {
        return 0;
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeString(id);
        dest.writeString(name);
    }

    public static final Creator<Category> CREATOR = new Creator<Category>() {
        @Override
        public Category createFromParcel(Parcel source) {
            return new Category(source);
        }

        @Override
        public Category[] newArray(int size) {
            return new Category[size];
        }
    };
}

然后,您可以实例化您的模型并将其包含在您的意图中。 Android适用于包裹。

Category category = new Category();
category.setName("Some name");
Intentent intentQR = new Intent(getApplicationContext(),
                        QRActivity.class);
                intentQR.putExtra("extra_category", category);
                startActivity(intentQR);