patternjavaMinor
Displaying NFC card data in Binary, Hex, String, etc
Viewed 0 times
carddisplayingnfchexbinarydatastringetc
Problem
I am working on an Android application for reading NFC cards. I came to a point when I needed to design an
I became aware that I wanted to use what I later discovered is named the "Back Stack", so I wrote up a small "testing" application project in Eclipse.
The following small files are the result of this morning's adventurous discovery:
AndroidManifest.xml
MainActivity.java
```
package com.example.tests;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
public class MainActivity extends Activity {
public static final String EXTRA_LABEL = "extra";
private EditText text;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
LinearLayout layout = new LinearLayout(this);
layout.setOrientation(LinearLayout.VERTICAL);
text = new EditText(this);
text.setText("initial value");
Button button = new Button(this);
button.setText("call");
final MainActivity parent = this;
button.setOnClickListener(new
abstract class to streamline the process of creating views for visualizing and editing various forms of data by subclassing the abstract class with views like BinaryView, HexView, StringView, et cetera.I became aware that I wanted to use what I later discovered is named the "Back Stack", so I wrote up a small "testing" application project in Eclipse.
developer.android.com kindly provided just what I needed; and after some research I was finally able to discover a reliable way of transmitting data to the child Activity during onCreate, and lastly a method for the parent Activity to accept data from the child Activity via all three finish() equivalents (the Action Bar "back" button, the previously hardware "back" button, and lastly a custom button within the child activity's view).The following small files are the result of this morning's adventurous discovery:
AndroidManifest.xml
MainActivity.java
```
package com.example.tests;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
public class MainActivity extends Activity {
public static final String EXTRA_LABEL = "extra";
private EditText text;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
LinearLayout layout = new LinearLayout(this);
layout.setOrientation(LinearLayout.VERTICAL);
text = new EditText(this);
text.setText("initial value");
Button button = new Button(this);
button.setText("call");
final MainActivity parent = this;
button.setOnClickListener(new
Solution
Style
You're a little inconsistent about how you're placing your
Other times, you place them like this:
Personally, I'd prefer the top version, but as long as you're being consistent, either style is probably okay.
The code contained in the file
There are more blank lines, but it's overall, much easier to read.
Nitpicks
This line here:
Should be either of these two things:
I'd personally go with the function parameter, as it's a pain to go and change a constant variable to a different value each time you need it to have a different value, but if
If there's anything else you want me to review, just mention it in the comments, and I'll see what I can do.
You're a little inconsistent about how you're placing your
@Overrides. Sometimes, you place them like this:@Override
public void foo( ... ) {
...
}Other times, you place them like this:
@Override public void foo( ... ) {
...
}Personally, I'd prefer the top version, but as long as you're being consistent, either style is probably okay.
The code contained in the file
MainActivity.java suffers from a lack of blank lines, and whitespace in general, which makes it very hard to read. I took the liberty of adding a few blank lines, and some whitespace in there, and this was the result:...
public class MainActivity extends Activity {
public static final String EXTRA_LABEL = "extra";
private EditText text;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
LinearLayout layout = new LinearLayout(this);
layout.setOrientation(LinearLayout.VERTICAL);
text = new EditText(this);
text.setText("initial value");
Button button = new Button(this);
button.setText("call");
final MainActivity parent = this;
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Activity child = new ChildActivity();
Intent intent = Intents.addStackActivity(parent, child);
intent.putExtra(EXTRA_LABEL, text.getText().toString());
startActivityForResult(intent, ChildActivity.EXTRA_LABEL_CODE);
}
});
layout.addView(button);
layout.addView(text);
this.setContentView(layout);
}
@Override
public void onActivityResult(int requestCode, int resultCode, final Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == ChildActivity.EXTRA_LABEL_CODE) {
runOnUiThread(new Runnable() {
@Override
public void run() {
text.setText(data.getStringExtra(ChildActivity.EXTRA_LABEL));
}
});
}
}
}There are more blank lines, but it's overall, much easier to read.
Nitpicks
This line here:
int id = 1;Should be either of these two things:
- A constant.
- A function parameter.
I'd personally go with the function parameter, as it's a pain to go and change a constant variable to a different value each time you need it to have a different value, but if
id is always going to be equal to 1, then it should probably just look like this:final int ID = 1;If there's anything else you want me to review, just mention it in the comments, and I'll see what I can do.
Code Snippets
@Override
public void foo( ... ) {
...
}@Override public void foo( ... ) {
...
}...
public class MainActivity extends Activity {
public static final String EXTRA_LABEL = "extra";
private EditText text;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
LinearLayout layout = new LinearLayout(this);
layout.setOrientation(LinearLayout.VERTICAL);
text = new EditText(this);
text.setText("initial value");
Button button = new Button(this);
button.setText("call");
final MainActivity parent = this;
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Activity child = new ChildActivity();
Intent intent = Intents.addStackActivity(parent, child);
intent.putExtra(EXTRA_LABEL, text.getText().toString());
startActivityForResult(intent, ChildActivity.EXTRA_LABEL_CODE);
}
});
layout.addView(button);
layout.addView(text);
this.setContentView(layout);
}
@Override
public void onActivityResult(int requestCode, int resultCode, final Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == ChildActivity.EXTRA_LABEL_CODE) {
runOnUiThread(new Runnable() {
@Override
public void run() {
text.setText(data.getStringExtra(ChildActivity.EXTRA_LABEL));
}
});
}
}
}int id = 1;final int ID = 1;Context
StackExchange Code Review Q#104974, answer score: 3
Revisions (0)
No revisions yet.