using preferences — my listview is empty (can't store data)

[亡魂溺海] 提交于 2019-12-11 06:56:55

问题


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!


回答1:


Try this....

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

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



回答2:


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>



回答3:


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



回答4:


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


来源:https://stackoverflow.com/questions/9209673/using-preferences-my-listview-is-empty-cant-store-data

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!