使用preferences - 我的列表视图是空的(不能存储数据) [英] using preferences -- my listview is empty (can't store data)
问题描述
我需要为了帮助正确设置preferences。
I need help in order to properly set up preferences.
我有我的主要活动从由pressing菜单按钮,我准备到preferences activity.There,我有3项在用户输入他的data.The第一项是一个序列号
I have my main activity from which by pressing the menu button ,i am going to the preferences activity.There,i have 3 entries where the user inputs his data.The first entry is a serial number.
我希望能够展示所有的序列号列表,当用户选择一个,表明他的其他项目(或做一些计算)。
I want to be able to show a list with all the serial numbers and when the user selects one,show him the other entries (or do some calculations ).
----------已更新------------------------------------
----------UPDATED------------------------------------
我的主要活动是:
public class Strength extends Activity implements OnClickListener{
View goto_list;
SharedPreferences mypref;
String [] values=new String [100];
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//Set up click listeners
goto_list=(View) findViewById(R.id.goto_list);
goto_list.setOnClickListener(this);
//Setup preferences
mypref= PreferenceManager.getDefaultSharedPreferences(this);
SharedPreferences.Editor prefsEditr=mypref.edit();
final Integer counter =values.length;
prefsEditr.putInt("size", counter);
for (int i=0;i<counter;i++) {
prefsEditr.putString("serial_number"+i, values[i]);
}
prefsEditr.putString("date", "");
prefsEditr.putString("strength", "1.0");
prefsEditr.commit();
}
我goto_list活动,这将显示带有序列号列表视图:
My goto_list activity which will show the listview with the serial numbers:
public class goto_list extends ListActivity {
private final String TAG="list";
SharedPreferences mypref;
String[] listItems = null;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.list);
mypref= PreferenceManager.getDefaultSharedPreferences(this);
final Integer counter = mypref.getInt("size",0);
listItems=new String[counter];
for (int i=0;i<counter;i++) {
listItems[i] = mypref.getString("serial_number"+i, "");
}
//what to do with ArrayAdapter?
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1,listItems);
setListAdapter(adapter);
}
protected void onListItemClick(ListView l, View v, int position, long id) {
Log.i(TAG, "position: " + position);
Log.i(TAG, "item: " + listItems[position]);
mypref.edit().putString("serial_number", listItems[position]).commit();
String item = (String) getListAdapter().getItem(position);
Intent i=new Intent(this,calcs_strength.class);
startActivity(i);
finish();
}
所以,我的问题是,列表视图是empty.It不显示数据。
So, my problem is that the listview is empty.It show no data.
感谢您!
推荐答案
您的数组是空的。
更改此:
for (int i=0;i<counter;i++) {
prefsEditr.putString("serial_number"+i, values[i]);
}
与
for (int i=0;i<counter;i++) {
// just replace the string with you serial number;
values[i] = "998-123-234-122_" + i;
prefsEditr.putString("serial_number"+i, values[i]);
}
这篇关于使用preferences - 我的列表视图是空的(不能存储数据)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!