【问题标题】:How do I Return a Runnable in this Method?如何在此方法中返回 Runnable?
【发布时间】:2011-08-19 15:20:39
【问题描述】:

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

*这是从this post 继续/相关的。但认为它可能是一个 Q 本身。

在活动中创建:

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

【问题讨论】:

    标签: android listview return sd-card runnable


    【解决方案1】:

    没有理由使用方法来返回 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);
        }
    };
    

    【讨论】:

    • 我明白了。谢谢!我试试看。
    猜你喜欢
    • 1970-01-01
    • 2014-02-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-03-02
    • 2017-02-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多