Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/209.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Android、泛型和固有性_Java_Android_Generics - Fatal编程技术网

Java Android、泛型和固有性

Java Android、泛型和固有性,java,android,generics,Java,Android,Generics,标题可能有点混乱,但这就是我面临的问题 我有一门课: public abstract class BaseFragmentActivity<T> extends FragmentActivity { static final int PROGRESS_DIALOG = 0; Dialog progessDialog; public abstract void displayData(T output); @Override prot

标题可能有点混乱,但这就是我面临的问题

我有一门课:

    public abstract class BaseFragmentActivity<T> extends FragmentActivity {
    static final int PROGRESS_DIALOG = 0;
    Dialog progessDialog;

    public abstract void displayData(T output);

    @Override
    protected Dialog onCreateDialog(int id) {
        if (id == PROGRESS_DIALOG) {
            ProgressDialog progressDialog = ProgressDialog.show(this, "",
                    "Loading. Please wait...", true);
            progessDialog = progressDialog;
        }

        return progessDialog;
    }

    class PerformOPTask extends AsyncTask<Void, String, T> {
        // connector=new JSONConnector();
        Connector connector;
        String curUrl;
        Class<T> clazz;

        PerformOPTask(String url, Class<T> curClazz) {
            //connector = new UnitTestConnector();
            connector = new JSONConnector();
            curUrl = url;
            clazz = curClazz;
        }

        @Override
        protected T doInBackground(Void... params) {

            return connector.getData(URLUtils.getFormattedUrl(curUrl),clazz);
        }

        @Override
        protected void onPostExecute(T output) {
            displayData(output);

        }
    }




}
公共抽象类BaseFragmentActivity扩展了FragmentActivity{
静态最终整数进度对话框=0;
对话进程对话;
公共摘要无效显示数据(T输出);
@凌驾
受保护的对话框onCreateDialog(int id){
如果(id==进度_对话框){
ProgressDialog=ProgressDialog.show(此“”,
“正在加载。请稍候…”,正确);
progressDialog=progressDialog;
}
返回程序对话框;
}
类PerformOPTask扩展了AsyncTask{
//连接器=新的JSONConnector();
连接器;
字符串curUrl;
课堂讨论;
PerformOPTask(字符串url,类curClazz){
//连接器=新的UnitTestConnector();
连接器=新的JSONConnector();
curUrl=url;
clazz=curClazz;
}
@凌驾
受保护的T doInBackground(无效…参数){
返回connector.getData(URLUtils.getFormattedUrl(curUrl),clazz);
}
@凌驾
受保护的void onPostExecute(T输出){
显示数据(输出);
}
}
}
然后我有一个子类:

public abstract class BaseListFragmentActivity<T> extends BaseFragmentActivity<T> implements OnItemClickListener, OnClickListener{

    protected ListView mList;


    /** Called when the activity is first created. */
    public void onCreate(Bundle icicle) {
        super.onCreate(icicle);
        setContentView(R.layout.table_list);
        CommonUtil.getActionBarWithBackButton(this,getLayoutInflater());
        mList=(ListView)findViewById(R.id.table_list_listView);
        mList.setOnItemClickListener(this);

    }

    public void onBackABButtonPressed(View view) {
        finish();
    }

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub

    }

    @Override
    public abstract void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3);


}
公共抽象类BaseListFragmentActivity扩展BaseFragmentActivity实现OnItemClickListener、OnClickListener{
受保护的ListView mList;
/**在首次创建活动时调用*/
创建公共空间(捆绑冰柱){
超级冰柱;
setContentView(R.layout.table_列表);
getActionBarWithBackButton(这个,getLayoutInflater());
mList=(ListView)findViewById(R.id.table\u list\u ListView);
mList.setOnItemClickListener(此);
}
public void onBackABButtonPressed(视图){
完成();
}
@凌驾
公共void onClick(视图v){
//TODO自动生成的方法存根
}
@凌驾
公共摘要无效MClick(AdapterView arg0、视图arg1、int arg2、long arg3);
}
现在我扩展这个类,如下所示:

public class ListAccountsActivity<T> extends BaseListFragmentActivity<AccountData> {

    protected Acct[] mItems;
    private String[] mIcons;
    protected boolean displayHandledBySubClass=false;


    /** Called when the activity is first created. */
    public void onCreate(Bundle icicle) {
        super.onCreate(icicle);


        new PerformOPTask(getString(R.string.get_account),AccountData.class).execute();
        showDialog(PROGRESS_DIALOG);
        //.getData(URLUtils.getFormattedUrl(getString(R.string.get_account)),actData);




    }

    @Override
    public void onItemClick(AdapterView<?> lv, View view, int position, long id) {
        // super.onListItemClick(l, v, position, id);
        // Get the item that was clicked
        Acct account = (Acct) mList.getAdapter().getItem(position);
        Intent intent=new Intent(this,AccountDetailViewActivity.class);
        intent.putExtra("selectedAccount",account);
        startActivity(intent);
    }

    @Override
    public void displayData(AccountData output){


        if(displayHandledBySubClass){
            //handle display in subclass
            handleDisplayData(output);
        }
        else {
            Acct[] accountArray = new Acct[output.getAccount().size()];
            mItems = output.getAccount().toArray(accountArray);
            IWMArrayAdapter<Acct> adapter = new IWMArrayAdapter<Acct>(this, mItems);
            adapter.setIcons(mIcons);
            adapter.setArrowNeeded();
            //mList is superClassVariable
            mList.setAdapter(adapter);
            dismissDialog(PROGRESS_DIALOG);
            adapter.notifyDataSetChanged();
        }
    }

    public void handleDisplayData(T output){

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        //Toast.makeText(this, "Tapped search", Toast.LENGTH_SHORT).show();
        super.onCreateOptionsMenu(menu);
        MenuInflater menuInflater = getMenuInflater();
        menuInflater.inflate(R.menu.list_servers_menu, menu);

        // Calling super after populating the menu is necessary here to ensure
        // that the
        // action bar helpers have a chance to handle this event.
        return true;
    }


}
公共类ListAccountsActivity扩展了BaseListFragmentActivity{
受保护的帐户[]位;
私有字符串[]mIcons;
受保护的布尔显示HandledBysubclass=false;
/**在首次创建活动时调用*/
创建公共空间(捆绑冰柱){
超级冰柱;
新的PerformOPTask(getString(R.string.get_account)、AccountData.class.execute();
showDialog(进度对话框);
//.getData(URLUtils.getFormattedUrl(getString(R.string.get_account)),actData);
}
@凌驾
public void onItemClick(适配器视图lv、视图视图、内部位置、长id){
//super.onListItemClick(左、右、位置、id);
//获取已单击的项目
账户=(账户)mList.getAdapter().getItem(位置);
意向意向=新意向(此,AccountDetailViewActivity.class);
intent.putExtra(“selectedAccount”,account);
星触觉(意向);
}
@凌驾
公共void显示数据(AccountData输出){
if(显示由子类处理){
//子类中的句柄显示
处理显示数据(输出);
}
否则{
Acct[]accountArray=新帐户[output.getAccount().size()];
mItems=output.getAccount().toArray(accountArray);
IWMArrayAdapter adapter=新IWMArrayAdapter(此为mItems);
适配器设置图标(mIcons);
adapter.setArrowNeeded();
//mList是超类变量
mList.setAdapter(适配器);
解雇对话框(进度对话框);
adapter.notifyDataSetChanged();
}
}
公共无效处理显示数据(T输出){
}
@凌驾
公共布尔onCreateOptions菜单(菜单){
//Toast.makeText(这是“点击搜索”,Toast.LENGTH_SHORT).show();
super.onCreateOptions菜单(菜单);
MenuInflater MenuInflater=getMenuInflater();
菜单充气(右菜单列表服务器菜单,菜单);
//在填充菜单后调用super是必要的,以确保
//那
//操作栏帮助程序有机会处理此事件。
返回true;
}
}
我的问题是,我能否以某种方式将handleDisplayData设置为泛型,以便可以将任何类型传递给它。我试图做的是尽可能地重用BaseListFragmentActivity中的逻辑,并处理特定于ListAccountsActivity或该类/子类中其子类的唯一任务

我希望我的问题很清楚,谢谢你的帮助这是你想要的吗

public abstract class ExtendedGeneric<C> extends GenericBase<DataOutput> {

boolean handleInSub;

@Override
public void displayData(DataOutput t) {
    if(handleInSub){
        handleInSubClass(getValue(t));
    }

    //handle here
    System.out.println(t);

}

protected abstract void handleInSubClass(C c);
protected abstract C getValue(DataOutput t);
公共抽象类ExtendedGeneric扩展GenericBase{
布尔handleInSub;
@凌驾
公共无效显示数据(数据输出t){
if(handleInSub){
handleInSubClass(getValue(t));
}
//在这里处理
系统输出打印ln(t);
}
受保护的抽象无效handleInSubClass(C C);
受保护的抽象C getValue(数据输出t);
}

当然,这只是假设数据类型C将来自数据输出t。想法是您还可以将ExtendeGeneric参数化,这样您就可以使扩展它的类控制提供给handleInSubClass的数据类型。

这就是您想要的吗

public abstract class ExtendedGeneric<C> extends GenericBase<DataOutput> {

boolean handleInSub;

@Override
public void displayData(DataOutput t) {
    if(handleInSub){
        handleInSubClass(getValue(t));
    }

    //handle here
    System.out.println(t);

}

protected abstract void handleInSubClass(C c);
protected abstract C getValue(DataOutput t);
公共抽象类ExtendedGeneric扩展GenericBase{
布尔handleInSub;
@凌驾
公共无效显示数据(数据输出t){
if(handleInSub){
handleInSubClass(getValue(t));
}
//在这里处理
系统输出打印ln(t);
}
受保护的抽象无效handleInSubClass(C C);
受保护的抽象C getValue(数据输出t);
}

当然,这只是假设数据类型C将来自数据输出t。其思想是,您还可以将ExtendenGeneric参数化,这样您就可以使扩展它的类控制数据类型supplie
public class GenericChild<T> extends GenericParent<GenericChild> {

    @Override
    public void displayData(GenericChild t) {
        // Do Something Here...
    }

    /**
     * Using protected better than public,
     * to prevent other class access directly to this method.
     * But make sure this the other class is not same package with this GenericChild,
     * because if it same package than the other class can access directly this method.
    **/
    protected void handleSubClass(T t){}
}
public class SubClass extends GenericChild<SubClass> {

    @Override
    public void handleSubClass(SubClass t){
        // Do Something Here...
    }
}
public abstract class GenericBase<T> { ... }
public abstract class ExtendedGeneric<T> extends GenericBase<T> { ... }
public class ExtendedGenericSub<T> extends ExtendedGeneric<T> { ... }
class Vehicle {};

abstract class RoadVehicle extends Vehicle {
  abstract int getNumberOfWheels();
}

class Truck extends RoadVehicle {
  int getNumberOfWheels() {
    return 8;
  }
}

class Car extends RoadVehicle {
  int getNumberOfWheels() {
    return 4;
  }
}

abstract class GenericHandler<T extends Vehicle> {
  public abstract void displayData(T t);
}

abstract class RoadVehicleHandler<T extends RoadVehicle> 
    extends GenericHandler<T> {
  public void displayData(T t) {
    System.out.println(t.getNumberOfWheels() + " wheels");
    specialStuff();
  }

  abstract void specialStuff();
}

class CarHandler extends RoadVehicleHandler<Car> {
  void specialStuff() { /* honk horn */ }
}

class TruckHandler extends RoadVehicleHandler<Truck> {
  void specialStuff() { /* flash lights */ }
}