使用首选项——我的列表视图是空的(无法存储数据)
我需要帮助才能正确设置首选项。
我有我的主要活动,通过按菜单按钮,我将进入首选项活动。那里,我有 3 个条目,用户在其中输入他的数据。第一个条目是序列号。
我希望能够显示一个包含所有序列号的列表,当用户选择一个序列号时,向他显示其他条目(或进行一些计算)。
----------更新------------------------------------
我的主要活动是:
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 活动将显示带有序列号的列表视图:
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();
}
所以,我的问题是列表视图是空的。它不显示任何数据。
谢谢你!
I need help in order to properly set up preferences.
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------------------------------------
My main activity is:
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();
}
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();
}
So, my problem is that the listview is empty.It show no data.
Thank you!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
试试这个....
Try this....
1): 在
prefsEditr.putFloat("strength", -1);
中,值“-1”是一个 Integer,因此存储为 Integer。您可以使用 Float.parseFloat 来解析 Integer(例如解析存储的 Integer 值或解析 Integer 值并存储它)。您能详细说明一下2)和3)吗?我不明白你的意思到底。
/edit:一些用于保存/读取数组的示例代码
/edit:还使用
PreferenceActivity
而不是ListActivity
。这让事情变得更容易。然后在
OnCreate()
中添加此代码:然后动态添加 Preference Views 到 Activity,只需调整以下代码:
PreferenceScreen
XML 的内容:1): In
prefsEditr.putFloat("strength", -1);
the value "-1" is an Integer and therefore stored as an Integer. You could useFloat.parseFloat
to parse an Integer (eg parsing the stored Integer value or parsing an Integer value and store it).Can you please elaborate 2) and 3)? I did not get what exactly you meant.
/edit: Some sample code for saving / reading an array
/edit: Also use a
PreferenceActivity
instead of aListActivity
. That makes things easier.Then add this code in
OnCreate()
:and then add dynamically Preference Views to the Activity, just adjust the code below:
Content of the
PreferenceScreen
XML:那么,您似乎可以将序列号传递给您的列表活动,并且它们会显示在列表中,对吧?现在您想知道如何使用适配器?
如果是这样,那么您只需要重写 onListItemClick,从 value[position] 中提取字符串,将其存储回您的首选项中,然后完成列表活动。确保将 String[] 转换为字段而不是本地变量,以便您可以在 onCreate 中填充它并在 onListItemClick 上使用它。
So, it seems like you're able to pass the serial numbers over to your list activity, and they show up in the list, right? And now you're wondering what to do with the adapter?
If so, then you just need to override onListItemClick, pull the string from values[position], store that back into your preferences, and finish() the list activity. Make sure turn that String[] into a field instead of a local var, so that you can fill it in in onCreate and use it on onListItemClick.
你的数组是空的。
改变这个:
与:
your array is empty.
change this:
with: