empty listview from data with preferences

Put your problem here if it does not fit any of the other categories.

Re: empty listview from data with preferences

Postby Phyll » Fri Feb 24, 2012 1:07 pm

Hi gpolis,

If you do that, it will work. You can always send it to me and I'll make it work. But it will work.

It will load each serial number that is entered into the preferences with the next sequential number appended to the word "serial" just like your for loop. The only difference is, it will work. When you look at the list, it will only have the serial numbers entered so far. To get the last one, retrieve the size and if its greater than 0 get the next to last one (size-1). If you need a particular one, select it from the list.

If you need to store the date and strength, just add them to the serial string and store the whole thing in the "serial"+n preference.

It will work.

Phyll
Phyll
Master Developer
Master Developer
 
Posts: 648
Joined: Fri Oct 14, 2011 11:19 am

Top

Re: empty listview from data with preferences

Postby gpolis » Fri Feb 24, 2012 1:22 pm

Hello Phyll,i send you a message.
I insist :) on that ,because you call getInt where i save data.Anyway , i don't know more than you .:)

Thanks a lot!
gpolis
Developer
Developer
 
Posts: 38
Joined: Wed Feb 15, 2012 6:28 pm

Re: empty listview from data with preferences

Postby Phyll » Fri Feb 24, 2012 3:09 pm

Hi gpolis,

Now that I see the whole thing, I see that I should have made this line:

int size = prefsEditr.getInt("size",0);

Like this:

int size = mypref.getInt("size",0);

That makes the whole thing like this:

Code: Select all
                 SharedPreferences mypref;
                  String [] values=new String [100];
                 //Setup preferences
                 mypref= PreferenceManager.getDefaultSharedPreferences(this);
                 SharedPreferences.Editor prefsEditr=mypref.edit();
                
                 int size = mypref.getInt("size",0);
                 prefsEditr.putString("serial"+size, serial);
                 size++;
                 prefsEditr.putInt("size", size);

                 prefsEditr.putString("date", "");
                 prefsEditr.putString("strength", "1.0");
                 prefsEditr.commit();   


Then you get this when you run it:

Image

Hope this helps.

Be sure and get rid of the old list in the preferences by uninstalling the previous version.

Phyll
Phyll
Master Developer
Master Developer
 
Posts: 648
Joined: Fri Oct 14, 2011 11:19 am

Re: empty listview from data with preferences

Postby gpolis » Fri Feb 24, 2012 3:33 pm

Phyll,thanks a lot!

I have one final question. In the List_hold activity i have :

Code: Select all
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", listItems[position]).commit();
      
      
       mypref= PreferenceManager.getDefaultSharedPreferences(this);
       String date=mypref.getString("date", "");
       mypref.edit().putString("date","").commit();
      
       date = (String) getListAdapter().getItem(position);
      //String item = (String) getListAdapter().getItem(position);
      Intent i=new Intent(this,calcs_strength.class);
      i.putExtra("value",date);
      startActivity(i);
      finish();


where i try to pass ,for example "date" and show it (or do calculations) at the calcs_strenght activity.
Until now , i can only pass the name of the "serial".
I have commended the lines that concern the "serial" (which as i say works).
I tried to make it work with "date" but with no success.
gpolis
Developer
Developer
 
Posts: 38
Joined: Wed Feb 15, 2012 6:28 pm

Re: empty listview from data with preferences

Postby Phyll » Fri Feb 24, 2012 5:14 pm

Hi gpolis,

I think you should pass these values to each Actiivty using the intents like this:

The list_hold activity:
Code: Select all
      String item = (String) getListAdapter().getItem(position);
      int a=2;
      Intent i=new Intent(this,calcs_strength.class);
      i.putExtra("serial", listItems[position]);
      i.putExtra("value",a);
      startActivity(i);
      finish();


The calc_strength activity:
Code: Select all
        result=(TextView) findViewById(R.id.result);
        Intent i = getIntent();
        String serial = i.getStringExtra("serial");
        int a = i.getIntExtra("value",2);
        result.setText("Remaining strength : "+a);


It seems like you are trying to use the preferences as global variables.

Make the exit from list_hold activity like this:
Code: Select all
        //set the value of a
        int a=2;
        Intent i=new Intent(this,calcs_strength.class);
      
        //send the position and a value in the intent
        i.putExtra("position", position);
        i.putExtra("value",a);
        startActivity(i);
        finish();


Then decode it in the calc_strength activity:

Code: Select all
        SharedPreferences mypref= PreferenceManager.getDefaultSharedPreferences(this);
        result=(TextView) findViewById(R.id.result);
       
        //get this intent that has the position in the list and the a value
        Intent i = getIntent();
       
        //use the position to get the serial number from the preferences
        String serial = mypref.getString("serial"+i.getIntExtra("serial",0),"");
       
        //just get the data and strength that would already be stored
        //there from the dialog
        String date = mypref.getString("date", "");
        String strength = mypref.getString("strength","");
       
        //get the a value passed in the intent
        int a = i.getIntExtra("value",2);
        result.setText("Remaining strength : "+a);


Hope this helps.

Phyll
Phyll
Master Developer
Master Developer
 
Posts: 648
Joined: Fri Oct 14, 2011 11:19 am

Re: empty listview from data with preferences

Postby gpolis » Fri Feb 24, 2012 7:22 pm

Hello Phyll,
the problem is that when i select a serial number from the list ,i want int the calcs_strength activity to show me the date and strenght that corresponds to that serial number.

(The int a=2 ,i put it just for testing)

I hope i am understandable.

Thanks again.
gpolis
Developer
Developer
 
Posts: 38
Joined: Wed Feb 15, 2012 6:28 pm

Top

Re: empty listview from data with preferences

Postby Phyll » Fri Feb 24, 2012 9:47 pm

Hi gpolis,

I couldn't get on the board for a while. Some kind of mySQL error or something.

Anyway, I think you should make them all part of one string when you save the serial number, save the date and strength also.

Save them as strings and turn the strength back into a number when you use it.

Like this:

Code: Select all
                 String serial = data.getStringExtra("serial");
                 String date = data.getStringExtra("date");
                 String strength = data.getStringExtra("strength");
                
                 Log.i("RETURNED",serial+" "+date+" "+strength);

                 SharedPreferences mypref = PreferenceManager.getDefaultSharedPreferences(this);
                 SharedPreferences.Editor prefsEditr=mypref.edit();
                
                 int size = mypref.getInt("size",0);
                 prefsEditr.putString("serial"+size, serial+","+date+","+strength);
                 size++;
                 prefsEditr.putInt("size", size);


Now when you get it back in the list, it will show up like serial,date,strength maybe 12345,02/24/2012,2.

To use it, you pass the whole thing to the strength activity in the intent and it takes the strength off the end and uses it. You could do it like this:

Code: Select all
              Intent i = getIntent();
              String serial = i.getStringExtra("serial");
              String[] parts = serial.split(",");
              serial = parts[0]; //just show this
              String date = parts[1];// do whatever you do with the date string
              float strength = Float.parseFloat(parts[2]);//convert this to number


or something like that.

Hope this helps.

Phyll
Phyll
Master Developer
Master Developer
 
Posts: 648
Joined: Fri Oct 14, 2011 11:19 am

Re: empty listview from data with preferences

Postby gpolis » Sat Feb 25, 2012 12:44 pm

Hello Phyll,

you showed me what to put in the Radioactivity_list and in calcs_strength activitites but i can't manage to run it because i am not sure what to put in the onListItemClick in the List_hold activity.How to handle the fields ,serial ,date and strength there.

Thanks!
gpolis
Developer
Developer
 
Posts: 38
Joined: Wed Feb 15, 2012 6:28 pm

Re: empty listview from data with preferences

Postby Phyll » Sat Feb 25, 2012 12:59 pm

Hi gpolis,

Well, going back to one of the previous examples you could do it this way in the list activity:

Code: Select all
        Intent i=new Intent(this,calcs_strength.class);
     
        //send the position in the intent
        i.putExtra("position", position);
        startActivity(i);
        finish();


Then pick them up in the calcs activity like this:

Code: Select all
        SharedPreferences mypref= PreferenceManager.getDefaultSharedPreferences(this);
        result=(TextView) findViewById(R.id.result);
       
        //get this intent that has the position in the list and the a value
        Intent i = getIntent();
       
        //use the position to get the serial number from the preferences
        String serial = mypref.getString("serial"+i.getIntExtra("serial",0),"");

        //use it something like this
        String[] parts = serial.split(",");
        serial = parts[0]; //just show this
        String date = parts[1];// do whatever you do with the date string
        float strength = Float.parseFloat(parts[2]);//convert this to number


Hope this helps.

Phyll
Phyll
Master Developer
Master Developer
 
Posts: 648
Joined: Fri Oct 14, 2011 11:19 am

Re: empty listview from data with preferences

Postby gpolis » Sat Feb 25, 2012 1:14 pm

Hello Phyll,

Now it works ,but when i add for example " result.setText("Remaining strength : "+strength);" in the calcs_strength ,it gives me always the strength that corresponds for the first input(item) in the list.

Doesn't suppose to give me the strength for the item of the list i select?

Maybe i must use somehow the "getListAdapter().getItem(position);" command?
gpolis
Developer
Developer
 
Posts: 38
Joined: Wed Feb 15, 2012 6:28 pm

Re: empty listview from data with preferences

Postby Phyll » Sat Feb 25, 2012 5:52 pm

Hi gpolis,

In the previous post, I had this line in the list activity:

i.putExtra("position", position);

It sends the position in the intent to the calc activity and gets the value stored for that serial in this line:

String serial = mypref.getString("serial"+i.getIntExtra("serial",0),"");

[EDIT]
The above line should be:

String serial = mypref.getString("serial"+i.getIntExtra("position",0),"");


After this line, serial should contain the three strings from that position when they are separated with split. Then parts[2] should have the strength string and be converted in this line:

float strength = Float.parseFloat(parts[2]);

I didn't know if it was supposed to be float or not but that's what I made it.

Yes, then it should be whatever you put in there when it was stored with serial and date. As long as you ask preferences for the "serial"+position string you will get that value.

The list adapter is only instantiated in the list activity, that is why we have to pass it to the next one and use it as a reference into the stored preference items.

Hope this helps.

Phyll
Last edited by Phyll on Sat Feb 25, 2012 7:04 pm, edited 1 time in total.
Phyll
Master Developer
Master Developer
 
Posts: 648
Joined: Fri Oct 14, 2011 11:19 am

Re: empty listview from data with preferences

Postby gpolis » Sat Feb 25, 2012 6:30 pm

Hello Phyll ,

if i understand well, you are trying to tell me that for strength for example i use parts[2] ,etc.

But my problem is this:
phyll1.png
Here is my list (with data for no1 , 1,1 and data for no2 2,2
phyll1.png (5.68 KiB) Viewed 188 times


phyll1.png
Here is my list (with data for no1 , 1,1 and data for no2 2,2
phyll1.png (5.68 KiB) Viewed 188 times






Thanks!
Attachments
phyll2.png
Here ,it supposed to give me :Remaining strength :2
phyll2.png (7.3 KiB) Viewed 188 times
gpolis
Developer
Developer
 
Posts: 38
Joined: Wed Feb 15, 2012 6:28 pm

Re: empty listview from data with preferences

Postby Phyll » Sat Feb 25, 2012 7:01 pm

Sorry gpolis,

Change this line to this:

String serial = mypref.getString("serial"+i.getIntExtra("position",0),"");

Trying to do too many things at once I guess.

Thanks,

Phyll
Phyll
Master Developer
Master Developer
 
Posts: 648
Joined: Fri Oct 14, 2011 11:19 am

Re: empty listview from data with preferences

Postby gpolis » Sat Feb 25, 2012 7:23 pm

Perfect!

Thanks a lot Phyll,you are most helpful.

Really,i appreciate it.
gpolis
Developer
Developer
 
Posts: 38
Joined: Wed Feb 15, 2012 6:28 pm

Top
Previous

Return to Other Coding-Problems

Who is online

Users browsing this forum: No registered users and 20 guests