无法扩展ListActivity和AsyncTask

时间:2012-02-29 09:46:46

标签: android android-asynctask extends

由于java不支持多重继承,因此我无法解决此问题。

这就是我想要做的事情:

public class CallForwardActivity extends ListActivity /* This is there I want to extend AsyncTask */ implements OnSharedPreferenceChangeListener
{

... // creating and initializing stuff

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

    userInfo = this.getSharedPreferences(USERINFO_FILE, 0);
    userInfo.registerOnSharedPreferenceChangeListener(this);
    userControl = new UserController(context);
    ListView lv = getListView();
    lv.setTextFilterEnabled(true);
    //setListAdapter(new ArrayAdapter<String>(this, R.layout.callforward_items, R.id.callforward_item_text, callforwardLabels));

    list = new ArrayList<HashMap<String,String>>();

    callForwardList = new SimpleAdapter( 
            this, 
            list,
            R.layout.callforward_items,
            new String[] { "line1","line2" },
            new int[] { R.id.callforward_item_text, R.id.callforward_number } );



    populateList();
    setListAdapter( callForwardList );

    lv.setOnItemClickListener(new OnItemClickListener() {

    public void onItemClick(AdapterView<?> parent, View view, int position, long id) 
    {
        CustomDialog(); // this will make the asynchronous task call if the user presses "OK" within the dialog box.
    }

  });

}

如何在不扩展AsyncTask的情况下使调用异步?

3 个答案:

答案 0 :(得分:4)

只需创建另一个扩展AsyncTask的类,并在ListActivity中使用它。如果你愿意,你可以把它变成内在的。希望这会有所帮助。

答案 1 :(得分:1)

errrmmm ....你无法在java中扩展两个类,你可以实现许多接口但扩展一个类。你应该创建另一个说内部类,它扩展了AsyncTask&lt; ..,..,..&gt;并在那里做你的背景工作。

有关java中多重继承的更多信息 http://java.sys-con.com/node/37748

答案 2 :(得分:0)

你试图使用扩展asynctask的活动是什么用例,asynctask用于后台进程,Acitivity组件用于提供UI

您正在尝试使用UIThread扩展BackgroundThread,这意味着您正在建立一种类似&#39; UIThread is-a BackgroundThread&#39; 的关系。

我觉得还有另一种解决你的用例的方法。