我遇到了ArrayList的问题。我在一个类中声明了ArrayList。我在OnCreate方法中填充它。在Oncreate方法中,我调用一个函数,在这个函数内部我访问ArrayList,但它是空的....它是starnge:
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();
}
有人可以帮帮我吗? THX
编辑: 抱歉,inList()中的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;
}
答案 0 :(得分:3)
您正在创建2个不同的ArrayLists:
这里(班上当地人):
ArrayList <String> bdd_checked = new ArrayList<String>();
在这里(你的函数onCreate中的本地):
List<String> bdd_checked = readList (this, "animal")
将第二行修改为:
bdd_checked = readList (this, "animal")