How do I Return a Runnable in this Method?

三世轮回 提交于 2019-12-25 02:16:07

问题


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;
}
}

回答1:


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);
    }
};


来源:https://stackoverflow.com/questions/5902516/how-do-i-return-a-runnable-in-this-method

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!