Jeongbebs
Jeongbebs

Reputation: 4120

Listview not showing on orientation Change

I have previously done a listview and the list is being displayed whether in landscape or portrait orientation. However, in my new app, when I decide to change orientation, whether landscape to portrait or vice versa, it does not load again. I am trying to populate the listview with items from sqlite database. Here is my code:

Attractions.java

public class Attractions extends ListActivity {
DataBaseHandler db = new DataBaseHandler(this);
ArrayList<Contact> imageArry = new ArrayList<Contact>();
ContactImageAdapter adapter;
int ctr, loaded; 
int [] landmarkImages={R.drawable.oblation,R.drawable.eastwood,R.drawable.ecopark,R.drawable.circle};
String []landmarkDetails = { "Oblation", "Eastwood", "Ecopark", "QC Circle"};

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_attractions);
    ctr  = db.checkContact(landmarkDetails[loaded]);
    db.deleteAll();


    // get image from drawable

    /**
     * CRUD Operations
     * */
    // Inserting Contacts
    Log.d("Insert: ", "Inserting ..");


    for(loaded=0; loaded <landmarkDetails.length;loaded++){

        Bitmap image = BitmapFactory.decodeResource(getResources(),
                landmarkImages[loaded]);

        // convert bitmap to byte
        ByteArrayOutputStream stream = new ByteArrayOutputStream();
        image.compress(Bitmap.CompressFormat.JPEG, 100, stream);
        byte imageInByte[] = stream.toByteArray();
        Log.d("Going to load images", "Image "+ loaded);

        Log.d("Goind to load objects", "loading");

        if(ctr == 0){
            Log.d("Nothing Loaded", "Loading Now");
            db.addContact(new Contact(landmarkDetails[loaded], imageInByte));}
            Log.d(landmarkDetails[loaded], "Loaded!");
            image.recycle();
    }


    // Reading all contacts from database
    List<Contact> contacts = db.getAllContacts();
    for (Contact cn : contacts) {
        String log = "ID:" + cn.getID() + " Name: " + cn.getName()
                + " ,Image: " + cn.getImage();

        // Writing Contacts to log
        Log.d("Result: ", log);
        //add contacts data in arrayList
        imageArry.add(cn);

    }
    adapter = new ContactImageAdapter(this, R.layout.screen_list,
            imageArry);
    ListView dataList = (ListView) findViewById(android.R.id.list);
    dataList.setAdapter(adapter);
}

UPDATE

I ran the app on portrait mode: list present, when switched to landscape: does not display anything. I switch to portrait again: list present again. Thanks for those who will help!

UPDATE2

When I change orientation, these lines are not being executed:

        List<Contact> contacts = db.getAllContacts();
    for (Contact cn : contacts) {
        String log = "ID:" + cn.getID() + " Name: " + cn.getName()
                + " ,Image: " + cn.getImage();

        // Writing Contacts to log
        Log.d("Result: ", log);
        //add contacts data in arrayList
        imageArry.add(cn);

    }
    adapter = new ContactImageAdapter(this, R.layout.screen_list,
            imageArry);
    ListView dataList = (ListView) findViewById(android.R.id.list);
    dataList.setAdapter(adapter);

The messages I set do not appear on the logcat. Please help. :(

Upvotes: 0

Views: 679

Answers (2)

cyanide
cyanide

Reputation: 3964

I probably know, what it is. This code is used for adding DB elements:

 if(ctr == 0){
        Log.d("Nothing Loaded", "Loading Now");
        db.addContact(new Contact(landmarkDetails[loaded], imageInByte));}

First time, you DB is clean. checkContact results 0, condition (ctr==0) is met, therefore addContact is executed.

Second time, you DB is "dirty" from previous run (note, that db.deleteAll is called after checking contacts). Since DB is not clean, ctr != 0, the code above is never executed. For that reason db.getAllContacts() returns empty data, and the last loop never fires.

Third time your DB is clean again, as a result of deleteAll, et cetera, et cetera!

A nice cute error, cannot think of something more elegant. :)

If you prevent activity from re-activating, the error will go, and, unless the appearance depends on orientation (e.g. several layouts for portrait and landscape), it should be all right.

Upvotes: 0

shaktiman_droid
shaktiman_droid

Reputation: 2633

open your manifest.xml file and change Activity<> tag as below: then check

<activity android:configChanges="orientation">

This will avoid the re-creation of Activity.

Upvotes: 1

Related Questions