Search code examples
androidlistviewreturnsd-cardrunnable

How do I Return a Runnable in this Method?


I don't know how to make a Return on a Runnable Method (or w/this particular method). I may have the idea wrong to(?). Any help? thnx!

*This is continued/related from this post. But thought it could be a Q on its own.

In the Activities onCreate:

Runnable doIfMounted = orderASC_Label();
    StorageStateChecker.performExternalStorageOperation(doIfMounted );

The Runnable:

/**
 * -- Default List Order (Ascending)
 * =====================================================================
 * @return 
 **/
public Runnable orderASC_Label() {
    Cursor databaseCursor = db.rawQuery(
            "SELECT * FROM AC_list ORDER BY `label` ASC", null);

    Adapter_AC databaseListAdapter = new Adapter_AC(this,
            R.layout.list_item, databaseCursor, new String[] { "label",
                    "title", "description", "gotoURL" }, new int[] {
                    R.id.label, R.id.listTitle, R.id.caption, R.id.dummy });

    databaseListAdapter.notifyDataSetChanged();
    this.setListAdapter(databaseListAdapter);
    return /*null; <-- What do I do here to make this runnable */
}

The StorageStateChecker class:

public class StorageStateChecker {

public static boolean performExternalStorageOperation(Runnable doIfMounted) {
    if (android.os.Environment.getExternalStorageState().equals(
            android.os.Environment.MEDIA_MOUNTED)) {

        if (doIfMounted != null) {
            doIfMounted.run();
        }
        return true;

    } else if (android.os.Environment.getExternalStorageState().equals(
            android.os.Environment.MEDIA_UNMOUNTED)) {
         //Alerts.sdCardMissing(this);
    }
    return false;
}
}

Solution

  • There's no reason to use a method to return a Runnable. Simply declare a member (or static final) Runnable. All the code that executes goes in the run() method.

    private static final Runnable ORDER_ASC = new Runnable() {
        public void run() {
            Cursor databaseCursor = db.rawQuery(
                "SELECT * FROM AC_list ORDER BY `label` ASC", null);
    
            Adapter_AC databaseListAdapter = new Adapter_AC(this,
                R.layout.list_item, databaseCursor, new String[] { "label",
                        "title", "description", "gotoURL" }, new int[] {
                        R.id.label, R.id.listTitle, R.id.caption, R.id.dummy });
    
            databaseListAdapter.notifyDataSetChanged();
            this.setListAdapter(databaseListAdapter);
        }
    };