2012-05-03 27 views
3

我使用的AsyncTask作爲具有以下簽名如下內部類:的AsyncTask和泛型


public abstract class BaseFragmentActivity 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<T> 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); 

    } 
} 

}

公共抽象類BaseListFragmentActivity延伸BaseFragmentActivity實現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); 

}

公共類ListAccountsActivity擴展BaseListFragmentActivity {

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

    // Create an array of Strings, that will be put to our ListActivity 
    // Log.i("MY INFO",this.runJSONParser().getAcct().toString()); 
    AccountData actData = new AccountData(); 
    //actData=(AccountData) 
    new PerformOPTask<AccountData>(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(ServerOutput output){** //error here 


    if(displayHandledBySubClass){ 
     //handle display in subclass 
     handleDisplayData(output); 
    } 
    else { 
     Acct[] accountArray = new Acct[((AccountData)output).getAccount().size()]; 
     mItems = ((AccountData)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(SrOutput 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; 
} 

}

我在外部類爲我calback方法以下方法簽名displayData

public abstract <T> void displayData(T output); 

現在我想擴展我的外部類d根據我期待的響應對象的類型對擴展類實施不同的displaydata方法。

當我在子類中,如下定義一個方法,我得到錯誤不有效超越:

@Override 
public void displayData(ServerOutput output){} 

的AsyncTask被調用爲:

new PerformOPTask<AccountData>(getString(R.string.get_account),AccountData.class).execute(); 

什麼是做到這一點的正確方法。我是新來的泛型的東西,所以我對這一切有點困惑。預先感謝您的幫助。

回答

1

對您的代碼進行此更改。

public abstract class BaseFragmentActivity<T> extends FragmentActivity{ 
public abstract void displayData(T output); 

class PerformOPTask extends AsyncTask<Void, String, T> 




/* ListAccountsActivity*/ 
public class ListAccountsActivity extends BaseListFragmentActivity<ServerOut> { 
+0

我不能讓FragmentActivity通用的,因爲這是不是我的控制,但我現在用 – sab

+0

抱歉改變it..Make BaseFragmentActivity庫通用的。 –

2

使用字符串,而不是在doInBackground虛空

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

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