Android Studio Mobile Coding ERROR

JC724

Weaksauce
Joined
Jan 20, 2016
Messages
118
So, I am trying to write up some code where I connect TO MS SQL SERVER, which I did get to work.



I know this worked as I was able to test what was in my database by querying it. It shows a successful connection when I query something that is there and when something isn’t is says invalid credentials.



The problem is after that the next thing I wanted to test, was setText in the editText fields right after I do a successful query, but I keep getting an error. Saying something about it" must be apart of the main thread" but the code is IN the same thread.

I even put two test cases in different spots IN the thread and it doesn’t work. I commented it out and made code bigger/bold below. Can someone please help me and tell me why I can’t set The Text in the editText field in the UI, and please give me a possible solution.



I need to edit the text fields once I do a successful query with the data in the MS SQL database/table.


public class MainActivity extends Activity
{
ConnectionClass connectionClass;
EditText edtuserid,edtpass,edBrFist,edBrFeet;
Button btnlogin;
ProgressBar pbbar;

String hold = "1";

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

connectionClass = new ConnectionClass();
edBrFist =(EditText) findViewById(R.id.brainSfistText);
edBrFeet =(EditText) findViewById(R.id.brainSfeetText);
edtuserid = (EditText) findViewById(R.id.et_username);
edtpass = (EditText) findViewById(R.id.et_password);
btnlogin = (Button) findViewById(R.id.btn_Login);
pbbar = (ProgressBar) findViewById(R.id.pbbar);
pbbar.setVisibility(View.GONE);





btnlogin.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {

DoLogin doLogin = new DoLogin();
doLogin.execute("");


}
});

}


public class DoLogin extends AsyncTask<String,String,String>
{
String z = "";
Boolean isSuccess = false;


String userid = edtuserid.getText().toString();
String password = edtpass.getText().toString();


@Override
protected void onPreExecute() {
pbbar.setVisibility(View.VISIBLE);
}

@Override
protected void onPostExecute(String r) {
pbbar.setVisibility(View.GONE);
Toast.makeText(MainActivity.this,r,Toast.LENGTH_SHORT).show();

if(isSuccess) {
Toast.makeText(MainActivity.this,r,Toast.LENGTH_SHORT).show();
}

}

@Override
protected String doInBackground(String... params) {
if(userid.trim().equals("")|| password.trim().equals(""))
z = "Please enter User Id and Password";
else
{
try {
Connection con = connectionClass.CONN();
if (con == null) {
z = "Error in connection with SQL server";
} else {
String query = "select * from BraiNetDataSet where UserID='" + userid + "'";
Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery(query);

if(rs.next())
{

z = "Login successfull";

// edBrFeet.setText("Test1");


isSuccess=true;
}
else
{
z = "Invalid Credentials";
isSuccess = false;
}

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

return z;
}

}

// edBrFist.setText("Test2");
}
 
I recommend stackexchange for extremely in-depth java and android development questions. :android: unlikely that anyone here has quite the android experience to help you out. :(

But I know your pain.... Android development is hard stuff these days.
 
Back
Top