I am writing an app that uses and externally connected USB barcode/RFID scanner. The data being scanned in, is "compound" data. Here is an example:
=+03000=W12560712345600=%2800&>0090452359
That is from a compound data scan. The delimiter in the data is the equals sign (=) or the ampersand (&). The first bit =+03000
says that there are three data parts in the scan:
=W12560712345600
=%2800
&>0090452359
This data can have any number of data parts from one to N.
In my Android app, I have a form with three EditText
elements. What I need to do with this compound scanned data is to break it up using the delimiters, and stick each piece of data into the proper EditText
field.
I have, after much pain, been made aware of the fact that I cannot just manipulate stdin, and that I need to use a TextWatcher
on one of my EditText
fields to capture the scanned data so I can manipulate it.
My issue is that I cannot figure out how to do this. Here is what I have:
activity_main.xml
<LinearLayout>
<TextView />
<EditText android:id="@+id/datafield01" />
<EditText android:id="@+id/datafield02" />
<EditText android:id="@+id/datafield03" />
<Button />
</LinearLayout>
MainActivity.java
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
EditText dataField01 = (EditText)findViewById(R.id.datafield01);
EditText dataField02 = (EditText)findViewById(R.id.datafield02);
EditText dataField02 = (EditText)findViewById(R.id.datafield03);
dataField01.addTextChangedListener(editTextWatcher);
}
TextWatcher editTextWatcher = new TextWatcher(){
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after){
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count){
}
@Override
public void afterTextChanged(CharSequence s){
}
};
}
I have tried capturing the CharSequence
into a StringBuffer
- using before, on, and afterTextChanged - so I can manipulate it and place it into the correct EditText
element, but I have been unsuccessful.
Modified MainActivity.java
public class MainActivity extends AppCompatActivity {
private StringBuffer stringBuffer;
@Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
stringBuffer = new StringBuffer();
EditText dataField01 = (EditText)findViewById(R.id.datafield01);
EditText dataField02 = (EditText)findViewById(R.id.datafield02);
EditText dataField02 = (EditText)findViewById(R.id.datafield03);
dataField01.addTextChangedListener(editTextWatcher);
System.out.println(stringBuffer.toString());
}
TextWatcher editTextWatcher = new TextWatcher(){
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after){
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count){
stringBuffer.append(s);
}
@Override
public void afterTextChanged(CharSequence s){
}
};
}
Whenever I try to System.out.println(stringbuffer);
there is nothing there.
The scanned code does not give a line break or any other kind of delimiter that it has come to an end, and the number of data parts can be one to N, but each data part does have a known, fixed length.
There has got to be other people doing something like this, but my Google searches have been fruitless.
So, is there any way to accomplish what I am trying to do, or am I completely out of luck?
Thanks.