Issue
Code 1 works when is running on the main activity but it creates a short black screen while accessing the SQL database.
To prevent the black screen I moved the SQL task to an AsyncTask but the data obtained from the query is not being passed to the adapter. I get no result out of it (Code 2). I can't figure out what is wrong. Please help.
Code 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);
}
Code 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;
}
}
}
Solution
The problem is that onPostExecute receives a null array? Or that the listView is never populated? What I see here is a race of threads, while your AsyncTask is still fetching info, the adapter is using an empty array to fill the list. And when the Asynctask finishes it loads the results on datamodels but never notifies the adapter that the data has changed. Either call an update on the adapter on passdata or create the adapter in passdata, then everything will work correctly.
Answered By - Sander Rito
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.