How can I Reuse Methods for ListViews?

前端 未结 2 1297
南笙
南笙 2021-01-24 12:01

I have several methods in the ListAC ListActivity that I want to reuse and would like to put in separate classes (if possible??). I will be having dozens of ListView Activities

2条回答
  •  Happy的楠姐
    2021-01-24 12:45

    Are there any similarities in the code of your extended SimpleCursorAdapter? With regards to code like getting the external storage state, you could implement a class called, for example Utils and make the methods within it static. You will more than likely need a Context or Activity passed as a parameter however.

    For example this is my external storage state checker.

    public static final boolean isExternalStorageAvailable() {
        if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
            return true;
        } else {
            return false;
        }
    }
    

    And my network state checker.

    public static final boolean isPreferedNetworkAvailable(final Context context) {
        final ConnectivityManager connectivityManager = (ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE);
        final NetworkInfo networkInfo = connectivityManager.getNetworkInfo(connectivityManager.getNetworkPreference());
        return networkInfo.isAvailable();
    }
    

提交回复
热议问题