empty listview from data with preferences

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

empty listview from data with preferences

Postby gpolis » Tue Feb 21, 2012 1:25 pm

Hello,

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();
gpolis
Developer
Developer
 
Posts: 38
Joined: Wed Feb 15, 2012 6:28 pm

Top

Re: empty listview from data with preferences

Postby Phyll » Tue Feb 21, 2012 10:49 pm

Hi gpolis,

It seems like you don't have a way to put them into the preferences. Is that right? If this is how they are selected, where are they entered so that they can show up in this list?

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 » Wed Feb 22, 2012 12:09 pm

Hi Phyll
I use menu in the main activity and from there go to preferences activity where i have the entries.
My preferences activity is empty.My preferences.xml has the entries (serial_number,date,strenght)

Do you say i must load the array in the preferences activity?And then pass it to the goto_list activity?Because i tried it and it didn't work.

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

Re: empty listview from data with preferences

Postby Phyll » Wed Feb 22, 2012 12:32 pm

Hi gpolis,

Just because "it didn't work" doesn't mean you don't have to do it.

If I'm understanding your question, it is, how do I enter these 3 items, serial number choice from a list, date and strength, and return them to my application. Must also apparently allow entering a new serial number to add to the list of choices.

Unfortunately Android doesn't have a combobox so to speak. We have to make one and apply some logic to entering a new value to the list or using a value from the list.

I don't know about the date. Is that the current date? Could we fill it in for the user?

I don't know what strength is. A number? A phrase?

Is that the jist of it?

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 » Wed Feb 22, 2012 12:59 pm

No Phyll ,maybe i haven't explained it good.

This is my strength.java activity where ,when i press the menu button ,using preferences,i go to the preferences menu where i have 3 entries.
Serial_number (string) , date (string) and strength(float).
I am entering the data (the above 3 entries).


Code: Select all
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);
     
        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++) {
           values[i] = "serial_number"+i;
            prefsEditr.putString("serial_number"+i, values[i]);
        }

        prefsEditr.putString("date", "");
        prefsEditr.putString("strength", "1.0");
        prefsEditr.commit();   
    }
   
  //called when a button is clicked
    public void onClick(View v) {
       switch (v.getId()){
       case R.id.goto_list:
          Intent k=new Intent(this,goto_list.class);
          startActivity(k); 
          break;
}
    }
// Called first time user clicks on the menu button
    public boolean onCreateOptionsMenu(Menu menu) {
      MenuInflater inflater = getMenuInflater();
      inflater.inflate(R.menu.menu, menu);
      return true;
    }

    // Called when an options item is clicked
    public boolean onOptionsItemSelected(MenuItem item) {
      switch (item.getItemId()) {
      case R.id.itemPrefs:
        startActivity(new Intent(this, PrefsActivity.class));
        break;
      }

      return true;
    }
   

}


Then , i want to show in a list all the serial numbers that the user has entered.That's why i load the string array (which holds the serila numbers) in the goto_list activity:

Code: Select all
public class goto_list extends ListActivity {
   
   SharedPreferences mypref=null;
   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);         
   }
      
}


The preferences activity is :
Code: Select all
public class PrefsActivity extends PreferenceActivity  {
   
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    addPreferencesFromResource(R.xml.prefs); 
}
}


I hope i made my self clear now.

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

Re: empty listview from data with preferences

Postby Phyll » Wed Feb 22, 2012 2:49 pm

Hi gpolis,

I see this:

Code: Select all
        final Integer counter =values.length;
        prefsEditr.putInt("size", counter);
        for (int i=0;i<counter;i++) {
           values[i] = "serial_number"+i;
            prefsEditr.putString("serial_number"+i, values[i]);
        }


that makes a list of numbers 0 to 99 or something. But these aren't the serial numbers that have been entered are they?

It looks like you want to use the preferences editor as a form to get these values. Is that right? How do you know when the user is done with the preferences editor?

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 » Wed Feb 22, 2012 3:12 pm

But these aren't the serial numbers that have been entered are they?


No,it supposes to be.

It looks like you want to use the preferences editor as a form to get these values. Is that right?


Yes.When i press the menu button , appear the 3 fields where i enter the data.

How do you know when the user is done with the preferences editor?


I enter data in the same screen as many times as i want and then press the back button to finish.

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

Re: empty listview from data with preferences

Postby Phyll » Wed Feb 22, 2012 10:42 pm

Hi gpolis,

But don't you end up with whatever data you entered last?

When you return, you don't get what you entered passed back or anything and you don't even know you're back do you? How do you know they entered anything? I didn't think the preferences had any way to extend a list. Just select from one.

Like I said, I think its more of a job for a dedicated dialog activity to get your input and pass back a result. Then you can use the input and add the serial number to your list if I'm following your flow here. What is that list for? Is that so you don't use the same serial number over again?

Its a little bit more complicated but I'm not sure you can make what you are doing work or work very well. I can show you what I think you need but I don't have anything ready at the moment. Let me know what you think about 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 » Thu Feb 23, 2012 11:38 am

Hello Phyll,

I am using preferences in order to save some data.
Then, that data ( only the serial number) i want to put them in a list and from there according to what the user selects ,show him some information about that (every serial number has its own date and strenght) or do some calculations.

As far as i understand this is applicable with preferences (to store data).

If i understand well your questions , the list doesn't have to do anything with the preferences screen (where i have the 3 entries).The list is another activity where i am showing the data (serial number) from the preferences.

Sorry if i can't make my self clear.

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

Re: empty listview from data with preferences

Postby Phyll » Thu Feb 23, 2012 11:50 am

Hi gpolis,

Well we're both having that problem I think.

The preferences that is built into Android allows you to store some values (in the file system really) but its usefullness as an entry dialog is questionable. It is intended to set long term values. Things that do not change often and represent long term settings for the app, usually.

For one thing, using startactivity will return to the current activity but does not return a result and does not inform you that the user is done entering. It would be better to create an entry dialog for these values and then you can store them in the preferences, add them to a list or whatever you intended to do with them. The preferences is just not suited for this.

I guess that is why I have tried and I am trying to steer you in another direction for this input. I can help you with 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 » Thu Feb 23, 2012 12:06 pm

Ok, Phyll if you can help me to set this up , i 'll be grateful.

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

Re: empty listview from data with preferences

Postby Phyll » Thu Feb 23, 2012 1:42 pm

Hi gpolis,

The dialog class can look like this:

Code: Select all
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView;
import android.widget.EditText;
import android.view.View;
import android.view.WindowManager.LayoutParams;
import android.widget.Button;

public class EntryDialog extends Activity {
    TextView serialNumber_String;
    EditText serial;
    TextView date_String;
    EditText date;
    TextView strength_String;
    EditText strength;

    @Override
    public void onCreate(Bundle icicle) {
        super.onCreate(icicle);
        setContentView(R.layout.entrydialog);
       
        LayoutParams params = getWindow().getAttributes(); 
        params.width = LayoutParams.FILL_PARENT; 
        getWindow().setAttributes((android.view.WindowManager.LayoutParams) params);       
       
        Button cancel = (Button)findViewById(R.id.button_cancel);
        cancel.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
               EntryDialog.this.setResult(RESULT_CANCELED);
               finish();
                }
            });
        strength = (EditText)findViewById(R.id.strengthEditText);
        strength_String = (TextView)findViewById(R.id.strengthTextView);
        date = (EditText)findViewById(R.id.dateEditText);
        date_String = (TextView)findViewById(R.id.dateTextView);
        serial = (EditText)findViewById(R.id.serialEditText);
        serialNumber_String = (TextView)findViewById(R.id.serialNumberTextView);       
        Button done = (Button)findViewById(R.id.button_done);
        done.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
              Intent in = new Intent();
                in.putExtra("serial", serial.getText().toString());
                in.putExtra("date", date.getText().toString());               
                in.putExtra("strength", strength.getText().toString());               
              EntryDialog.this.setResult(RESULT_OK, in);   
              finish();               
                }
            });
        }//oncreate


    //TODO: Fill In Methods Etc.
    }//class


The layout looks like this:

Code: Select all
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <TextView
        android:id="@+id/serialNumberTextView"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="Serial Number"
        android:textSize="14sp"
        />
    <EditText
        android:id="@+id/serialEditText"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text=""
        android:textSize="14sp"
        />
    <TextView
        android:id="@+id/dateTextView"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="Date"
        android:textSize="14sp"
        />
    <EditText
        android:id="@+id/dateEditText"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text=""
        android:textSize="14sp"
        />
    <TextView
        android:id="@+id/strengthTextView"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="Strength"
        android:textSize="14sp"
        />
    <EditText
        android:id="@+id/strengthEditText"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text=""
        android:textSize="14sp"
        />
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:weightSum="2.0">   
    <Button
        android:id="@+id/button_done"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="Done"
        android:layout_weight="1.0"
        />
    <Button
        android:id="@+id/button_cancel"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="Cancel"
        android:layout_weight="1.0"
        />
</LinearLayout>
</LinearLayout>


Declare it in the manifest like this:

Code: Select all
      <activity android:name=".EntryDialog"
         android:label="EntryDialog"
         android:theme="@android:style/Theme.Dialog">
      </activity>


Add this to your main activity that will start it:

Code: Select all
    private static final int SERIAL_DIAG = 1;   // Serial dialog's id

    @Override
    public void onCreate(Bundle icicle) {
        super.onCreate(icicle);
        setContentView(R.layout.main);
        Button enter = (Button)findViewById(R.id.button_enter);
        enter.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {

// This is the part you need to call it. The rest
// of this oncreate is just to show how it might
// be used. Use the next two lines to start the
// dialog.
                Intent i = new Intent(App11.this, EntryDialog.class);
                startActivityForResult(i, SERIAL_DIAG);               
                }
            });
        }

// You need this to get the result back
// when the user clicks done in the dialog


    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == Activity.RESULT_OK) {
           if (requestCode == SERIAL_DIAG) {
              
              String serial = data.getStringExtra("serial");
              String date = data.getStringExtra("date");
              String strength = data.getStringExtra("strength");
              
              Log.i("RETURNED",serial+" "+date+" "+strength);

//check input and/or fixup
//store in preferences
//add to list
//pass on to list display

               }//serial dialog
           
            }//if result_ok
        }//onactivityresult


Of course you might need to check what the user has entered and there are a lot of other things you can do with this basic form.

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 » Thu Feb 23, 2012 5:55 pm

Phyll thanks a lot for your help!

I used your code and i added a button "goto_data" which shows me the list with the serial numbers.(it points to List_hold activity)

Also,in the main activity i added as you say :

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;
                  String [] values=new String [100];
                 //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++) {
                     values[i] = "serial"+i;
                      prefsEditr.putString("serial"+i, values[i]);
                  }

                  prefsEditr.putString("date", "");
                  prefsEditr.putString("strength", "1.0");
                  prefsEditr.commit();   
                  }//serial dialog


The List_hold activity which is going to show me the data:

Code: Select all
public class List_hold extends ListActivity {
   
   SharedPreferences mypref=null;
   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"+i,"");
        }
       ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
            android.R.layout.simple_list_item_1,listItems);            
       setListAdapter(adapter);   
      
   }
   


The problem is that i get a list with serial numbers as i have "putString("serial"+i)".
It doesn't correspond to my set of data.

What else may i do ?
Thanks!
gpolis
Developer
Developer
 
Posts: 38
Joined: Wed Feb 15, 2012 6:28 pm

Re: empty listview from data with preferences

Postby Phyll » Thu Feb 23, 2012 10:20 pm

Hi gpolis,

I think this is your problem. Doesn't this reset everything when it runs? You are making values[i] and storing it in named item "serial"+i with a value of "serial"+i. Is that right?

Code: Select all
                  final Integer counter =values.length;
                  prefsEditr.putInt("size", counter);
                  for (int i=0;i<counter;i++) {
                     values[i] = "serial"+i;
                      prefsEditr.putString("serial"+i, values[i]);
                  }


I think you should first get the size from the preferences. It will come back with the next number in order. It will only have this entry on the first one. Two on the second one, etc. I didn't try it in Eclipse, but something like this:

Code: Select all
        int size = prefsEditr.getInt("size",0);
        prefsEditr.putString("serial"+size, serial);
        size++;
        prefsEditr.putInt("size", size);


You should probably uninstall the app so that it will remove the preferences you already have and start a new list. In the list part just get the size and use that to populate your array for the list. Same as you were I guess. If you want to store those other values with this, I think I would put them together in one string like this:

serial = serial+","+date+","+strength

or use spaces, then keep that whole string in the preference item of each record instead of just serial.

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 11:36 am

Hello Phyll,

yes,it's wrong as i have it.But ,i don't understand how to implement it now.

I have this:

Code: Select all
final Integer counter =values.length;
                  prefsEditr.putInt("size", counter);
                  for (int i=0;i<counter;i++) {
                     values[i] = "serial"+i;
                      prefsEditr.putString("serial"+i, values[i]);
                  }


I can't replace it with this:

Code: Select all
int size = prefsEditr.getInt("size",0);
        prefsEditr.putString("serial"+size, serial);
        size++;
        prefsEditr.putInt("size", size);


because the one is for putting data(,putInt) and the other is for loading (.getInt).
I am confused about how to implement it.

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

Top
Next

Return to Other Coding-Problems

Who is online

Users browsing this forum: Google [Bot] and 15 guests