示例中的错误(AsyncTask)

时间:2013-03-25 11:54:01

标签: android

我从http://appfulcrum.com/2011/09/21/listview-example-5-using-sqlite-database/做了一个例子 代码中有一个错误

package com.appfulcrum.blog.examples.listviewcustomdb;

import android.app.ListActivity;
import android.database.Cursor;
import android.database.SQLException;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ListView;
import android.widget.Toast;

public class CustomListViewDB extends ListActivity {

private ListView mainListView = null;
CustomSqlCursorAdapter adapter = null;
private SqlHelper dbHelper = null;
private Cursor currentCursor = null;

private ListView listView = null;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.simple);

    if (this.dbHelper == null) {
        this.dbHelper = new SqlHelper(this);

    }

    listView = getListView();
    listView.setItemsCanFocus(false);
    listView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);

    Button btnClear = (Button) findViewById(R.id.btnClear);
    btnClear.setOnClickListener(new OnClickListener() {

        public void onClick(View v) {

            Toast.makeText(getApplicationContext(),
                    " You clicked Clear button", Toast.LENGTH_SHORT).show();
            ClearDBSelections();

        }
    });

    new SelectDataTask().execute();

    this.mainListView = getListView();

    mainListView.setCacheColorHint(0);

}

@Override
protected void onRestart() {
    super.onRestart();
    new SelectDataTask().execute();
}

@Override
protected void onPause() {

    super.onPause();
    this.dbHelper.close();
}

protected void ClearDBSelections() {

    this.adapter.ClearSelections();

}

private class SelectDataTask extends AsyncTask<string, void,="" string=""> {

    protected void onPreExecute() {
        // this.dialog.setMessage("Getting Names...");
        // this.dialog.show();
    }

    protected String doInBackground(final String... args) {

        try {

            CustomListViewDB.this.dbHelper.createDatabase();
            CustomListViewDB.this.dbHelper.openDataBase();

            CustomListViewDB.this.currentCursor = CustomListViewDB.this.dbHelper
                    .getCursor();

        } catch (SQLException sqle) {

            throw sqle;

        }
        return null;
    }

    // can use UI thread here
    protected void onPostExecute(final String result) {

        startManagingCursor(CustomListViewDB.this.currentCursor);
        int[] listFields = new int[] { R.id.txtTitle };
        String[] dbColumns = new String[] { SqlHelper.COLUMN_TITLE };

        CustomListViewDB.this.adapter = new CustomSqlCursorAdapter(
                CustomListViewDB.this, R.layout.single_item,
                CustomListViewDB.this.currentCursor, dbColumns, listFields,
                CustomListViewDB.this.dbHelper);
        setListAdapter(CustomListViewDB.this.adapter);

    }
}

}
行中的

错误

private class SelectDataTask extends AsyncTask<string, void,="" string=""> {

如何写这行没有错误? Eclipse仍然在发誓

new SelectDataTask().execute();

写道“方法execute()未定义类型CustomListViewDB.SelectDataTask”

1 个答案:

答案 0 :(得分:2)

在Java中,您无法为变量设置默认值,这不是C#。

应该是:

private class SelectDataTask extends AsyncTask<String, Void, String> {

此外,Java中的String - string等类型没有别名。