-1

I am googling for the last few hours, I've seen this question asked like 20 times but none of the solutions seem to work for me. I have a ListView and a CustomList class, that incorporates a list adapter and a simple dialog for editing/removing/renaming items. everything works as supposed until the first screen rotation. After that, the list stops being updated, although the adapter contains all the right data. I will post both files without skipping a line. I don't need any data being saved at this time, I will handle that later. This is really weird because supposedly the application gets completely restarted after screen rotation. What makes it even stranger, the dummy items are added onCreate even after screen rotation, but after that there is no other response from the list.

Please excuse my english and my noobness and try to point me in the right direction.

  package com.sl.mylandmarks;

import android.app.Activity; import android.content.ContentResolver; import android.content.Context; import android.location.LocationManager; import android.os.Bundle; import android.provider.Settings; import android.text.Editable; import android.text.TextWatcher; import android.util.Log; import android.view.Menu; import android.view.View; import android.view.View.OnClickListener; import android.widget.AdapterView; import android.widget.AdapterView.OnItemLongClickListener; import android.widget.Button; import android.widget.EditText; import android.widget.ListView; import android.widget.TextView;

public class MainActivity extends Activity {

    Button addButton;   ListView landmarksList;     EditText inputName, inputSearch;    CustomList customList;  Context globalContext;  TextView gpsState;  private LocationManager locationManager;    private GPSTracker gpsTracker;  private double longitude;   private double latitude;

    @Override   protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);         setContentView(R.layout.activity_main);

        gpsTracker = new GPSTracker(MainActivity.this);

        globalContext = this;

        addButton = (Button) findViewById(R.id.addBtn);

        customList = new CustomList(this);

                landmarksList = (ListView) findViewById(R.id.listView1);        landmarksList.setAdapter(customList.getAdapter());      landmarksList.setChoiceMode(ListView.CHOICE_MODE_SINGLE);       landmarksList.setTextFilterEnabled(true);       landmarksList.setLongClickable(true);       customList.theList = landmarksList;


        inputName = (EditText) findViewById(R.id.EditItemName);

        gpsState = (TextView) findViewById(R.id.gpsState);

        ContentResolver contentResolver = getBaseContext().getContentResolver();        boolean gpsStatus = Settings.Secure.isLocationProviderEnabled(
                contentResolver, LocationManager.GPS_PROVIDER);         if (gpsStatus) {            gpsState.setText("GPS Enabled");        } else {            gpsState.setText("GPS Disabled");       }

        // // SEARCH BOX

        inputSearch = (EditText) findViewById(R.id.editText3);      inputSearch.addTextChangedListener(new TextWatcher() {

            public void afterTextChanged(Editable s) {

            }

            public void beforeTextChanged(CharSequence s, int start, int count,
                    int after) {            }

            public void onTextChanged(CharSequence s, int start, int before,
                    int count) {
                customList.getAdapter().getFilter().filter(s);          }       });         // // SEARCH BOX



        // / DUMMY ITEMS        customList.addItem("Home", "43.1565 / 15.8645");        customList.addItem("Work", "43.1565 / 15.8645");        customList.addItem("Denis` apartment", "43.1565 / 15.8645");        customList.addItem("Natasa", "43.1565 / 15.8645");      customList.addItem("Bruce Wayne", "43.1565 / 15.8645");         customList.addItem("Walker shop", "43.1565 / 15.8645");         customList.addItem("Chuck Norris Residence", "43.1565 / 15.8645");      customList.addItem("Some landmark", "43.1565 / 15.8645");       // customList.removeItem(3);


        OnItemLongClickListener listLongClick = new OnItemLongClickListener() {

            @Override           public boolean onItemLongClick(AdapterView<?> arg0, View arg1,
                    int arg2, long arg3) {
                customList.showOptionsDialog(arg2);
                return true;// event consumed, not dispatched forward           }       };

        landmarksList.setOnItemLongClickListener(listLongClick);

        OnClickListener ButtonClick = new View.OnClickListener() {

            @Override           public void onClick(View v) {
                switch (v.getId()) {
                case R.id.addBtn:

                    customList.addItem(inputName.getText().toString(),
                            "45.5644 / 23.6541");

                    inputName.setText("");
                    break;

                }

            }

        };

        addButton.setOnClickListener(ButtonClick);

    }

    @Override   public void onPause() {         super.onPause();


    }

    @Override   public void onResume() {        super.onResume();

    }

    @Override   public boolean onCreateOptionsMenu(Menu menu) {         // Inflate the menu; this adds items to the action bar if it is present.        getMenuInflater().inflate(R.menu.main, menu);       return true;

    }

}

and CustomList.java

package com.sl.mylandmarks;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.app.Dialog;
import android.content.Context;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.SimpleAdapter;

public class CustomList {

    ArrayList<HashMap<String, String>> list = new ArrayList<HashMap<String, String>>(
            2);
    HashMap<String, String> maplist;
    SimpleAdapter listAdapter;
    public Context context;
    public ListView theList;
    public CustomList(Context con) {

        context = con;
        String[] from = { "line1", "line2" };

        int[] to = { android.R.id.text1, android.R.id.text2 };

        listAdapter = new SimpleAdapter(context, list,
                android.R.layout.simple_list_item_2, from, to);

    }

    public void addItem(String name, String coords) {
        if ((name != null) && (name.length() != 0)) {
            maplist = new HashMap<String, String>();
            maplist.put("line1", name);
            maplist.put("line2", coords);
            list.add(maplist);
            listAdapter.notifyDataSetChanged();
        }
    }

    public void removeItem(int id) {



        Log.d("removing",list.remove(id).toString());

        listAdapter.notifyDataSetChanged();
    }

    public void renameItem(int id, String newName) {
        Log.d("SL","Rename Selected");
        maplist = new HashMap<String, String>();
        maplist.put("line1", newName);
        maplist.put("line2", list.get(id).get("line2"));
        list.set(id, maplist);
        listAdapter.notifyDataSetChanged();

    }

    public SimpleAdapter getAdapter() {

        return listAdapter;
    }

    public void showOptionsDialog(final int position) {

        //Log.d("SL", String.valueOf(position));
        final Dialog optionsDialog = new Dialog(context);
        optionsDialog.setContentView(R.layout.list_dialog);
        optionsDialog.setTitle("Options");
        optionsDialog.show();
        final EditText itemNameEdit = (EditText) optionsDialog
                .findViewById(R.id.EditItemName);
        final Button removeBtn = (Button) optionsDialog
                .findViewById(R.id.removeBtn);
        final Button renameBtn = (Button) optionsDialog
                .findViewById(R.id.renameBtn);
        final Button cancelBtn = (Button) optionsDialog
                .findViewById(R.id.cancelBtn);

        itemNameEdit.setText(list.get(position).get("line1"));
        itemNameEdit.setSelection(itemNameEdit.length());

        OnClickListener ButtonClick = new View.OnClickListener() {

            @Override
            public void onClick(View arg0) {
                switch (arg0.getId()) {
                case R.id.removeBtn:
                    removeItem(position);
                    optionsDialog.dismiss();
                    break;
                case R.id.renameBtn:
                    renameItem(position, itemNameEdit.getText().toString());
                    optionsDialog.dismiss();
                    break;
                case R.id.cancelBtn:
                    optionsDialog.dismiss();
                    break;
                }
            }

        };

        removeBtn.setOnClickListener(ButtonClick);
        renameBtn.setOnClickListener(ButtonClick);
        cancelBtn.setOnClickListener(ButtonClick);

    }

}
Nick
  • 585
  • 4
  • 11
  • What do you mean by "supposedly the application gets completely restarted after screen rotation"? Normally, on screen rotation, the onStop() method of your activity will be called by the system, and then onRestart() followed by onStart(). Your activity should not be deleted/destroyed completely unless your Android device lack ressources. I recommend trying this in the emulator if possible. – roky Apr 18 '13 at 20:57
  • Well I noticed that onCreate is also successfully called, everything works as supposed, except what I wrote above. Will try it right now – Nick Apr 18 '13 at 21:02
  • Same thing happens in the emulator. This is really weird, I wouldn't have come here otherwise, this bug ate my day – Nick Apr 18 '13 at 21:18
  • http://meta.tex.stackexchange.com/questions/228/ive-just-been-asked-to-write-a-minimal-example-what-is-that – m0skit0 Apr 18 '13 at 21:35

1 Answers1

0

I not sure what exactly is your question. And I didn't look at your code, because I don't think that SO is a good place to just dump classes and say "fix this". But I might have an answer for you.

It is true that Android is recreating the activity when you rotate (like roky says). If you come from an iPhone development background (which is my case), it look like kind of weird.

But if your activity is not too complex, you can say to Android to handle it. Do to this, add this to your tag of your AndroidManifest.xml

<activity android:configChanges="orientation|screenSize" ... >
</activity>

Here is a better explanation than mine : android:configChanges="orientation" does not work with fragments

If my answer was the good one, it would be nice to accept it!

Community
  • 1
  • 1
NLemay
  • 2,331
  • 3
  • 29
  • 45
  • Sorry for dumping my classes, I didn't want to miss anything. Please take a look at the code and see if you can tell why does the list stop being updated after the first rotation. I'm not trying to handle a rotation nor save any data. At this moment all I want is for it to act as if the application was restarted, which it doesn't – Nick Apr 18 '13 at 21:14
  • No problem. It's still better than not putting code at all! But you should try to make your question more precise. But before looking at your code, did you tried my trick? – NLemay Apr 18 '13 at 21:18
  • well, doing that means I have to handle the rotation myself. I am working on this right now, although this is not what I'm after. I'm fine with it rotating itself while restarting the app. What I don't understand is why the List is no longer updated after rotation. – Nick Apr 18 '13 at 21:27
  • Sorry, this actually worked and I forgot to tell you. Thanks a lot man, it took me a while to understand how android handles configuration changes – Nick May 03 '13 at 21:58
  • You're welcome. Thank you for letting me know that my solution was working for you! – NLemay May 06 '13 at 13:32