I have a project in which i read data from SQL Server. In an Activity i try to read data from two DB and then filter one of them from the other and after all all display in ListView,it has done in onCreate method, but it takes some time,However its OK. Now i want to set an Update Button and exactly Call the same methods as in onCreate is. Well, cause of Processing time, i want to display a ProgressBar or ProgressDialog to the End-user. I know i should put my methods in Thread, and googled alot, but non of them worked. The ProgressBar disappear after all methods. This is my Last try that gives me java.lang.RuntimeException cause of calling Constructor i think,because it point to constructor in logCat : Here is what i have done in update Button. And This Class name is IceCream that i instance it here with the Constructor.
update.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick (View v){
progressBar.setVisibility(View.VISIBLE);
blackListPartCodes.clear();
typeArray.clear();
typeAdapter.clear();
handler = new Handler() {
@Override
public void handleMessage(Message msg)
{
progressBar.setVisibility(View.GONE);
super.handleMessage(msg);
}
};
progressBar.setVisibility(View.VISIBLE);
ConnectionHelper blackListConnection = new ConnectionHelper();
CallableStatement callableStatement;
try {
callableStatement = blackListConnection.getConnection().prepareCall("{call SpGetAllIceBlackListProduct}");
callableStatement.execute();
ResultSet resultSet = callableStatement.getResultSet();
if(resultSet.next()) {
do {
String partCode = new String(resultSet.getString("PartCode"));
blackListPartCodes.add(partCode);
}while (resultSet.next());
}
blackListConnection.closeConnection();
resultSet.close();
callableStatement.close();
} catch (SQLException e) {
e.printStackTrace();
}catch (Exception e) {
Toast.makeText(getApplicationContext(), "Try Again",Toast.LENGTH_SHORT).show();
}
new Thread() {
public void run() {
Statement statement = null;
String query;
query = "MyQuery";
try {
ConnectionHelper connectionHelper2 = new ConnectionHelper();
statement = connectionHelper2.getConnection().createStatement();
ResultSet resultSet = statement.executeQuery(query);
if(resultSet.next()) {
do {
setPartName(resultSet.getString("XX"));
setPartCode(resultSet.getString("XX"));
setPrice(resultSet.getString("XX"));
setUnitName(resultSet.getString("XX"));
IceCream iceCream = new IceCream(getPartName(), getPartCode(), getUnitName(), getPrice());
iceCreams.add(iceCream);
} while (resultSet.next());
}
connectionHelper2.closeConnection();
statement.close();
resultSet.close();
}
catch (SQLException e) {
}catch (Exception e) {
Toast.makeText(getApplicationContext(), "Try Again",Toast.LENGTH_SHORT).show();
}
handler.sendEmptyMessage(0);
}
}.start();
checkMatches(iceCreams, blackListPartCodes);
for(int i = 0; i<iceCreams.size(); i++) {
typeArray.add(iceCreams.get(i).getPartName());
typeAdapter.getItemViewType(R.id.listView);
iceCreamTypeList.setAdapter(typeAdapter);
}
}
}
);
First, it gives me java.lang.RuntimeException: Can't create handler inside thread in the second Connection(Connection2) and the Line `
catch (Exception e) {
Toast.makeText(getApplicationContext(), "Try Again",Toast.LENGTH_SHORT).show();
}
And after i removed it, it gives me error in Constructor of IceCreame. This Line :
IceCream iceCream = new IceCream(getPartName(), getPartCode(), getUnitName(), getPrice());
Note that this class name is IceCream. Any help will Appreciate. I Googled alot, but to chance, where i am doing wrong?</b>
What I have tried:
I have tried java.os.Handle also, but no chance.
Any Solution?