1

Currently when the user selects an item from the listview with the function onItemClick, I am able to successfully get the data and load it into other fragments but when the app closes by the user closing in the task manager, the user has to go into the listview again and select the item again from the listview.

I've tried using sharedPreferences to save and restore the values of the selected item in the listview but have had no luck.

public static String SelectedName = null;
public static String SelectedPhNo = null;


// Set the ListViews OnItemClick Listener
            display_contacts1.setOnItemClickListener(new AdapterView.OnItemClickListener()
            {
                @Override
                public void onItemClick(AdapterView<?> parent, View view, int position, long id)
                {
                    //String namedisplay = arrayAdapter.getItem(position); //<<<<<<<<<< this gets the phone name
                    String namedisplay = myCursor.getString(myCursor.getColumnIndex(DataDBAdapter.COl_MYTABLE_PHONENAME));
                    String phoneno_display = myCursor.getString(myCursor.getColumnIndex(DataDBAdapter.COL_MYTABLE_PHONENUMBER));

                    Toast.makeText(view.getContext(), namedisplay + " Selected for Communication", Toast.LENGTH_SHORT).show();
                    Toast.makeText(view.getContext(), phoneno_display, Toast.LENGTH_SHORT).show();

                    // Set the selected contact variables
                    SelectedName = namedisplay;
                    SelectedPhNo = phoneno_display;
                 }
             });

 // Save the data here
    @Override
    public void onSaveInstanceState(Bundle outState)
    {
       super.onSaveInstanceState(outState);

      // Save the textview state 1st item is the key and 2nd item is the variable
      outState.putString("savedname", SelectedName);
      outState.putString("savedphNo", SelectedPhNo);
    }


 @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
    {
        View view = inflater.inflate(R.layout.fragment_select_modem, container, false);
        btn_add = (Button) view.findViewById(R.id.btn_add);

        if(savedInstanceState != null)
        {
            savedInstanceState.getString("savedname");
            savedInstanceState.getString("savedphNo");
        }
    }
Sahil Bora
  • 173
  • 1
  • 12

0 Answers0