使用首选项——我的列表视图是空的(无法存储数据)

发布于 2025-01-03 18:34:24 字数 2425 浏览 0 评论 0原文

我需要帮助才能正确设置首选项。

我有我的主要活动,通过按菜单按钮,我将进入首选项活动。那里,我有 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

夜访吸血鬼 2025-01-10 18:34:24

试试这个....

SharedPreferences mypref= PreferenceManager.getDefaultSharedPreferences(this);
SharedPreferences.Editor prefsEditr=mypref.edit();
prefsEditr.putString("username", username);
prefsEditr.commit();

username = mypref.getString("username", "");

Try this....

SharedPreferences mypref= PreferenceManager.getDefaultSharedPreferences(this);
SharedPreferences.Editor prefsEditr=mypref.edit();
prefsEditr.putString("username", username);
prefsEditr.commit();

username = mypref.getString("username", "");
等风也等你 2025-01-10 18:34:24

1): 在 prefsEditr.putFloat("strength", -1); 中,值“-1”是一个 Integer,因此存储为 Integer。您可以使用 Float.parseFloat 来解析 Integer(例如解析存储的 Integer 值或解析 Integer 值并存储它)。

您能详细说明一下2)和3)吗?我不明白你的意思到底

/edit:一些用于保存/读取数组的示例代码

public String[] readArray() {
    SharedPreferences openPreferences = context.getSharedPreferences("arrayfile", Context.MODE_PRIVATE);
    final Integer counter = openPreferences.getInt("size", 0);
    String[] result = new String[counter];
    for (int i=0;i<counter;i++) {
        result[i] = openPreferences.getString("entry_"+i, "");
    }
    return result;
}

public void writeArray(String[] array) {
    Editor editor = context.getSharedPreferences("arrayfile", Context.MODE_PRIVATE).edit();
    final Integer counter =array.length; 
    editor.putInt("size", counter);
    for (int i=0;i<counter;i++) {
        editor.putString("entry_"+i, array[i]);
    }
    editor.commit();
}

/edit:还使用 PreferenceActivity 而不是 ListActivity。这让事情变得更容易。

然后在 OnCreate() 中添加此代码:

    PreferenceManager localPreferenceManager = getPreferenceManager();
    localPreferenceManager.setSharedPreferencesName("main_prefs");
    localPreferenceManager.setSharedPreferencesMode(MODE_PRIVATE);
            [for every entry do:] {
                addNewPref("ArrayValue"); 
            }

然后动态添加 Preference Views 到 Activity,只需调整以下代码:

 private void addNewPref(String title) {
    Preference newPref = new Preference(this);
    newPref.setTitle(title);
    ((PreferenceScreen) getPreferenceManager().findPreference("category_key")).addItemFromInflater(newPref);
}

PreferenceScreen XML 的内容:

<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen android:key="category_key" xmlns:android="http://schemas.android.com/apk/res/android" >
</PreferenceScreen>

1): In prefsEditr.putFloat("strength", -1); the value "-1" is an Integer and therefore stored as an Integer. You could use Float.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

public String[] readArray() {
    SharedPreferences openPreferences = context.getSharedPreferences("arrayfile", Context.MODE_PRIVATE);
    final Integer counter = openPreferences.getInt("size", 0);
    String[] result = new String[counter];
    for (int i=0;i<counter;i++) {
        result[i] = openPreferences.getString("entry_"+i, "");
    }
    return result;
}

public void writeArray(String[] array) {
    Editor editor = context.getSharedPreferences("arrayfile", Context.MODE_PRIVATE).edit();
    final Integer counter =array.length; 
    editor.putInt("size", counter);
    for (int i=0;i<counter;i++) {
        editor.putString("entry_"+i, array[i]);
    }
    editor.commit();
}

/edit: Also use a PreferenceActivity instead of a ListActivity. That makes things easier.

Then add this code in OnCreate():

    PreferenceManager localPreferenceManager = getPreferenceManager();
    localPreferenceManager.setSharedPreferencesName("main_prefs");
    localPreferenceManager.setSharedPreferencesMode(MODE_PRIVATE);
            [for every entry do:] {
                addNewPref("ArrayValue"); 
            }

and then add dynamically Preference Views to the Activity, just adjust the code below:

 private void addNewPref(String title) {
    Preference newPref = new Preference(this);
    newPref.setTitle(title);
    ((PreferenceScreen) getPreferenceManager().findPreference("category_key")).addItemFromInflater(newPref);
}

Content of the PreferenceScreen XML:

<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen android:key="category_key" xmlns:android="http://schemas.android.com/apk/res/android" >
</PreferenceScreen>
凡尘雨 2025-01-10 18:34:24

那么,您似乎可以将序列号传递给您的列表活动,并且它们会显示在列表中,对吧?现在您想知道如何使用适配器?

如果是这样,那么您只需要重写 onListItemClick,从 value[position] 中提取字符串,将其存储回您的首选项中,然后完成列表活动。确保将 String[] 转换为字段而不是本地变量,以便您可以在 onCreate 中填充它并在 onListItemClick 上使用它。

String[] listItems = null;//fill in in onCreate
SharedPreferences prefs = null;//grab in onCreate

@Override
public void onListItemClick(ListView l, View v, int position, long id)
{
    Log.i(TAG, "position: " + position);
    Log.i(TAG, "item: " + listItems[position]);
    prefs.edit().putString("mySerial", listItems[position]).commit();
}

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.

String[] listItems = null;//fill in in onCreate
SharedPreferences prefs = null;//grab in onCreate

@Override
public void onListItemClick(ListView l, View v, int position, long id)
{
    Log.i(TAG, "position: " + position);
    Log.i(TAG, "item: " + listItems[position]);
    prefs.edit().putString("mySerial", listItems[position]).commit();
}
我也只是我 2025-01-10 18:34:24

你的数组是空的。

改变这个:

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]);
}

your array is empty.

change this:

for (int i=0;i<counter;i++) {
    prefsEditr.putString("serial_number"+i, values[i]);
}

with:

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]);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文