我正在处理nfc标记,当我扫描nfc标记时,标记存储在string
中的内容,如果字符串与listview内容匹配,则将string
与listview
内容匹配listview
项目颜色的背景变为绿色。
我已经编写了代码,以便将标记内容添加到我有listview
的字符串中,如果标记内容与Listview
中的内容匹配,则会转换背景listview
。 1}}项目颜色变为绿色,我们得到的问题是,即使正在扫描单个隔间,多个项目也会变为绿色,但是当点击完成button
时,只有正在注册的扫描隔间正好在我们滚动时发生
以下是代码:
public class Main15Activity extends AppCompatActivity {
ListView l1;
TextView t1;
Button b1;
String tagcontent,rname;
Context ctx = this;
Databaseop mydb;
ArrayAdapter<String> listAdapter;
private NfcAdapter nfcAdapter;
ArrayList<String> theList,arrayList;
int flag=0;
Databaseop dp;
int counter;
int count=0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main15);
l1 = (ListView) findViewById(R.id.listview3);
b1 = (Button) findViewById(R.id.button14);
//b2 = (Button) findViewById(R.id.button30);
t1 = (TextView) findViewById(R.id.textView2);
nfcAdapter = NfcAdapter.getDefaultAdapter(this);
if (nfcAdapter == null) {
Toast.makeText(this,
"NFC NOT supported on this devices!",
Toast.LENGTH_LONG).show();
finish();
} else if (!nfcAdapter.isEnabled()) {
Toast.makeText(this,
"NFC NOT Enabled!",
Toast.LENGTH_LONG).show();
startActivity(new Intent(Settings.ACTION_NFC_SETTINGS));
finish();
}
mydb = new Databaseop(Main15Activity.this);
final String rname = getIntent().getStringExtra("rname");
t1.setText(rname);
dp = new Databaseop(ctx);
mydb = new Databaseop(Main15Activity.this);
Cursor data = mydb.getListContents(dp, rname);
if (data.getCount() == 0) {
Toast tos=Toast.makeText(Main15Activity.this, "There are no contents in this list!", Toast.LENGTH_LONG);
tos.setGravity(Gravity.CENTER_VERTICAL, 0, 0);
LinearLayout toastLayout = (LinearLayout) tos.getView();
TextView toastTV = (TextView) toastLayout.getChildAt(0);
toastTV.setTextSize(20);
tos.setGravity(Gravity.CENTER_VERTICAL, 0, 0);
tos.show();;
} else {
while (data.moveToNext()) {
String theListcon = data.getString(0);
Intent intent=getIntent();
//String user=intent.getStringExtra("name");
final String rid=intent.getStringExtra("rid");
final String[] contents = convertStringToArray(theListcon);
for (int j = 0; j <= contents.length - 1; j++) {
String cmp=contents[j];
dp = new Databaseop(ctx);
dp.reports2(dp,rid,cmp);
}
//arrayList=new ArrayList<>(Arrays.asList(contents));
listAdapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,contents){
public View getView(int position , View convertView, ViewGroup parent){
View view=super.getView(position, convertView, parent);
TextView textView=(TextView)view.findViewById(android.R.id.text1);
textView.setTextColor(Color.WHITE);
textView.setTypeface(null, Typeface.BOLD);
//textView.setBackgroundColor(Color.RED);
String content=getItem(position);
if(content.equals(tagcontent))
{
// int dkgrn=006400;
textView.setBackgroundColor(getResources().getColor(R.color.DARKGREEN));
Databaseop dp = new Databaseop(ctx);
dp.repoupdt(dp,tagcontent,rid);
Toast tos1= Toast.makeText(Main15Activity.this,tagcontent+" is Visited",Toast.LENGTH_SHORT);
tos1.setGravity(Gravity.CENTER_VERTICAL, 0, 0);
LinearLayout toastLayout = (LinearLayout) tos1.getView();
TextView toastTV = (TextView) toastLayout.getChildAt(0);
toastTV.setTextSize(20);
tos1.setGravity(Gravity.CENTER_VERTICAL, 0, 0);
tos1.show();
}
//Toast.makeText(Main15Activity.this, "Location not on route", Toast.LENGTH_SHORT).show();
return view;
}
};
l1.setAdapter(listAdapter);
}
}
b1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent=getIntent();
String rid=intent.getStringExtra("rid");
Cursor data1 = dp.getstatcnt(Integer.parseInt(rid));
data1.moveToNext();
final int theListcon = Integer.parseInt(data1.getString(0));
final int j= l1.getAdapter().getCount();
dp.updatereport(dp,rid,theListcon,j);
dp.updateendtime(dp,rid);
new AlertDialog.Builder(Main15Activity.this).setTitle("Warning")
.setMessage("Viseted "+theListcon+" out of "+j+ " Compartments \n Are you sure you want to end rounds?")
.setPositiveButton("yes", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(Main15Activity.this, ""+theListcon+"/"+j,
Toast.LENGTH_LONG).show();
Intent i=new Intent(Main15Activity.this,MainActivity.class);
startActivity(i);
}
}).setNegativeButton("No", null).show();
}
});
}
public static String strSeparator = ", ";
public static String[] convertStringToArray(String str) {
String[] arr = str.split(strSeparator);
return arr;
}
@Override
protected void onNewIntent(Intent intent) {
Tag tag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
if (tag == null) {Toast.makeText(this,"no ndef message",Toast.LENGTH_SHORT).show();
} else {
Parcelable[] parcelables=intent.getParcelableArrayExtra(NfcAdapter.EXTRA_NDEF_MESSAGES);
if(parcelables!=null && parcelables.length>0){
readTextFromTag((NdefMessage)parcelables[0]);
}else{Toast.makeText(this,"no ndef message",Toast.LENGTH_SHORT).show();}
}
super.onNewIntent(intent);
}
private void readTextFromTag(NdefMessage ndefMessage) {
NdefRecord[] ndefRecords=ndefMessage.getRecords();
if(ndefRecords!=null&&ndefRecords.length>0){
NdefRecord ndefRecord=ndefRecords[0];
String tagcontent=gettextfromNdefrecord(ndefRecord);
}else {
Toast.makeText(this,"no ndef message",Toast.LENGTH_SHORT).show();
}
}
@Override
protected void onResume() {
Intent intent = new Intent(this, Main15Activity.class);
intent.addFlags(Intent.FLAG_RECEIVER_REPLACE_PENDING);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, 0);
IntentFilter[] intentFilters = new IntentFilter[]{};
nfcAdapter.enableForegroundDispatch(this, pendingIntent, intentFilters, null);
super.onResume();
}
@Override
protected void onPause() {
nfcAdapter.disableForegroundDispatch(this);
super.onPause();
}
public String gettextfromNdefrecord(NdefRecord ndefRecord) {
try {
byte[] payload = ndefRecord.getPayload();
String textencoding = ((payload[0] & 128) == 0) ? "UTF-8" : "UTF-16";
int languageSize = payload[0] & 0063;
tagcontent = new String(payload, languageSize+1, payload.length - languageSize-1 , textencoding);
listAdapter.notifyDataSetChanged();
} catch (UnsupportedEncodingException e) {
Log.e("gettextfromndefrecord", e.getMessage(), e);
}
return tagcontent;
}
@Override
public void onBackPressed() {
// Simply Do noting!
Toast toast=Toast.makeText(Main15Activity.this,"you cannot go back from here",Toast.LENGTH_LONG);
LinearLayout toastLayout = (LinearLayout) toast.getView();
TextView toastTV = (TextView) toastLayout.getChildAt(0);
toastTV.setTextSize(20);
toast.show();
}
}
答案 0 :(得分:0)
您还必须检查其他情况
您正在设置条件的背景颜色,您应该为后一种情况设置另一种颜色。
if(content.equals(tagcontent)){
textView.setBackgroundColor(getResources().getColor(R.color.DARKGREEN));
}
else{
textView.setBackgroundColor(getResources().getColor(R.color.WHITE));
}