我有一个 ArrayList 的问题。我在一个类中声明了 ArrayList。我在 OnCreate 方法中填充它。在 Oncreate 方法上,我调用了一个函数,在这个函数内部,我访问了 ArrayList,但它是空的....
public class List_themeActivity extends Activity{
String[] array ;
ArrayList <String>bdd_checked = new ArrayList<String>();
private SQLiteDatabase bdd;
private ListView listView_listebase;
ListView msgList;
ArrayList<MessageDetails> details;
AdapterView.AdapterContextMenuInfo info;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.list_theme);
List<String> bdd_checked = readList (this, "animal");
initDB("LSPCC4.db");
[...]
}
}
protected void initDB(String BD_NAME){
int lg_bdd_checked=0;
lg_bdd_checked=this.bdd_checked.size();
}
有人可以帮助我吗?谢谢
编辑:抱歉,iniDB() 中的 ArrayList 为空。
感谢maqjav,问题解决了,我做到了:
前:
List<String> bdd_checked = readList (this, "animal");
后:
bdd_checked = (ArrayList<String>) readList (this, "animal");
由于readList(),我已经投了:
public static List readList (Context context, String prefix) { SharedPreferences prefs = context.getSharedPreferences("YourApp", Context.MODE_PRIVATE);
int size = prefs.getInt(prefix+"_size", 0);
List<String> data = new ArrayList<String>(size);
for(int i=0; i<size; i++)
data.add(prefs.getString(prefix+"_"+i, null));
return data;
}