我有一个应用程序,利用TSL1128阅读器读取rfid标签。它使用iascicommandresponder库与读者进行通信。
我有一个名为orderfullfill.java的类,负责listview和更新这个类的ui。第
读取器功能是在另一个类(orderresponder.java)中设置的,该类实现了这个iasciicommandresponder,因此它可以在后台运行,同时orderfullfill显示对arraylist所做的更改。
我已尝试将arrayadapter从orderfullfill.java传递到orderresponder以调用notifydatasetchanged();但这没有效果。我必须在单击按钮时调用addpignumber()方法来更新我的listview。
理想情况下,我需要在列表中添加新标记时更新listview。请有人帮我在正确的位置应用notifydatasetchanged(),这样当我可以添加一个标记时,它会自动添加到列表中。我非常感谢得到的任何帮助。
public class orderFullfill extends AppCompatActivity implements PopupMenu.OnMenuItemClickListener {int where; public static ArrayList scannedPigs = new ArrayList<String>();
// The Reader currently in use
private Reader mReader = null;
private boolean mIsSelectingReader = false;
// The model that performs actions with the reader
private TriggerModel mModel;
public static String dispatchweek;
public static String customer;
public static String accountnumber;
public static String supplyfarm;
public static String breed;
public static int numberofpigs;
public static int pigNum;
public static ArrayAdapter<String> adapter;
public static String weekNow;
public static Context mContext;
Switch trick; TextView nameholder;
/**
* @return the current AsciiCommander
*/
protected AsciiCommander getCommander()
{
return AsciiCommander.sharedInstance();
}
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.orderfullfill);
mContext = orderFullfill.this;
addPigNumber();
InventoryCommand iCommand = InventoryCommand.synchronousCommand();
iCommand.setFilterStrongest(TriState.YES);//high power reading
trick = (Switch) findViewById(R.id.scanM2);
trick.setVisibility(View.INVISIBLE);
trick.setChecked(true);
trick.setText(getResources().getString(R.string.lowPowerScan));
trick.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
InventoryCommand iCommand = new InventoryCommand();
//InventoryCommand iCommand = InventoryCommand.synchronousCommand();
if(isChecked)
{
iCommand.setTakeNoAction(TriState.YES);
iCommand.setFilterStrongest(TriState.YES);// low power reading
getCommander().executeCommand(iCommand);
trick.setText(getResources().getString(R.string.lowPowerScan));
}
if(!isChecked)
{
trick.setText(getResources().getString(R.string.highPowerScan));
iCommand.setFilterStrongest(TriState.NO);//high power reading
iCommand.setTakeNoAction(TriState.NO);
getCommander().executeCommand(iCommand);
}
}
});
nameholder = (TextView)findViewById(R.id.nameView);
nameholder.setText(customer);
AsciiCommander.createSharedInstance(getApplicationContext());
final AsciiCommander commander = getCommander();
// Ensure that all existing responders are removed
commander.clearResponders();
// Add the LoggerResponder - this simply echoes all lines received from the reader to the log
// and passes the line onto the next responder
// This is ADDED FIRST so that no other responder can consume received lines before they are logged.
// commander.addResponder(new LoggerResponder());// logcat input
// Add responder to enable the synchronous commands
// commander.addSynchronousResponder();
commander.addResponder(new orderResponder());
// Configure the ReaderManager when necessary
ReaderManager.create(getApplicationContext());
// Add observers for changes
ReaderManager.sharedInstance().getReaderList().readerAddedEvent().addObserver(mAddedObserver);
ReaderManager.sharedInstance().getReaderList().readerUpdatedEvent().addObserver(mUpdatedObserver);
ReaderManager.sharedInstance().getReaderList().readerRemovedEvent().addObserver(mRemovedObserver);
// Create a (custom) model and configure its commander and handler
mModel = new TriggerModel();
mModel.setCommander(getCommander());
mModel.initialise();
ListView scroller = (ListView)findViewById(R.id.listviewD);
scroller.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
PopupMenu popup = new PopupMenu(orderFullfill.this, view);
popup.setOnMenuItemClickListener(orderFullfill.this);
popup.inflate(R.menu.edit_menu);
where = position;
popup.show();
}
});
weekNow = orderResponder.getWeeknow();
}
public ArrayAdapter getAdapter()
{
adapter = new ArrayAdapter<>(mContext, android.R.layout.simple_spinner_item, scannedPigs);
return adapter;
}
public void addPigNumber()
{
adapter = new ArrayAdapter<String>(mContext, android.R.layout.simple_spinner_item, scannedPigs);
TextView counter = findViewById(R.id.editTextNumber);
if(numberofpigs >= 0 )
{
counter.setText("Pigs Left: " +numberofpigs);
if(numberofpigs == 0)
{
counter.setBackgroundColor(Color.parseColor("#BCBDF3E8"));
}
}
ListView scroller = findViewById(R.id.listviewD);
try {
adapter.setDropDownViewResource(R.layout.support_simple_spinner_dropdown_item);
scroller.setAdapter(adapter);
}catch (Exception e)
{
String result ="";
result.equals(e.toString());
}
}
public void orderdetailsOnclick(View view) {
addPigNumber();
AlertDialog.Builder alert = new AlertDialog.Builder(this);
alert.setTitle("Customer Order: " + customer);
alert.setMessage("Account Number: " + accountnumber + "\r\n" +
"Dispatch Week: " + dispatchweek + "\r\n" +
"Supply Farm: " + supplyfarm + "\r\n" +
"Breed: "+ breed + "\r\n" + "Number of Pigs: " + pigNum);
alert.setCancelable(true);
alert.show();
}
@Override
public synchronized void onResume()
{
super.onResume();
addPigNumber();
// Register to receive notifications from the AsciiCommander
LocalBroadcastManager.getInstance(this).registerReceiver(mMessageReceiver, new IntentFilter(AsciiCommander.STATE_CHANGED_NOTIFICATION));
// Remember if the pause/resume was caused by ReaderManager - this will be cleared when ReaderManager.onResume() is called
boolean readerManagerDidCauseOnPause = ReaderManager.sharedInstance().didCauseOnPause();
// The ReaderManager needs to know about Activity lifecycle changes
ReaderManager.sharedInstance().onResume();
// The Activity may start with a reader already connected (perhaps by another App)
// Update the ReaderList which will add any unknown reader, firing events appropriately
ReaderManager.sharedInstance().updateList();
// Locate a Reader to use when necessary
AutoSelectReader(!readerManagerDidCauseOnPause);
mIsSelectingReader = false;
displayReaderState();
}
@Override
public synchronized void onPause() {
super.onPause();
// Register to receive notifications from the AsciiCommander
LocalBroadcastManager.getInstance(this).unregisterReceiver(mMessageReceiver);
addPigNumber();
ReaderManager.sharedInstance().onPause();
}
@Override
protected void onDestroy()
{
super.onDestroy();
// Remove observers for changes
ReaderManager.sharedInstance().getReaderList().readerAddedEvent().removeObserver(mAddedObserver);
ReaderManager.sharedInstance().getReaderList().readerUpdatedEvent().removeObserver(mUpdatedObserver);
ReaderManager.sharedInstance().getReaderList().readerRemovedEvent().removeObserver(mRemovedObserver);
// readResponder.tempList = false;
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.reader_menu, menu);
// Reset, connect, disconnect dropdown menu
mResetMenuItem = menu.findItem(R.id.reset_reader_menu_item);
mConnectMenuItem = menu.findItem(R.id.connect_reader_menu_item);
mDisconnectMenuItem= menu.findItem(R.id.disconnect_reader_menu_item);
return true;
}
//----------------------------------------------------------------------------------------------
// ReaderList Observers
//----------------------------------------------------------------------------------------------
Observable.Observer<Reader> mAddedObserver = new Observable.Observer<Reader>()
{
@Override
public void update(Observable<? extends Reader> observable, Reader reader)
{
// See if this newly added Reader should be used
AutoSelectReader(true);
}
};
Observable.Observer<Reader> mUpdatedObserver = new Observable.Observer<Reader>()
{
@Override
public void update(Observable<? extends Reader> observable, Reader reader)
{
}
};
Observable.Observer<Reader> mRemovedObserver = new Observable.Observer<Reader>()
{
@Override
public void update(Observable<? extends Reader> observable, Reader reader)
{
// Was the current Reader removed
if( reader == mReader)
{
mReader = null;
// Stop using the old Reader
getCommander().setReader(mReader);
}
}
};
@Override
public boolean onMenuItemClick(MenuItem item) {
switch (item.getItemId())
{
case R.id.delete_menu:
boolean complete = false;
scannedPigs.remove(where);
numberofpigs = numberofpigs + 1;
//myDB.deleteEntry(scannedItems); -> IF YOU ADD IT TO THE DB REMOVE IT
//select items from the list
addPigNumber();
return true;
case R.id.edit_menu_:
return true;
}
return super.onOptionsItemSelected(item);
}}
orderfullfill.xml文件
<?xml version="1.0" encoding="utf-8"?><android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="@+id/editTextNumber"
android:layout_width="177dp"
android:layout_height="56dp"
android:layout_marginStart="24dp"
android:layout_marginLeft="24dp"
android:layout_marginTop="80dp"
android:textSize="30sp"
app:layout_constraintBottom_toTopOf="@+id/listviewD"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="1.0" />
<ListView
android:id="@+id/listviewD"
android:layout_width="354dp"
android:layout_height="221dp"
android:layout_marginBottom="12dp"
app:layout_constraintBottom_toTopOf="@+id/scanM2"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.421"
app:layout_constraintStart_toStartOf="parent" />
<Button
android:id="@+id/invoiceMaker"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="12dp"
android:text="@string/makeInvoice"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.75"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/scanM2" />
<TextView
android:id="@+id/nameView"
android:layout_width="333dp"
android:layout_height="52dp"
android:layout_marginLeft="25dp"
android:layout_marginTop="16dp"
android:layout_marginRight="25dp"
android:layout_marginBottom="303dp"
android:textSize="20dp"
app:layout_constraintBottom_toTopOf="@+id/scanM2"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.0" />
<Button
android:id="@+id/orderdetailsBtn"
android:layout_width="119dp"
android:layout_height="54dp"
android:layout_marginTop="68dp"
android:layout_marginEnd="20dp"
android:layout_marginRight="20dp"
android:onClick="orderdetailsOnclick"
android:text="@string/showOrder"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.887"
app:layout_constraintStart_toEndOf="@+id/editTextNumber"
app:layout_constraintTop_toTopOf="parent" />
<Switch
android:id="@+id/scanM2"
android:layout_width="359dp"
android:layout_height="39dp"
android:layout_below="@id/listviewD"
android:layout_marginTop="232dp"
android:text="@string/highPowerScan"
android:textOff="No"
android:textOn="Yes"
android:textSize="27dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.461"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/orderdetailsBtn" />/android.support.constraint.ConstraintLayout>
java->这是tsl读取器在读取rfid标签时通过ascii进行通信的类。方法拼接器将rfid值添加到arrayadapter,然后notifydatasetchanged();被称为。但是,当从orderfullfill.class外部调用此方法时,不会发生任何事情。
public class orderResponder extends Application implements IAsciiCommandResponder {
public static boolean tempList;
NoteHelper myDB;
public List<String> getReadInput() {
return readInput;
}
public void setReadInput(List<String> readInput) {
this.readInput = readInput;
}
public static List<String> readInput = new ArrayList<>();
@Override
public boolean isResponseFinished() {
return false;
}
@Override
public void clearLastResponse() {
}
@Override
public boolean processReceivedLine(String s, boolean b) throws Exception {
isitMe(s);
return false;
}
public List<String> getList()
{
return readInput;
}
public boolean isitMe (String input)
{
String firstFourChars = "";
//get first 4 chars to see if it contains "EP: "
if(input.length()>4)
{
firstFourChars = input.substring(0,4);
}
else
{
firstFourChars = input;
}
if(firstFourChars.equals("EP: "))// this is the inital identifier for the RFID tags
{
splicer(input);
return true;
}
else
{
return false;
}
}
//takes the last 24 characters removing the 'EP: '
public void splicer (String input)
{ // ArrayAdapter magic = orderFullfill.getAdapter(); THIS Didnt work
orderFullfill.adapter = new ArrayAdapter<String>(orderFullfill.mContext, android.R.layout.simple_spinner_item, orderFullfill.scannedPigs);
String output ="";
output = input.substring(input.length() -24);//maybe take 24 chars
if (orderFullfill.scannedPigs.size() == 0) {
// orderFullfill.scannedPigs.add(output);
orderFullfill.adapter.add(output);
orderFullfill.adapter.notifyDataSetChanged();
orderFullfill.numberofpigs = orderFullfill.numberofpigs - 1;
} else//if the list has values check you are not duplicating the values
{
if (orderFullfill.scannedPigs.contains(output)) {
return;//you are already in the list
} else//you are unique you can join the list
{
// orderFullfill.scannedPigs.add(output);
orderFullfill.adapter.add(output);
orderFullfill.adapter.notifyDataSetChanged();
orderFullfill.numberofpigs = orderFullfill.numberofpigs - 1;
}
}
}
public static String getWeeknow()
{
Date date = new Date();
SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yyyy");
String dateH = formatter.format(date);
//String dateH = date.toString();
String[] array = dateH.split("\\/");
String output ="";
int month = Integer.parseInt(array[1]);
month = month -1;
Calendar calendar = Calendar.getInstance();
calendar.set(Integer.parseInt(array[2]),month,Integer.parseInt(array[0]));
int weekOfyear = calendar.get(Calendar.WEEK_OF_YEAR);
output = String.valueOf(weekOfyear);
return output;
}}
1条答案
按热度按时间w8rqjzmb1#
创建orderresponder构造函数方法。并将活动(orderfullfill.this)和上下文作为参数发送给orderresponder。像这样呼唤;
我希望它能起作用。