0

タイトルは少し紛らわしいかもしれませんが、これが私が直面していることです

私はクラスを持っています:

    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またはそのサブクラスに固有の唯一のタスクを処理することです。

私の質問が明確であることを願っています、助けてくれてありがとう

4

4 に答える 4

2

これは、あなたの望むことですか?

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

}

これはもちろん、データ型 C が DataOutput t から取得されることを前提としています。ExtendenGeneric をパラメータ化して、それを拡張するクラスが handleInSubClass に提供されるデータ型を制御できるようにすることもできます。

于 2012-05-04T02:07:00.660 に答える
2

私の理解が正しければ、サブクラスのベースから型固有のメソッドを使用できるようにする必要があり、そのためにはすべてを汎用にする必要があります。

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

ポイントは、ExtendedGenericextendsGenericBase<DataOutput>の場合、 のメソッドのみGenericBase<DataOutput>が からアクセスできるということExtendedGenericです。

于 2012-05-04T02:15:29.480 に答える
1

私たちはこのようなことについて話しているのですか?

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 */ }
}
于 2012-05-04T02:36:10.890 に答える
1

できますよ:

ジェネリック親

public abstract class GenericParent<T> {

    public abstract void displayData(T t);

}

GenericChild

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...
    }
}
于 2012-05-04T02:14:30.797 に答える