Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/186.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/video/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 滚动Listview时,项目将其背景更改为绿色_Android_Listview - Fatal编程技术网

Android 滚动Listview时,项目将其背景更改为绿色

Android 滚动Listview时,项目将其背景更改为绿色,android,listview,Android,Listview,我使用的是nfc标记,当我扫描nfc标记时,标记存储在字符串中的内容,并将字符串与listview内容匹配。如果字符串与listview内容匹配,则listview项的背景颜色变为绿色 我已经编写了将标记内容放入一个字符串的代码,我有listview,其中有一些内容如果标记内容与listview中的内容匹配,它会将背景listview项颜色变为绿色,我们遇到的问题是,即使扫描单个隔间,多个项目也会变成绿色,但当单击“完成”按钮时,只有扫描的隔间会被注册,这只是在我们滚动时发生的 代码如下: pu

我使用的是nfc标记,当我扫描nfc标记时,标记存储在字符串中的内容,并将字符串与listview内容匹配。如果字符串与listview内容匹配,则listview项的背景颜色变为绿色

我已经编写了将标记内容放入一个字符串的代码,我有listview,其中有一些内容如果标记内容与listview中的内容匹配,它会将背景listview项颜色变为绿色,我们遇到的问题是,即使扫描单个隔间,多个项目也会变成绿色,但当单击“完成”按钮时,只有扫描的隔间会被注册,这只是在我们滚动时发生的

代码如下:

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();
}
}

你还得检查其他的情况

您正在为某个条件设置背景色,应该为后一个条件设置另一种颜色

 if(content.equals(tagcontent)){

          textView.setBackgroundColor(getResources().getColor(R.color.DARKGREEN));
}

else{

   textView.setBackgroundColor(getResources().getColor(R.color.WHITE));
}           

在listview或recyclerview中执行选择类型操作时,应记住一件事。你也应该总是写条件的其他部分。然后将值设置为默认值。先生,我也尝试过写其他部分,但没有找到解决我问题的方法。这个解决方案是可行的,但我想要的是,将有多个标签被扫描,所以状态应该被保留,即如果扫描了一个新的标签,以前扫描的标签将变回红色,而当前扫描的标签将变为绿色“想要”是指已变为绿色的项目必须保持绿色。您的意思是说,即使在关闭应用程序并重新打开后仍要恢复状态吗?不,我希望扫描的所有项目都变为绿色,其余项目应保持红色。