简体   繁体   English

Android SharedPreferences getString-必需的String,找到了java.lang.String

[英]Android SharedPreferences getString - Required String , Found java.lang.String

I am learning how to develop apps for Android in java. 我正在学习如何使用Java开发适用于Android的应用程序。 I have made my own ArrayAdapter . 我已经制作了自己的ArrayAdapter In its getView method I want to change the string used by a TextView . 我想在其getView方法中更改TextView所使用的字符串。 I want to get this string from the SharedPreferences . 我想从SharedPreferences获取此字符串。

Code is at the bottom of the post. 代码在帖子的底部。

AndroidStudio is telling me there is an error in this line: AndroidStudio告诉我这一行有错误:

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 使用NiceAdapter<String>您可以定义一个称为String的通用类型,这不是SharedPreferences.getString期望的

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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