我正在用字符串填充列表视图,用户可以通过单击按钮添加项目,然后将其保存到SharedPreferences,并在onCreate上加载SharesPreferences字符串。我的问题是它仅加载一个listView项。
这就是我所拥有的
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
preferences = PreferenceManager.getDefaultSharedPreferences(this);
listView = (ListView) findViewById(R.id.lview);
date = new
SimpleDateFormat("dd/MM/yyyy",Locale.getDefault()).format(new Date());
//Load strings from SharedPreferences
try {
isanswer = preferences.getBoolean("dialogOptions", false);
times = preferences.getString("day", null);
humeur = preferences.getString("humour", null);
test = preferences.getString("draw", null);
imgit = getResources().getIdentifier(test, "drawable",
getPackageName());
}catch (Exception e){
//If there is nothing then
isanswer = false;
times = null;
humeur = null;
test = null;
}
// An initial answer it works with a boolen
if (!isanswer) {
dialogOne();
} else {
dialogTwoo();
}
//Defining ArrayLists
imgadt = new ArrayList<Integer>(Arrays.asList(imgit));
listadapt = new ArrayList<String>(Arrays.asList(times));
descradapt= new ArrayList<String>(Arrays.asList(humeur));
adapter = new costomAdapter(this, R.layout.lviewcostom, listadapt,
descradapt, imgadt);
listView.setAdapter(adapter);
adapter.notifyDataSetChanged();
}
//i've tried this to get all strings in ArrayList and put them as ListView Items.
public void onResume(){
super.onResume();
for (int i = 0; i < imgl.size(); i++) {
imgadt = new ArrayList<Integer>(imgl.get(i) + i);
}
for (int i = 0; i < stringList.size(); i++) {
listadapt = new ArrayList<String>(stringList.get(i) + i);
}
for (int i = 0; i < descriptionl.size(); i++) {
descradapt = new ArrayList<String>(descriptionl.get(i) + i);
}
adapter.notifyDataSetChanged();
}
// and this
public void onResume(){
super.onResume();
imgadt = new ArrayList<Integer>(imgl.get(0));
listadapt = new ArrayList<String>(stringList.get(0))
descradapt = new ArrayList<String>(descriptionl.get(0));
adapter.notifyDataSetChanged();
}
// and my ClickListener
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (times == null) {
times = date;
humeur = "Sick";
test = "sick" ;
times = date;
humeur = "Heureux";
test = "happy";
imgadt = new ArrayList<Integer> (Arrays.asList(R.drawable.happy));
listadapt = new ArrayList<String>(Arrays.asList(date));
descradapt= new ArrayList<String>(Arrays.asList("Heureux"));
} else {
imgadt.add(R.drawable.sick);
listadapt.add(date);
descradapt.add("Sick");
times = times + "\n" + date";
humeur = humeur + "\n" + "Sick" ;
test = test + "," + "sick";
}
}
});
添加到共享首选项
okit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
preferences.edit().putString("day", times).commit();
preferences.edit().putString("humour", humeur).commit();
preferences.edit().putString("draw", test).commit();
adapter.notifyDataSetChanged();
if(empty){
adapter = new costomAdapter(Main.this,
R.layout.lviewcostom, listadapt, descradapt, imgadt);
listView.setAdapter(adapter);
}
}
});
通常我是这样的:String[] string = {"New Test", "New Test 2"};
但是我想从共享的首选项中获取此信息,这就是为什么我要这样做:String [] strings = new String [] {times};
有什么办法吗?
编辑:
我自己做到的:D
对于正在搜索它的其他人,这是我所做的:
String str1,
String[] str;
ArrayList<String> listadapt;
Integer allItems;
ListView listView;
Custom Adapter adapter;
和onCreate
//load from SharedPreferences
times = preferences.getString("day", null);
allItems = preferences.getInt("all", 0);
str = times.split(",");
// Définir ListView custom ArrayAdapter
listadapt = new ArrayList<String>();
adapter = new customAdapter(Main.this, listadapt, descradapt, imgadt);
defineThem();
listView.setAdapter(adapter);
adapter.notifyDataSetChanged();
///以str加载所有字符串(时间不包含“,”或“ \ n”或其他) 公共无效define(){
//If there is only one String on str
if (allItems == 1){
str1 = str[0];
listadapt.add(str1);
}
//If there is twoo Strings on str
if (allItems == 2){
str1 = str[0];
str2 = str[1];
//Add items to adapter
listadapt.add(str1);
listadapt.add(str2);
}
如果您想装载很多物品,那么上面的那个不是一个好的解决方案。
如果您想加载很多字符串,我会发现另一种方式:
for(int i = 0 ; i < str.lenght; i++){
listadapt.add(string[0] )
}
可以关闭