AsrsorTask中的PreparedStatement上的游标实现

时间:2017-01-13 20:32:15

标签: java android-asynctask cursor prepared-statement implements

所以我正在尝试向我的类实现一个Cursor,这样我就可以填充我的数据网格(不确定这是否是正确的方法,当我需要使用可调用的SP和预处理语句时)我从这个{ {3}}但我得到了

  

DataGridActivity.Itemnumber不是抽象的,并且不会覆盖Cursor中的抽象方法响应(Bundle)

我不知道如何在这里实现抽象方法 normaly使用getstring方法为我准备好的语句想知道是否有某种方式我可以这样做而不是填充我的网格而不是使用光标

public class Itemnumber extends AsyncTask<String,String,String> implements Cursor {
    String z = "";
    @Override
    protected void onPreExecute() {
    }
    @Override
    protected void onPostExecute(String r) {
    }

    @Override
    protected String doInBackground(String... params) {
        try {
            Connection con = connectionClass.CONN();
            if (con == null) {
                z = "Error in connection with SQL server";
            } else {
                PreparedStatement preparedStatement = null;
                String sqli = "select ID,ItemNumber,Trashed,Sold from [file].[Item] where [ItemNumber] =?";
                preparedStatement = con.prepareStatement(sqli);
                preparedStatement.setString(1, "test");
                ResultSet rs = preparedStatement.executeQuery();
                if (rs.next()) {
                } else {
                }
            }
        } catch (Exception ex) {
            z = "Exceptions";
        }
        return z;
    }
    @Override
    public Bundle respond(Bundle extras){
        moveToFirst();
        return Bundle.EMPTY;
    }
}

我的Datagrid活动类

Cursor csr = new Itemnumber();
//create DataTable object
DataTable dtDataSource = new DataTable();
//define column
dtDataSource.addAllColumns(new String[]{"column_1", "column_2","column_3", "column_4});
//create DataRow
DataTable.DataRow drRow;

//populate data from cursor into DataSource
if(csr.moveToFirst()){
    do{
        drRow = dtDataSource.newRow();
        drRow.set("column_1", csr.getString(csr.getColumnIndex("field_1")));
        drRow.set("column_2", csr.getString(csr.getColumnIndex("field_2")));
        drRow.set("column_2", csr.getString(csr.getColumnIndex("field_3")));
        drRow.set("column_4", csr.getString(csr.getColumnIndex("field_4")));

        dtDataSource.add(drRow);
    } while(csr.moveToNext());
    csr.close();
}        
/**
 *  Prepare the DataGrid
 */
//initialize DataGrid
DataGrid dg = (DataGrid)findViewById(R.id.datagrid);
//define column style, bond each DataGrid column by DataTable column
dg.addColumnStyles(new DataGrid.ColumnStyle[]{
        new DataGrid.ColumnStyle(getString(R.string.ID), "column_1", 80),
        new DataGrid.ColumnStyle(getString(R.string.ItemNumber), "column_2", 120),
        new DataGrid.ColumnStyle(getString(R.string.Trashed), "column_3", 100),
        new DataGrid.ColumnStyle(getString(R.string.Sold), "column_4", 150)    
});
//set the DataTable as source
dg.setDataSource(dtDataSource);
//generate the DataGrid
dg.refresh();

2 个答案:

答案 0 :(得分:0)

您必须覆盖Itemnumber课程中的最后一个方法。 此方法位于interface Cursor中,并命名为:respond(Bundle)

因此,如果您的班级Itemnumber已实施Cursor,请添加

@Override
Bundle respond(Bundle extras){
   return Bundle.EMPTY; 
}

并实施,您的方法必须返回

答案 1 :(得分:0)

我想出了如何使用我的准备声明来做到这一点。

由于实现所有抽象方法和接口似乎没有接近逻辑

public class DataGridActivity extends Activity {
    /** Called when the activity is first created. */
    ConnectionClass connectionClass;
    //define column
    DataTable.DataRow drRow;
    DataTable dtDataSource = new DataTable();
    Button btnsearch;
    DataGrid dg;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
       setContentView(R.layout.datagridt);
        dg = (DataGrid)findViewById(R.id.datagrid);
        btnsearch = (Button) findViewById(R.id.btnsearch);
        connectionClass = new ConnectionClass(this.getApplicationContext());


        /**
         *  Prepare the DataGrid
         */
        //initialize DataGrid
        //define column style, bond each DataGrid column by DataTable column
                dg.addColumnStyles(new DataGrid.ColumnStyle[]{
                new DataGrid.ColumnStyle(getString(R.string.dito_nr), "column_1", 80),
                new DataGrid.ColumnStyle(getString(R.string.Biltype), "column_2", 120),
                new DataGrid.ColumnStyle(getString(R.string.kort_nr), "column_3", 100),
                new DataGrid.ColumnStyle(getString(R.string.Del_Type), "column_4", 150)

        });

    }


    public class Itemnumber extends AsyncTask<String,String,String> {
        String z = "";
        @Override
        protected void onPreExecute() {
            dtDataSource.addAllColumns(new String[]{"column_1", "column_2","column_3", "column_4"});
            drRow = dtDataSource.newRow();
            dtDataSource.add(drRow);
            dg.setDataSource(dtDataSource);
            dg.refresh();
        }
        @Override
        protected void onPostExecute(String r) {
        }

        @Override
        protected String doInBackground(String... params) {
            try {
                Connection con = connectionClass.CONN();
                if (con == null) {
                    z = "Error in connection with SQL server";
                } else {
                    PreparedStatement preparedStatement = null;
                    String sqli = "select ID,ItemNumber,Trashed,Sold from [file].[Item]";
                    preparedStatement = con.prepareStatement(sqli);
                    ResultSet rs = preparedStatement.executeQuery();
                    if (rs.next()) {
                        //create DataRow
                        drRow.set("column_1", rs.getString(1));
                        drRow.set("column_2", rs.getString(2));
                        drRow.set("column_3", rs.getString(3));
                        drRow.set("column_4", rs.getString(4));

                    } else {
                    }

                }
            } catch (Exception ex) {
                z = "Exceptions";
            }
            return z;

        }
    }

    public void btnsearch (View view) {
        // TODO Auto-generated method stub

        Itemnumber item = new Itemnumber();
        item.execute("");
    }
}