我无法弄清楚如何将onActivityResult的结果传递给resultBreakdown。我知道有一个get / set,我看了几个教程,但我只是没有得到它。或者,还有更好的方法?如果我注释掉* / resultBreakdown Class
,程序运行正常旁注,我刚开始使用java / android。我是一个更好的学习者,然后阅读。我知道我的代码有点笨拙。谢谢你的帮助
注意:我编辑了代码以反映建议的更改
dtype('O')
}
答案 0 :(得分:1)
我认为resultCode
requestCode
而不是requestCode == CommonStatusCodes.SUCCESS
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == 0) {
if (resultCode == Activity.RESULT_OK) {
if (data != null) {
Barcode barcode = data.getParcelableExtra("barcode");
barcodeResult.setText("Barcode value : " + barcode.displayValue);
resultsBreakdown(barcode.displayValue)
} else {
barcodeResult.setText("No Barcode Found");
}
super.onActivityResult(requestCode, resultCode, data);
}
}
}
public void resultsBreakdown(String barcodeData){
if (barcodeData.length == (44)) {
pc = (barcodeData.CharSequence(2,10);
pd = (barcodeData.CharSequence(13,18);
sn = (barcodeData.CharSequence(21,27);
nw = (barcodeData.CharSequence(13,18);
)else(
bcc = "invalid barcode";
}
)
}
答案 1 :(得分:1)
发布的代码需要大量的工作 - 所以这个答案很可能只是部分
(1)TextView在错误的位置初始化
声明为类实例变量的TextView
在此时也无法初始化。所以在那里留下声明(声明bardcodeResult):
TextView barcodeResult;
TextView bcc;
TextView pc;
TextView pd;
TextView sn;
TextView nw;
但是以onCreate
的相同方式将初始化移至barcodeResult
方法。
// in onCreate
barcodeResult = (TextView) findViewById(R.id.barcode_result);
bcc = (TextView)findViewById(R.id.bccheck);
pc = (TextView)findViewById(R.id.pcode);
pd = (TextView)findViewById(R.id.pdate);
sn = (TextView)findViewById(R.id.seqnum);
nw = (TextView)findViewById(R.id.nweight);
(2)语法resultsBreakdown
无效且功能错误(需要调用setText
) - 尝试:
public void resultsBreakdown(String result) {
if (result.length() == 44) {
pc.setText(result.CharSequence(2,10));
pd.setText(result.CharSequence(13,18));
sn.setText(result.CharSequence(21,27));
nw.setText(result.CharSequence(13,18));
} else {
bcc.setText("invalid barcode");
}
}
(3)实际调用resultsBreakdown
方法:
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == 0) {
if (requestCode == CommonStatusCodes.SUCCESS) {
if (data != null) {
Barcode barcode = data.getParcelableExtra("barcode");
barcodeResult.setText("Barcode value : " + barcode.displayValue);
//-- THIS LINE WAS ADDED TO CALL METHOD
resultsBreakdown(barcode.displayValue.toString());
} else {
barcodeResult.setText("No Barcode Found");
}
super.onActivityResult(requestCode, resultCode, data);
}
}
}
答案 2 :(得分:0)
resultBreakdown既不是类也不是方法。甚至不是编译。您将resultBreakDown更改为Method:
public void resultsBreakdown(Barcode barcodeResult){
if (barcodeResult.length == (44)) {
pc = (barcodeResult.CharSequence(2,10);
pd = (barcodeResult.CharSequence(13,18);
sn = (barcodeResult.CharSequence(21,27);
nw = (barcodeResult.CharSequence(13,18);
} else{
bcc = "invalid barcode";
}
稍后在onActivityResult中使用条形码参数调用您的方法。
答案 3 :(得分:0)
由于您没有放置
ScanBarcodeActivity
java文件,因此代码不完整。嗯......我在我的项目中放置了一个小代码,它将清除你的概念甚至你可以使用它。
这里假设我在活动MyClassA.java
中是一个按钮,当它被点击时,它将启动另一个活动MyClassB.java
,记住它将启动该活动以及布局..来自您的活动在特定事件中完成它,当它完成时,MyClassA.java
将收集其结果......
点击按钮点击活动MyClassA.java
我想开始一项活动
结果为MyClassB.java
:
ContactsBtn.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
ContactsBtn.setEnabled(false);
Intent intent = new Intent(MyClassA.this, MyClassB.class);
startActivityForResult(intent, REQUEST_CODE);
}
});
REQUEST_CODE
应在MyClassA
课程中宣布为公开static final int REQUEST_CODE = 1;
现在从活动MyClassB.java
开始就特定事件说明点击您要关闭此活动的按钮,并希望将结果发送到之前的活动:
SelectContactsButton.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
Intent intent = new Intent();
intent.putStringArrayListExtra("WhitelistNames", (ArrayList<String>) WhitelistNames);
setResult(Activity.RESULT_OK, intent);
finish();
}
});
这里我将WhitelistNames
数组列表作为结果发送给调用活动。
现在再次从活动MyClassA.java
,你必须收集结果:
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
if (requestCode == REQUEST_CODE)
{
if (resultCode == Activity.RESULT_OK)
{
ArrayList<String> WhitelistNames = data.getStringArrayListExtra("WhitelistNames");
Log.d("ContactsContracts", "\nPREVIOUS LIST : "+ContactsNumbers);
}
}
}
请记住onActivityResult
方法应该是MyClassA.java
类的一部分,不应该在onCreate
中,也不应该在此类的其他现有标准方法中。
希望有所帮助
答案 4 :(得分:0)
感谢大家的帮助。这个按需工作
package com.example.spdwiz18.testproject;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.view.View.OnClickListener;
import com.google.android.gms.common.api.CommonStatusCodes;
import com.google.android.gms.vision.barcode.Barcode;
import java.text.DateFormat;
import java.time.*;
import java.time.temporal.*;
import java.time.LocalDate;
import java.util.Calendar;
import java.util.Date;
public class GrindLogActivity extends AppCompatActivity {
TextView barcodeResult;
TextView bcc;
TextView pc;
TextView pd;
TextView sn;
TextView nw;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.gringlogactivity);
bcc = (TextView)findViewById(R.id.bccheck);
pc = (TextView)findViewById(R.id.pcode);
pd = (TextView)findViewById(R.id.pDate);
// TextView en = (TextView)findViewById(R.id.estnum);
sn = (TextView)findViewById(R.id.seqnum);
nw = (TextView)findViewById(R.id.nweight);
barcodeResult = (TextView) findViewById(R.id.barcode_result);
TextView julianDate = (TextView) findViewById(R.id.datecode);
TextView td1 = (TextView) findViewById(R.id.todaydate1);
// this is how to set id's from the xml file with vNames. (julianDate)
// this is how you get a julian/original date for android
LocalDate now = LocalDate.now();
int julian = now.get(ChronoField.DAY_OF_YEAR);
// this how to set you current date for android
Date date = new Date();
String stringDate = DateFormat.getDateInstance().format(date);
// this is how to set your vNames to your method variables
julianDate.setText(Integer.toString(julian));
td1.setText(stringDate);
}
/*add click event to the scan barcode button */
public void scanBarcode(View v) {
Intent intent = new Intent(this, ScanBarcodeActivity.class);
startActivityForResult(intent, 0);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == 0) {
if (requestCode == CommonStatusCodes.SUCCESS) {
if (data != null) {
Barcode barcode = data.getParcelableExtra("barcode");
barcodeResult.setText("Barcode value : " + barcode.displayValue);
resultsBreakdown(barcode.displayValue.toString());
} else {
barcodeResult.setText("No Barcode Found");
}
super.onActivityResult(requestCode, resultCode, data);
}
}
}
public void resultsBreakdown(CharSequence result) {
if (result.length() == 44) {
pc.setText(result.subSequence(2,10));
pd.setText(result.subSequence(13,18));
sn.setText(result.subSequence(21,27));
nw.setText(result.subSequence(13,18));
} else {
bcc.setText("invalid barcode");
}
}}