简体   繁体   中英

Android SharedPreferences getString - Required String , Found java.lang.String

I am learning how to develop apps for Android in java. I have made my own ArrayAdapter . In its getView method I want to change the string used by a TextView . I want to get this string from the SharedPreferences .

Code is at the bottom of the post.

AndroidStudio is telling me there is an error in this line:

String description = prefs.getString(title + "description", "missing description");

Incompatible types. Required: String, Found: java.lang.String

Far as I know these things are the same thing. But I cannot run my program because of this error. How do I get rid of it?


public class NiceAdapter<String> extends ArrayAdapter<String> {

    private SharedPreferences prefs;
    private final SharedPreferences.Editor editor;
    private ArrayList<String> items;

    public NiceAdapter(Context context, int resource, int textViewResourceId, ArrayList<String> passedItems) {
        super(context, resource, textViewResourceId, passedItems);

        items = passedItems;
        prefs = context.getSharedPreferences("NiceListPrefs", ListActivity.MODE_PRIVATE);
        editor = prefs.edit();
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        convertView = super.getView(position,convertView,parent);

        TextView titleView = (TextView)convertView.findViewById(R.id.title);
        TextView descriptionView = (TextView)convertView.findViewById(R.id.description);

        String title = items.get(position);
        titleView.setText(title.toString());

        String description = prefs.getString(title + "description", "missing description");
        descriptionView.setText(description);

        return convertView;
    }
}

Change

public class NiceAdapter<String> extends ArrayAdapter<String> {

with

public class NiceAdapter extends ArrayAdapter<String> {

With NiceAdapter<String> you are defining a generic type called String, which is not what SharedPreferences.getString expects

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM