Prabu Guna Prabu Guna - 4 years ago 451
Java Question

No suitable method found for add (Set<String>)

Here the

MainActivity.java
file. I'm getting error in
notes.add(set);


package inandroid.jeetna.appnotes;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.Set;

public class MainActivity extends AppCompatActivity {

static ArrayList<String> notes = new ArrayList<>();
static ArrayAdapter arrayAdapter;
static Set<String> set;


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);

FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});

ListView listView = (ListView) findViewById(R.id.listView);

SharedPreferences sharedPreferences = this.getSharedPreferences("inandroid.jeetna" +
".appnotes", Context.MODE_PRIVATE);

set = sharedPreferences.getStringSet("notes", null);

notes.clear();

if(set!=null){
notes.add(set);
}else{
notes.add("Testing data default note");
set = new HashSet<String>();
set.addAll(notes);
sharedPreferences.edit().putStringSet("notes", set).apply();
}

arrayAdapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1,
notes);
listView.setAdapter(arrayAdapter);

listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
Intent in = new Intent(getApplicationContext(), EditYourNote.class);
in.putExtra("noteId", i);
startActivity(in);
}
});




}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();

//noinspection SimplifiableIfStatement
if (id == R.id.add) {
return true;
}

return super.onOptionsItemSelected(item);
}
}


Here's the Error message is:

Error:(55, 18) error: no suitable method found for add(Set<String>)
method ArrayList.add(int,String) is not applicable
(actual and formal argument lists differ in length)
method ArrayList.add(String) is not applicable
(actual argument Set<String> cannot be converted to String by method invocation conversion)
method AbstractList.add(String) is not applicable
(actual argument Set<String> cannot be converted to String by method invocation conversion)
method AbstractList.add(int,String) is not applicable
(actual and formal argument lists differ in length)
method AbstractCollection.add(String) is not applicable
(actual argument Set<String> cannot be converted to String by method invocation conversion)

Answer Source

use notes.addAll(set) instead of notes.add(set). This method is for collections appending.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download