标题可能有些混乱,但这就是我要面对的问题

我有一堂课:

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

        }
    }




}


然后我有一个子类为:

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


}


现在,我将扩展此类,如下所示:

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


}


我的问题是我可以通过某种方式使handleDisplayData通用,以便可以将任何类型传递给它。我想要做的是尽可能重用BaseListFragmentActivity中的逻辑,并处理该类/子类中ListAccountsActivity或其子类的唯一任务。

我希望我的问题很清楚,谢谢您的帮助

最佳答案

如果我理解正确,则希望能够从子类的基础使用特定于类型的方法,为此,您需要使所有内容通用:

public abstract class GenericBase<T> { ... }
public abstract class ExtendedGeneric<T> extends GenericBase<T> { ... }
public class ExtendedGenericSub<T> extends ExtendedGeneric<T> { ... }


关键是,如果ExtendedGeneric扩展GenericBase<DataOutput>,则只能从GenericBase<DataOutput>访问ExtendedGeneric的方法。

10-07 15:26