Zxing Compare Result against local Data? - java

#Override
public void onActivityResult ( int requestCode, int resultCode, Intent data){
IntentResult result = IntentIntegrator.parseActivityResult(requestCode, resultCode, data);
if (result != null) {
if (result.getContents() == null) {
Log.d("MainActivity", "Cancelled");
Toast.makeText(this, "Cancelled", Toast.LENGTH_LONG).show();
} else {
Log.d("MainActivity", "Scanned");
Toast.makeText(this, "Scanned: " + result.getContents(), Toast.LENGTH_LONG).show();
}
} else {
super.onActivityResult(requestCode, resultCode, data);
}
}
Current working method without comparing against Data^
Below is method where Result is compared with Data to see if there is a duplicate(not working)
#Override
public void onActivityResult ( int requestCode, int resultCode, Intent data){
IntentResult result = IntentIntegrator.parseActivityResult(requestCode, resultCode, data);
if (result != null) {
if (result.getContents() == 555821282,8882288,888822884,1145698,248635887,4477996) {
Log.d("MainActivity", "Yes");
Toast.makeText(this, "Yes", Toast.LENGTH_LONG).show();
} else {
Log.d("MainActivity", "No");
Toast.makeText(this, "No: " + result.getContents(), Toast.LENGTH_LONG).show();
}
} else {
super.onActivityResult(requestCode, resultCode, data);
}
}
I get the Integer is too large. I have 500 UPC to put there but I cant even put one. Can I put the numbers in a file somewhere on Android Studio and reference it there? What is a solution to this problem?

IntentResut.getContents() returns a String. To compare two strings in Java, you should use the String.equals(String) method. Your onActivityResult should look like this
#Override
public void onActivityResult ( int requestCode, int resultCode, Intent data){
IntentResult result = IntentIntegrator.parseActivityResult(requestCode, resultCode, data);
if (result != null) {
String data = result.getContents();
if (data != null) {
if (data.equals("555821282")
|| data.equals("8882288")
|| data.equals("888822884")
/* ... rest of the options ... */ ) {
Log.d("MainActivity", "Yes");
Toast.makeText(this, "Yes", Toast.LENGTH_LONG).show();
} else {
Log.d("MainActivity", "No");
Toast.makeText(this, "No: " + result.getContents(), Toast.LENGTH_LONG).show();
}
} else {
// No data
}
} else {
super.onActivityResult(requestCode, resultCode, data);
}
}
Since you have 500 UPCs to compare, you probably don't want to write a 500 line if-statement. Instead I'd recommend putting all the UPCs (as strings) into a List and then your if-statement can check if the list contains the barcode.
List<String> knownUpcs = Arrays.asList("555821282", "8882288", "888822884", "1145698", "248635887", "4477996");
...
if (knownUpcs.contains(data)) {
// do your thing
}
Here the list is defined statically, but there is no reason you couldn't create the list by reading the UPCs from a file on disk.

Related

Crop Image Activity-Android Studio

protected void onActivityResult(int requestCode, int resultCode, #Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == CropImage.CROP_IMAGE_ACTIVITY_REQUEST_CODE ){
CropImage.ActivityResult result = CropImage.getActivityResult(data);
if(resultCode == RESULT_OK) {
imageUri = result.getUri();
image_added.setImageURI(imageUri);
}
} else {
Toast.makeText(this, "Something went wrong , try again!", Toast.LENGTH_SHORT).show();
startActivity(new Intent(PostActivity.this , MainActivity2.class));
finish();
}
}
My code still works but when I select the image from gallery it does not get selected it directly gives the toast "Something went wrong , try again!"

How do I pass ArrayLists to another activity, modify it, and return it back to use?

I'm trying to create a login/register part of a project, and I'm having trouble with passing the sign-up information back to the login activity. I initialized username_info, password_info, and name_info in MainActivity, and I want to send it to SignUpActivity through Intent.
Intent i = new Intent(this, SignUpActivity.class);
i.putExtra("username_info", username_info);
i.putExtra("password_info", password_info);
i.putExtra("name_info", name_info);
startActivityForResult(i, 101);
After values are added in the other activity, it's sent back like this (the arraylists have the same name in both activities):
Intent r = new Intent();
r.putExtra("username_info", username_info);
r.putExtra("password_info", password_info);
r.putExtra("name_info", name_info);
setResult(Activity.RESULT_OK, r);
finish();
}
and it's received here:
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == 101) {
if (resultCode == Activity.RESULT_OK) {
Bundle e = getIntent().getExtras();
username_info = e.getStringArrayList("username_info");
password_info = e.getStringArrayList("password_info");
name_info = e.getStringArrayList("name_info");
}
}
}
But the array lists are unchanged when I get back to the MainActivity. I'm new to Android Studio, so I might just be making a simple mistake.
EDIT:
I'm crashing when the username and password don't match, but it should be returning a toast instead:
#Override
public void onClick(View v) {
username = username_input.getText().toString();
password = password_input.getText().toString();
int index = username_info.indexOf(username);
if (username_info.size() < 1) {
Toast.makeText(MainActivity.this, "You must sign up first", Toast.LENGTH_SHORT).show();
}
else if (password_info.get(index).equals(password)) {
Toast.makeText(MainActivity.this, "make an activity", Toast.LENGTH_SHORT).show();
// open activity
}
else {
Toast.makeText(MainActivity.this, "Incorrect username/password", Toast.LENGTH_SHORT).show();
}
}
});
Don't use getIntent() in onActivityResult, instead use the Intent data:
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == 101) {
if (resultCode == Activity.RESULT_OK) {
Bundle e = data.getExtras();
username_info = e.getStringArrayList("username_info");
password_info = e.getStringArrayList("password_info");
name_info = e.getStringArrayList("name_info");
}
}
}

How to access bundle that AccountManager.newChooseAccountIntent returns?

I am using the intent newChooseAccountIntent to have a user pick an account. I read here that the intent returns a bundle with the keys KEY_ACCOUNT_NAME for account name and KEY_ACCOUNT_TYPE for account type.
https://developer.android.com/reference/android/accounts/AccountManager.html
My question is how do I access the bundle the intent returns? Would I do data.getBundleExtra() in my activity result function?
Activity result function code
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
callbackManager.onActivityResult(requestCode, resultCode, data);
super.onActivityResult(requestCode, resultCode, data);
if(resultCode == RESULT_OK) {
Log.d("frag", "is return empty " + data.getBundleExtra("KEY_ACCOUNT_NAME"));
}
else if (resultCode == RESULT_CANCELED) {
Log.d("frag", "intent fired and something went wrong");
}
}
As per the Javadoc, both AccountManager.KEY_ACCOUNT_NAME and AccountManager.KEY_ACCOUNT_TYPE are strings, meaning you use getStringExtra():
if(resultCode == RESULT_OK) {
String name = data.getStringExtra(AccountManager.KEY_ACCOUNT_NAME);
String type = data.getStringExtra(AccountManager.KEY_ACCOUNT_TYPE);
Log.d("frag", "Got " + name + ", " + type);
}

How to adding image item to RecyclerView using a button without internet?

I am trying to use a adding button to get the photo from the gallery and put it to RecyclerView with text. When the user selected image form the gallery, the application will return the image path, but now, I no idea how to use the path to get the image and put it to RecyclerView to display with text, here is my coding:
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
try {
if (requestCode == RESULT_LOAD_IMG && resultCode == RESULT_OK
&& null != data) {
Uri selectedImage = data.getData();
Toast.makeText(getActivity(), "Yor are picked image from" + selectedImage, Toast.LENGTH_LONG).show();
} else {
Toast.makeText(getActivity(), "You haven't picked image.",
Toast.LENGTH_LONG).show();
}
} catch (Exception e) {
Toast.makeText(getActivity(), "Something went wrong.", Toast.LENGTH_LONG)
.show();
}
}
Sorry for my English. Thank you for help.
Here is my adding button
In your adapter class you have to implement add method, something like this:
public void addImage(String imagePath) {
imagesPaths.add(imagePath);
notifyDataSetChanged();
}
notifyDataSetChanged() is crucial, because it calls for update on RecyclerView.
So in your onActivityResult you just have to pass image path to adapter:
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
try {
if (requestCode == RESULT_LOAD_IMG && resultCode == RESULT_OK
&& null != data) {
Uri selectedImage = data.getData();
// Here you pass image path
yourAdapter.add(selectedImage.toString());
Toast.makeText(getActivity(), "Yor are picked image from" + selectedImage, Toast.LENGTH_LONG).show();
} else {
Toast.makeText(getActivity(), "You haven't picked image.",
Toast.LENGTH_LONG).show();
}
} catch (Exception e) {
Toast.makeText(getActivity(), "Something went wrong.", Toast.LENGTH_LONG)
.show();
}
}

Receiving result in android

I am beginner to android..I Integrated qr scanner in my app..problem is I added qr scanner button in two fragment..for receiving result I added on activityResult method..In my mainactivity..Is any another method for receiving result in android Instead of OnactivityResult method..If not their I should I add multiple onActivityResult method In my Mainactivty..can anyOne help me..
below is the my code for onActivityresult
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
IntentResult scanResult =IntentIntegrator.parseActivityResult(requestCode, resultCode,
data);
if (scanResult != null) {
if (scanResult.getContents() == null) {
Log.d("ScanFragment", "Cancelled scan");
Toast.makeText(this, "Cancelled", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(this, " " + scanResult.getContents(), Toast.LENGTH_SHORT).show();
}
} else {
//result fragment
super.onActivityResult(requestCode, resultCode, data);
}
// }
}
static final int FRAGMENT_ONE_REQUEST = 1; // The request code
static final int FRAGMENT_TWO_REQUEST = 2; // The request code
then use this from fragment one
startActivityForResult(intent, FRAGMENT_ONE_REQUEST);
then use this from fragment two
startActivityForResult(intent, FRAGMENT_TWO_REQUEST);
in each fragment
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(resultCode == Activity.RESULT_OK) {
//some code
if (requestCode == FRAGMENT_ONE_REQUEST) {
}
}
}

Categories