在android api

时间:2015-10-29 08:41:44

标签: java android api

我有一个项目,通过使用Zxing lib来吸引学生 和Parse.com存储学生信息,但我遇到了问题 从parse.com检索学生姓名时,有时会显示null或我在异常查询中写的任何内容。

这是我在android studio中的java代码。

/**
 * Initializes controls, values...
 */
private void init() {
    scanButton = (Button) findViewById(R.id.scan_buttun);
    scanButton.setOnClickListener(this);
    HomeButton = (Button) findViewById(R.id.s_home);
    HomeButton.setOnClickListener(this);
    saveButton = (Button) findViewById(R.id.save_button);
    saveButton.setOnClickListener(this);
    showButton = (Button) findViewById(R.id.show_button);
    showButton.setOnClickListener(this);
    resultTextView = (TextView) findViewById(R.id.result_txt);


}

/ *通过扫描来获取学生ID * /

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    IntentResult scanResult = IntentIntegrator.parseActivityResult(requestCode, resultCode, data);
    if(scanResult != null) {
        Log.i("SCAN", "scan result: " + scanResult);
        re = scanResult.getContents();
        resultTextView.setText("Student " + re + " is successfully done" );

    } else
        Log.e("SCAN", "Sorry, the scan was unsuccessful...");


}

/ *使用来自Parse.com * /

的ID来检索学生姓名
public String getStudentInfo (String id_s) {

    ParseQuery<ParseObject> query = ParseQuery.getQuery("Student");
    query.getInBackground(id_s, new GetCallback<ParseObject>() {
        public void done(ParseObject object, ParseException e) {
            if (e == null) {
                name = object.getString("Name");
            } else {
                name = "nothing";
            }
        }
    });
    return name;

}



@Override
public void onClick(View view) {
    int id = view.getId();
    if (id == R.id.scan_buttun) {
        // start scanning
        IntentIntegrator intentIntegrator = new IntentIntegrator(this);
        intentIntegrator.initiateScan();
    }

    if (id == R.id.s_home) {
        // start scanning
        startActivity(new Intent(scan.this, DriverHome.class));
    }

    if (id == R.id.save_button) {
        // save id in array list

        if (student.contains(re)) {
            resultTextView.setText("Sorry " + re + " already existing");
        } else {
            student.add(re);
           /* st = student.get(count);
            count++;
             getStudentInfo(st);*/
           resultTextView.setText("Student " + re + " was saved and the            total number of students is " + student.size());
        }

    }

/ *显示数组列表中的名称* /

          if (id == R.id.show_button) {
        for (int i = 0; i < student.size(); i++) {
            student_id =  student.get(i);
            sName = getStudentInfo(student_id);
            names.add(sName);

        }

        for (int i = 0; i < names.size(); i++) {
            resultTextView.setText(names.get(i)+ " ");

        }



    }

}

0 个答案:

没有答案