如何在此方法中返回可运行对象?

发布于 11-05 08:01 字数 1720 浏览 5 评论 0原文

我不知道如何返回可运行方法(或使用此特定方法)。我的想法可能是错误的(?)。有什么帮助吗?谢谢!

*这是这篇文章的继续/相关内容。但我认为它本身就可以成为一个问题。

在 Activity onCreate:

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

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 */
}

StorageStateChecker 类中:

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

南薇2024-11-12 08:01:29

没有理由使用方法来返回 Runnable。只需声明一个成员(或静态最终)Runnable。所有执行的代码都在 run() 方法中。

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

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);
    }
};
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文