我想向InformationActivity发送2个不同的值。
首先:我想发送条形码编号和列表(工作正常)。但由于某种原因,我发送的列表(小麦或小吃或鸡蛋取决于我按下的内容)没有出现在InformationActivity中。
我认为问题是因为它分为两个不同的类。
如何更改代码,以便将条形码字符串和其中一个列表发送到下一个活动中?
PS。我尝试将intent声明为全局变量并且崩溃了应用程序 PSS。目前我只是通过在该阵列中打印i项来测试它。
主:
public class HomeActivity extends AppCompatActivity{
private FirebaseAuth firebaseAuth;
private Button buttonLogout;
private ZXingScannerView scannerView;
private final int permission_code = 1;
Spinner spinner;
ArrayAdapter<CharSequence> adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
firebaseAuth = FirebaseAuth.getInstance();
if(firebaseAuth.getCurrentUser() == null){
finish();
startActivity(new Intent(this, MainActivity.class));
}
FirebaseUser user = firebaseAuth.getCurrentUser();
//android spinner to select profile
spinner = (Spinner)findViewById(R.id.spinnerProfiles);
adapter = ArrayAdapter.createFromResource(this, R.array.restrictions, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_list_item_1);
spinner.setAdapter(adapter);
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
String item = parent.getItemAtPosition(position).toString();
Toast.makeText(getBaseContext(), item + " Selected", Toast.LENGTH_SHORT).show();
Intent intent = new Intent(getApplicationContext(), InformationActivity.class);
switch (position){
case 0:
String[] wheat = getResources().getStringArray(R.array.Wheat);
intent.putExtra("Profile", wheat);
break;
case 1:
String[] crus = getResources().getStringArray(R.array.Crustaceans);
intent.putExtra("Profile", crus);
break;
case 2:
String[] eggs = getResources().getStringArray(R.array.Eggs);
intent.putExtra("Profile", eggs);
break;
}
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
return;
}
});
}
//opens camera when button is pressed
public void scanBarcode(View view) {
//check if user given app camera permissions
if (ContextCompat.checkSelfPermission(this, Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.CAMERA}, permission_code);
}
//opens camera
scannerView = new ZXingScannerView(this);
scannerView.setResultHandler(new ZXingScannerResultHandler());
//stops camera and scannerview
setContentView(scannerView);
scannerView.startCamera();
}
//stops camera and outputs barcode result to a Toast
class ZXingScannerResultHandler implements ZXingScannerView.ResultHandler {
@Override
public void handleResult(Result result) {
String resultBarcode = result.getText();
Intent intent = new Intent(getApplicationContext(), InformationActivity.class);
intent.putExtra("barcode", resultBarcode.toString());
startActivity(intent);
scannerView.stopCamera();
}
}
InformationActivity:
public class InformationActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_information);
TextView barcodeView = (TextView) findViewById(R.id.tvBarcode);
barcodeView.setText(getIntent().getExtras().getString("barcode"));
TextView profileView = (TextView) findViewById(R.id.tvProfile);
String[] Profile = getIntent().getStringArrayExtra("Profile");
profileView.setText(Profile[1]);
}
@Override
public void onBackPressed() {
Intent intent = new Intent(getApplicationContext(), HomeActivity.class);
startActivity(intent);
}
}
答案 0 :(得分:0)
您的import pandas as pd
df = pd.read_csv('kmeans.csv', header=None)
df.plot(kind='scatter', x=0, y=1, c=2, colormap='rainbow', colorbar=None,
markeredgewidth=0.0) # No circle margin!
方法正在向onItemSelected()
对象添加String[]
,但之后只会抛出Intent
。
Intent
稍后,您使用不同 @Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
...
Intent intent = new Intent(getApplicationContext(), InformationActivity.class);
switch (position){
case 0:
String[] wheat = getResources().getStringArray(R.array.Wheat);
intent.putExtra("Profile", wheat);
break;
case 1:
String[] crus = getResources().getStringArray(R.array.Crustaceans);
intent.putExtra("Profile", crus);
break;
case 2:
String[] eggs = getResources().getStringArray(R.array.Eggs);
intent.putExtra("Profile", eggs);
break;
}
}
启动活动:
Intent
重要的是要理解这两个意图不以某种方式共享;第一个与第二个完全分开,只要@Override
public void handleResult(Result result) {
String resultBarcode = result.getText();
Intent intent = new Intent(getApplicationContext(), InformationActivity.class);
intent.putExtra("barcode", resultBarcode.toString());
startActivity(intent);
scannerView.stopCamera();
}
完成执行,意图就会被抛弃。
您应该做的是更改onItemSelected()
方法,以便以某种方式存储有关选择的信息(可能更新活动中的变量),然后更改onItemSelected()
方法以读取保存的信息并将数组添加到intent。也许是这样的:
handleResult()
String[] selectedArray;
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
...
switch (position){
case 0:
selectedArray = getResources().getStringArray(R.array.Wheat);
break;
case 1:
selectedArray = getResources().getStringArray(R.array.Crustaceans);
break;
case 2:
selectedArray = getResources().getStringArray(R.array.Eggs);
break;
}
}
答案 1 :(得分:0)
我认为您应该使用startActivityForResult()来更好地管理您的活动之间的信息交换,并构建一个更加可靠的应用。
答案 2 :(得分:0)
你需要调用scanBarcode()函数来激活意图并使用Ben提供的完美解决方案启动InformationActivity:
String[] selectedArray;
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
...
switch (position){
case 0:
selectedArray = getResources().getStringArray(R.array.name);
break;
case 1:
selectedArray = getResources().getStringArray(R.array.name);
break;
}
}
也简单地调用finish();函数在InformationActivity的onBackPressed()中导航到Mainactivity:
@Override
public void onBackPressed() {
super.finish(); // or use super.onBackPressed();
}
快乐编码:)