AsyncTask没有返回值

时间:2019-02-27 00:43:43

标签: android arraylist android-asynctask

代码1在主要活动上运行时有效,但是在访问SQL数据库时会创建黑色黑屏。

为了防止黑屏,我将SQL任务移到了AsyncTask,但是从查询中获得的数据没有传递到适配器。 我没有得到任何结果(代码2)。 我不知道怎么了。 请帮忙。

代码1

public class CommentsView extends AppCompatActivity {

ArrayList<CommentObject> datamodels;
ListView listview;
ConnectionClassBatchRecord connectionClassBR;
String rcomments,rdoneby,rdateby,stepid,z;
private static CommentsListAdapter commentsAdapter;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_comments_view);
    connectionClassBR = new ConnectionClassBatchRecord();
    datamodels=new ArrayList<>();
    String step_Id = (String) getIntent().getExtras().get("Step_Id");
    stepid=step_Id;

    try {
        Connection con = connectionClassBR.CONN();
        if (con == null) {
            Log.v(TAG,"Error in connection with SQL server");
        } else {

            String query ="SELECT * FROM comments WHERE Step_Id='"+stepid+"'";
            PreparedStatement preparedStatement = con.prepareStatement(query);
            ResultSet rs =preparedStatement.executeQuery();

            while (rs.next()) {
                rcomments = rs.getString("Comment");
                rdoneby=rs.getString("DoneBy");
                rdateby=rs.getString("DoneByDate");
                CommentObject commentobject=new CommentObject(rcomments,rdoneby,rdateby);
                datamodels.add(commentobject);


            }
        }
    } catch (Exception ex) {
        //z = "Exceptions";
        z=ex.getMessage();
        Log.v(TAG,"Exceptionmessage"+z);
    }

    listview = (ListView) findViewById(R.id.commentslist);
    CommentsListAdapter commentsadapter = new CommentsListAdapter(this, datamodels);
    listview.setAdapter(commentsadapter);

}

代码2

public class CommentsView extends AppCompatActivity {

ArrayList<CommentObject> datamodels;
ListView listview;
ConnectionClassBatchRecord connectionClassBR;
String rcomments,rdoneby,rdateby,stepid,z;
private static CommentsListAdapter commentsAdapter;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_comments_view);
    connectionClassBR = new ConnectionClassBatchRecord();
    datamodels=new ArrayList<>();
    String step_Id = (String) getIntent().getExtras().get("Step_Id");
    stepid=step_Id;

    FindcommentsAsyncTask findcommentsAsyncTask=new FindcommentsAsyncTask(this);
    findcommentsAsyncTask.execute(stepid);

    listview = (ListView) findViewById(R.id.commentslist);
    CommentsListAdapter commentsadapter = new CommentsListAdapter(this, datamodels);
    listview.setAdapter(commentsadapter);

}

private class FindcommentsAsyncTask extends AsyncTask<String,Void,ArrayList<CommentObject>> {

    String z,comm,sid;
    ResultSet result_rs;

    private ProgressDialog dialog;
    private Context context;
    private Activity activity;
    public FindcommentsAsyncTask(Activity activity) {
        this.activity = activity;
        this.context = activity;
        this.dialog = new ProgressDialog(context);
    }

    @Override
    protected void onPreExecute() {
        this.dialog.setMessage("Retrieving Data");
        this.dialog.show();
    }

    @Override
    protected ArrayList<CommentObject> doInBackground(String... params) {

        sid=params[0];

        try {
            Connection con = connectionClassBR.CONN();
            if (con == null) {
                Log.v(TAG,"Error in connection with SQL server");
            } else {

                String query ="SELECT * FROM comments WHERE Step_Id='"+sid+"'";
                PreparedStatement preparedStatement = con.prepareStatement(query);
                ResultSet rs =preparedStatement.executeQuery();

                while (rs.next()) {
                    rcomments = rs.getString("Comment");
                    rdoneby=rs.getString("DoneBy");
                    rdateby=rs.getString("DoneByDate");
                    CommentObject commentobject=new CommentObject(rcomments,rdoneby,rdateby);
                    datamodels.add(commentobject);


                }
            }
        } catch (Exception ex) {
            //z = "Exceptions";
            z=ex.getMessage();
            Log.v(TAG,"Exceptionmessage"+z);
        }

        return datamodels;
    }

    @Override
    protected void onPostExecute(ArrayList<CommentObject> mresult_rs)
    {
        passdata(mresult_rs);
        if (dialog.isShowing()) {
            dialog.dismiss();
        }
    }
}

private void passdata(ArrayList<CommentObject> mresult_rs){

    if (mresult_rs == null) {
        Toast.makeText(getApplication(), "No comments Found", Toast.LENGTH_LONG).show();
    } else {
        datamodels=mresult_rs;
    }
}

}

1 个答案:

答案 0 :(得分:0)

问题是onPostExecute接收到一个空数组?还是从不填充listView? 我在这里看到的是线程争用,而您的AsyncTask仍在获取信息,而适配器使用空数组填充列表。并且,当Asynctask完成时,它将结果加载到数据模型上,但从不通知适配器数据已更改。 要么在passdata上调用适配器的更新,要么在passdata上创建适配器,然后一切都会正常工作。