Android 检索适配器中SharedReference的每个值

Android 检索适配器中SharedReference的每个值,android,listview,sharedpreferences,android-adapter,Android,Listview,Sharedpreferences,Android Adapter,我试图检索存储在SharedReference变量中的每个值。我使用listview来显示SharedReference变量中存储的所有值。这些值是来自internet的URL,指向listview中显示的不同图像。 我想做的是如何通过适配器在listview中显示SharedReference的每个值。Adapter类中getView的代码是: public View getView(int position, View convertView, ViewGroup parent) {

我试图检索存储在SharedReference变量中的每个值。我使用listview来显示SharedReference变量中存储的所有值。这些值是来自internet的URL,指向listview中显示的不同图像。 我想做的是如何通过适配器在listview中显示SharedReference的每个值。Adapter类中getView的代码是:

public View getView(int position, View convertView, ViewGroup parent) {

    // reference to convertView
    View v = convertView;

    // inflate new layout if null
    if(v == null) {
        LayoutInflater inflater = LayoutInflater.from(mContext);
        v = inflater.inflate(R.layout.activity_listado_imagenes_subidas, null);
    }

    // load controls from layout resources
    ImageView imagen = (ImageView)v.findViewById(R.id.imagen);
    Button botonCopiarUrl = (Button)v.findViewById(R.id.btnCopiarImagenUrl);

    SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(mContext);

    // set data to display
    imagen.setImageDrawable(sharedPreferences.getString("url", ""));

    // return view
    return v;
}
使用这段代码,无论listview中有多少项,我始终只获取SharedReferences的第一个值。 你能帮我解决这个问题吗


高级版谢谢。

您从SharedReferences只获得一个值,因为您试图只获得一个值。您可以创建一个循环来获取其他值。

我使用以下代码加密所有存储的字符串,这将提供您所需的一切

SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(MyApp.getAppContext());

    Map<String, ?> all = prefs.getAll();
    String key, value;
    for(Map.Entry<String,?> entry : all.entrySet()) {
        key = entry.getKey();
        if(entry.getValue() instanceof String) {
            value = (String) entry.getValue();

            // encrypt and store again
        }      
    }
SharedReferences prefs=PreferenceManager.GetDefaultSharedReferences(MyApp.getAppContext());
Map all=prefs.getAll();
字符串键,值;
for(Map.Entry:all.entrySet()){
key=entry.getKey();
if(entry.getValue()instanceof String){
value=(字符串)entry.getValue();
//重新加密并存储
}      
}
问候,,
Thomas

您如何存储其他URL?你当然应该告诉我们。您可以使用“url0”、“url1”、“url2”、“url3”。然后用
…getString(“url”+position,”)
检索。我用url存储url,然后存储url值,比如“url”,url值;“url”,url值;“url”,url值;等等,那么您只有一个键值对。只剩下最后一个了。照我的建议去做。很奇怪,你没有对它发表评论。不,我有很多对键,它们都有相同的键“url”。这可能吗?你为什么问<代码>则只有一个键值对。你自己也看到了。啊,也许我误解了你的问题,如果你只有一个“url”偏好,那么重复你所有的偏好是没有意义的。您只能使用脚本为一个键(例如“url”)存储单个值,listview将在每行中显示存储在SharedReference中的每个值项?或者我必须编写一些程序,根据我正在工作的视图的位置,以友好方式检索SharedReference的值吗?我的代码片段将允许您遍历所有首选项。您可以使用prefs.getAll()检索适配器之外的所有首选项,将该映射转换为数组,并使用适配器的位置int访问单个项。。。那么这就是您面临的用例吗?(在列表中显示所有首选项键/值对?)