I have the main activity from which by pressing the menu button ,i am going to the preferences activity.Then,i have 3 entries where the user inputs his data.First entry is a serial number.
I want to be able to show a list with all the serial numbers the user has entered and when the user selects one,show him the other entries .
My problem until now is that the list is empty.
The main activity:
- Code: Select all
public class Strength extends Activity implements OnClickListener{
View goto_list;
SharedPreferences mypref;
String [] values=new String [100]; //this will hold the values for serial_numbers
/** 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();
}
the goto_list activity:
- Code: Select all
public class goto_list extends ListActivity {
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, "");
}
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();


