创建ArrayAdapter的自定义Abstract类

时间:2014-02-03 15:15:19

标签: java android class android-arrayadapter abstract

我想创建自己的ArrayAdapter抽象版本,它增加了自定义Intent导航和Toast弹出窗口等功能。

以下是我的自定义活动的示例:

public abstract class RBActivity extends Activity implements IActivity {

    private Navigator navigator;
    private Toaster toaster;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        navigator = new Navigator(this);
        toaster = new Toaster(this);
    }

    public Navigator navigate() {
        return navigator;
    }

    protected void popBurntToast(String msg) {
        toaster.popBurntToast(msg);
    }

    protected void popToast(String msg) {
        toaster.popToast(msg);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        navigator = null;
        toaster = null;
    }   
}

然后在我的活动中,我只是从RBActivity而不是Activity延伸。然后我可以调用this.navigate()。toSomeActivity();使用我的Navigator class,它具有轻松创建意图的方法来浏览我的应用程序。我也有自己的RBListActivity

以下是我的导航器类的示例方法:

public void toSiteSubscribeActivity(Site _site) {
    Intent intent = new Intent(activity, SiteSubscribeActivity.class);
    Bundle b = new Bundle();
    b.putLong(Navigator.INTENT_KEY_CHANNEL_ID, _site.getIdSite());
    b.putString(Navigator.INTENT_KEY_CHANNEL_NAME, _site.getName());
    b.putString(Navigator.INTENT_KEY_CHANNEL_DESCRIPTION, _site.getDescription());
    intent.putExtras(b);
    activity.startActivity(intent);
}    

无论如何,我想要一个RBArrayAdapter<>但我不知道如何创建一个通用版本,不需要我在<>中指定类类型大括号即

public abstract class RBArrayAdapter extends ArrayAdapter<Article> {
    private Navigator navigator;
    private Toaster toaster;

    public RBArrayAdapter(Activity _activity, int _layout, ArrayList<Article> _notes) {
        super(_activity, _layout, _notes);

        navigator = new Navigator(_activity);
        toaster = new Toaster(_activity);
    }


    public Navigator navigate() {
        return navigator;
    }   
}

我尝试过像public abstract class RBArrayAdapter extends ArrayAdapter<?>这样的事情,但显然不起作用......

我想简而言之,我问如何在尖括号中接受泛型类

任何帮助都非常感谢,干杯

编辑:看完ArrayAdapter.java(https://github.com/android/platform_frameworks_base/blob/master/core/java/android/widget/ArrayAdapter.java后)我觉得我需要的是ArrayAdapter<T>语法。现在调查那个

1 个答案:

答案 0 :(得分:0)

如果我理解正确,您只需要使用语法来完成这项工作。试试这个:

public abstract class RBArrayAdapter<T> extends ArrayAdapter<T> {
    private Navigator navigator;
    private Toaster toaster;

    public RBArrayAdapter(Activity _activity, int _layout, ArrayList<T> _notes) {
        super(_activity, _layout, _notes);

        navigator = new Navigator(_activity);
        toaster = new Toaster(_activity);
    }


    public Navigator navigate() {
        return navigator;
    }   
}

然后,当您实例化RBArrayAdapter子类时,您应该能够替换<T>的任何参数(类似这样):

public class MyStringArrayAdapter extends RBArrayAdapter<String> {

    public MyStringArrayAdapter(Activity _activity, int _layout, ArrayList<String> _notes) {
      super(_activity, _layout, _notes);
    }

}

玩得开心。