大家好我正在使用自定义适配器来填充复选框的ListView。主要活动是片段(不是活动)。 每个复选框都有名称和ID,例如Shells - 12345.我要做的是当屏幕加载并且用户扫描RFID标签时,它会读取标签ID,例如12345,如果他们的ID,则选中复选框火柴。 我怎样才能做到这一点?
感谢任何帮助
编辑: 我在我的登录部分使用了nfc,如下所示:
public class loginPage extends AppCompatActivity {
DateFormat time = new SimpleDateFormat("h:mm a");
DateFormat date = new SimpleDateFormat("yyyy.MM.dd ");
NfcAdapter mAdapter;
PendingIntent pendingIntent;
private String serialId = "";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login_page);
mAdapter = NfcAdapter.getDefaultAdapter(this);
pendingIntent = PendingIntent.getActivity(
this, 0, new Intent(this, getClass()).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), 0);
if (mAdapter == null) {
Toast.makeText(this,
"NFC NOT supported on this devices!",
Toast.LENGTH_LONG).show();
finish();
} else if (!mAdapter.isEnabled()) {
Toast.makeText(this,
"NFC NOT Enabled!",
Toast.LENGTH_LONG).show();
finish();
}
//buttonbypass
Button toMain = (Button) findViewById(R.id.but_bypass);
toMain.setOnClickListener(new View.OnClickListener(){
public void onClick(View v){
Intent myIntent = new Intent(loginPage.this,MainActivity.class);
startActivity(myIntent);
}
});
}
@Override
public void onResume() {
super.onResume();
mAdapter.enableForegroundDispatch(this, pendingIntent, null, null);
}
@Override
public void onPause(){
super.onPause();
mAdapter.disableForegroundDispatch(this);
}
@Override
public void onNewIntent(Intent intent) {
String action = intent.getAction();
if (NfcAdapter.ACTION_TAG_DISCOVERED.equals(action) ||
NfcAdapter.ACTION_TECH_DISCOVERED.equals(action)) {
Tag tag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
if (tag != null) {
((GlobalData) getApplication()).setUserTag(tag);
String serialID = ((GlobalData) this.getApplication()).getUserID();
if(serialID.equalsIgnoreCase("04950f4ae53f80")){
String jobPath = "USERS/04950F4AE53F80/LOGTIME/";
final DatabaseReference myRef = FirebaseDatabase.getInstance().getReference(jobPath);
// LogClass logClass = new LogClass(date.format(Calendar.getInstance().getTime()), time.format(Calendar.getInstance().getTime()),"");
// myRef.child(date.format(Calendar.getInstance().getTime())).setValue(logClass);
Intent goToConfirm = new Intent(this, confirmLogin.class);
startActivity(goToConfirm);
}else {
Toast.makeText(this,"This is not an employee!", Toast.LENGTH_SHORT).show();
}
}
}
}
}
我试图在我的适配器中实现这一点,如下所示:
public class MyCustomAdapter extends ArrayAdapter<JobOverviewResourceClass> {
private ArrayList<JobOverviewResourceClass> JobResourcesclass;
//PendingIntent pendingIntent = PendingIntent.getActivity(getContext(), 0, new Intent(getContext(), getClass()).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), 0);
NfcAdapter mAdapter;
PendingIntent pendingIntent;
int i = 1;
String resourceID;
public MyCustomAdapter(Context context, int textViewResourceId, ArrayList<JobOverviewResourceClass> JobResourcesclass) {
super(context, textViewResourceId, JobResourcesclass);
this.JobResourcesclass = new ArrayList<>();
this.JobResourcesclass.addAll(JobResourcesclass);
}
private class ViewHolder {
TextView code;
TextView amnt;
CheckBox name;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
Log.v("ConvertView", String.valueOf(position));
if (convertView == null) {
LayoutInflater vi = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = vi.inflate(R.layout.job_overview_resource_checks, null);
holder = new ViewHolder();
holder.name = (CheckBox) convertView.findViewById(R.id.Resource_checkBox);
holder.code = (TextView) convertView.findViewById(R.id.Resource_checkBox_Text);
holder.amnt = (TextView) convertView.findViewById(R.id.Resource_checkBox_amount_Text);
convertView.setTag(holder);
holder.name.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
CheckBox cb = (CheckBox) v;
JobOverviewResourceClass resource = (JobOverviewResourceClass) cb.getTag();
Toast.makeText(getContext(),
"Clicked on Checkbox: " + cb.getText() +
" is " + cb.isChecked(),
Toast.LENGTH_LONG).show();
final Integer neededquant = resource.getmRESQUANTITY();
final String shelfresname = resource.getShelfresneeded();
//final Integer shelfquant =;
final DatabaseReference ref = FirebaseDatabase.getInstance().getReference("RESOURCES/INVENTORY");
ref.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
for (DataSnapshot data : dataSnapshot.getChildren()
) {
ResourceInventoryClass rcs = data.getValue(ResourceInventoryClass.class);
String key = data.getKey();
resourceID = rcs.ID;
nfcStuff();
if (i == 0) {
if (rcs.NAME.equals(shelfresname)) {
final Integer shelfquant = rcs.getmQUANTITY();
i++;
final Integer NewShelfQuantity = shelfquant - neededquant;
ref.child(key).child("QUANTITY").setValue(NewShelfQuantity);
break;
}
}
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
Log.e("Chat", "The read failed: " + databaseError.getDetails());
}
});
resource.setSelected(cb.isChecked());
//END OF ONCLICK ACTIVITY
}
});
} else {
holder = (ViewHolder) convertView.getTag();
}
JobOverviewResourceClass resource = JobResourcesclass.get(position);
holder.code.setText(resource.getmName());
String xx = Integer.toString(resource.getmRESQUANTITY());
holder.amnt.setText(" x " + xx);
holder.name.setChecked(resource.isSelected());
holder.name.setTag(resource);
return convertView;
}
public void nfcStuff() {
mAdapter = NfcAdapter.getDefaultAdapter(getContext());
pendingIntent = PendingIntent.getActivity(
getContext(), 0, new Intent(getContext(), getClass()).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), 0);
}
public void onNewIntent(Intent intent) {
String action = intent.getAction();
if (NfcAdapter.ACTION_TAG_DISCOVERED.equals(action) ||
NfcAdapter.ACTION_TECH_DISCOVERED.equals(action)) {
Tag tag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
if (tag != null) {
String serialID = ((GlobalData) this.getContext()).getUserID();
if (serialID.equalsIgnoreCase(resourceID)) {
i = 0;
} else {
Toast.makeText(getContext(), "This is not a resource!", Toast.LENGTH_SHORT).show();
}
}
}
}
}
这不起作用,因为当我扫描标签时它会显示apps列表并且不会选中该框