0

here is the code:

    @Override
public void onBindViewHolder(@NonNull final WorkerViewHolder holder, int position) {
    final Worker currentWorker = mWorkerList.get(position);
    holder.textView_worker_name.setText(currentWorker.getName());//sets each worker in the Recycler
    holder.btnWorking.setVisibility(View.INVISIBLE);

    Log.d("myappCurrent",currentWorker.getName() + wWorkerList.get(0).getWorkerName());


    for(AtWorkWorker worker: wWorkerList){

        if(currentWorker.getName() == worker.getWorkerName()){

            holder.btnWorking.setText("Working on "+ worker.gettask());
            holder.btnWorking.setVisibility(View.VISIBLE);
            Log.d("myapp", worker.gettask());

        }
    } //iterates over wworkerlist(obj array) and matches position var with the corresponding position in AtWorkWorkers list

}

the log.d just before the for loop prints out this:

2020-09-13 15:35:17.307 22092-22092/com.example.sheetmetalroofersaudit D/myappCurrent: TrevorTrevor
2020-09-13 15:35:17.322 22092-22092/com.example.sheetmetalroofersaudit D/myappCurrent: CandaceTrevor
2020-09-13 15:35:17.336 22092-22092/com.example.sheetmetalroofersaudit D/myappCurrent: Kevin soareTrevor
2020-09-13 15:35:17.348 22092-22092/com.example.sheetmetalroofersaudit D/myappCurrent: akins smithTrevor

but the if statement never triggers when Trevor and Trevor line up.

The wWorkerlist comes from sharedpreferences so could there be a delay in getting the values and the if statement is run through before its triggered?

Trevor Soare
  • 120
  • 8
  • This is not good: `if(currentWorker.getName() == worker.getWorkerName()){`. Don't compare Strings using `==` or `!=`. Use the `equals(...)` or the `equalsIgnoreCase(...)` method instead. Understand that `==` checks if the two *object references* are the same which is not what you're interested in. The methods on the other hand check if the two Strings have the same characters in the same order, and that's what matters here. – Hovercraft Full Of Eels Sep 13 '20 at 23:07
  • Better to do something like `if(currentWorker.getName().equals(worker.getWorkerName())) {` instead – Hovercraft Full Of Eels Sep 13 '20 at 23:07
  • Awesome thank you. That solved everything – Trevor Soare Sep 14 '20 at 01:44
  • @TrevorSoare People told you what to do, but not the rule to remember **When using objects compare with `.equals(...)`; when using primitives, compare with `==`** – Edwin Buck Sep 14 '20 at 02:25

0 Answers0