簡體   English   中英

如何在baseAdapter中使用sharedPreferences?

[英]How can I use sharedPreferences in baseAdapter?

我該怎么做?

SharedPreferences spdata = this.getSharedPreferences(
                your file name,
                Context.MODE_PRIVATE);

但編輯器看不到getSharedPreferences ...getDefaultSharedPreferences ..

您需要具有一個Context ,然后具有context.getSharedPreferences() ,您可以通過適配器的構造函數來獲取它。

您的適配器的構造函數:

public MyCustomAdapter(Context context) {
     this.context = context; // assuming you have a local field named context in adapter's class
}

在您的活動的onCreate()東西中:

MyCustomAdapter adapter = new MyCustomAdapter(this);
list.setAdapter(adapter);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM