这是我的java代码:
public class OrderList extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener {
String partyCondition;
ListView lstOrder;
SimpleAdapter ADAhere;
ConnectionClass connectionclass;
String call, db, un, passwords;
Connection connect;
ResultSet rs, statusResult;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.orderlist_main);
partyCondition = getIntent().getStringExtra("party_condition");
lstOrder = findViewById(R.id.order_list);
connectionclass = new ConnectionClass();
call = connectionclass.getip();
un = connectionclass.getun();
passwords = connectionclass.getpassword();
db = connectionclass.getdb();
connect = ConnectionClass.CONN(un, passwords, db, call);
}
public void settingData(){
if (connect != null){
String querycmd = "select GLNAME, convert(varchar(11), ORDDATE, 100) as ORDDATE, " +
"CONTSIZE, ORDNO, sum(LOADABLITY) 'LOADABLITY', BLDATE " +
"from ORDTBL" + partyCondition +
" group by glname, orddate, contsize, ordno, bldate";
try {
Statement statement = connect.createStatement();
rs = statement.executeQuery(querycmd);
List<Map<String, String>> data = null;
data = new ArrayList<Map<String, String>>();
while (rs.next()) {
Map<String, String> datanum = new HashMap<String, String>();
datanum.put("oName", rs.getString("ORDNO"));
datanum.put("oDate", "Date: " + rs.getString("ORDDATE"));
datanum.put("oContainer", "Container: " + rs.getString("CONTSIZE"));
datanum.put("pName", rs.getString("GLNAME"));
String status = "SELECT\n" +
" ORDTBL.GLNAME\n" +
" ,ORDTBL.ORDNO\n" +
" ,ORDTBL.QTY as ORD_QTY\n" +
" ,SALTBL.QTY as SAL_QTY\n" +
"FROM dbo.ORDTBL\n" +
"LEFT OUTER JOIN dbo.SALTBL\n" +
" ON ORDTBL.ORDNO = SALTBL.ORDNO\n" +
" AND ORDTBL.IALIAS = SALTBL.IALIAS\n" +
"where ORDTBL.ORDNO = '" + rs.getString("ORDNO") + "'";
datanum.put("oSatus", statusFinder(status));
data.add(datanum);
}
String[] storageID = { "oName", "oDate", "oSatus", "oContainer", "pName" };
int[] partyData = { R.id.order_name, R.id.order_date, R.id.order_status, R.id.order_container, R.id.olt_party_name };
ADAhere = new SimpleAdapter(OrderList.this, data,R.layout.orderlist_template, storageID, partyData);
lstOrder.setAdapter(ADAhere);
} catch (SQLException e) {
Toast.makeText(OrderList.this, e.getMessage(),Toast.LENGTH_LONG).show();
}
} else {
Toast.makeText(OrderList.this, "Database connection problem",Toast.LENGTH_LONG).show();
}
}
public String statusFinder(String qry) throws SQLException {
Statement statementTwo = connect.createStatement();
statusResult = statementTwo.executeQuery(qry);
int rows = 0;
int succ = 0, lose = 0;
if(!statusResult.next()){
return "No record";
}
while (statusResult.next()){
rows++;
int a = statusResult.getInt("ORD_QTY");
int b = statusResult.getInt("SAL_QTY");
if (a == b){
succ++;
}else {
lose++;
}
}
if (succ == rows){
return "Complate";
}else if (succ == 0){
return "Pending";
}else {
return "Partiallly Pending";
}
}
@Override
public void onBackPressed() {
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
if (drawer.isDrawerOpen(GravityCompat.START)) {
drawer.closeDrawer(GravityCompat.START);
} else {
super.onBackPressed();
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
@SuppressWarnings("StatementWithEmptyBody")
@Override
public boolean onNavigationItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.nav_customer_list) {
Intent intent = new Intent(OrderList.this, MainActivity.class);
startActivity(intent);
finish();
} else if (id == R.id.nav_orders) {
} else if (id == R.id.nav_outstanding) {
} else if (id == R.id.nav_setting) {
} else if (id == R.id.nav_logout) {
}
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}
}
这是第二个Activity,第一个是MainActivity。
我希望在加载活动时实现加载栏,
我尝试过“AsyncTask”,但是当我从主要活动中点击Button时,它只是停留在主要活动上,直到数据变胖并且“加载”它显示的OrderList活动之后但我不想要它。 是否有可能当我点击MainActivity中的按钮时,第一个完整的空白页面将显示加载栏,当数据完全加载时,它会显示出来。
我不是Android的专业人士,我不知道我做错了所以如果有更好的有效方式添加ListView你可以指导我。
答案 0 :(得分:0)
我在您的代码中添加了几行代码,希望它对您有所帮助......
public void settingData(){
final ProgressDialog progressDialog = new ProgressDialog(getActivity());
progressDialog.setMessage("Loading...");
progressDialog.show();
if (connect != null){
String querycmd = "select GLNAME, convert(varchar(11), ORDDATE, 100) as ORDDATE, " +
"CONTSIZE, ORDNO, sum(LOADABLITY) 'LOADABLITY', BLDATE " +
"from ORDTBL" + partyCondition +
" group by glname, orddate, contsize, ordno, bldate";
try {
Statement statement = connect.createStatement();
rs = statement.executeQuery(querycmd);
List<Map<String, String>> data = null;
data = new ArrayList<Map<String, String>>();
while (rs.next()) {
progressDialog.dismiss();
Map<String, String> datanum = new HashMap<String, String>();
datanum.put("oName", rs.getString("ORDNO"));
datanum.put("oDate", "Date: " + rs.getString("ORDDATE"));
datanum.put("oContainer", "Container: " + rs.getString("CONTSIZE"));
datanum.put("pName", rs.getString("GLNAME"));
String status = "SELECT\n" +
" ORDTBL.GLNAME\n" +
" ,ORDTBL.ORDNO\n" +
" ,ORDTBL.QTY as ORD_QTY\n" +
" ,SALTBL.QTY as SAL_QTY\n" +
"FROM dbo.ORDTBL\n" +
"LEFT OUTER JOIN dbo.SALTBL\n" +
" ON ORDTBL.ORDNO = SALTBL.ORDNO\n" +
" AND ORDTBL.IALIAS = SALTBL.IALIAS\n" +
"where ORDTBL.ORDNO = '" + rs.getString("ORDNO") + "'";
datanum.put("oSatus", statusFinder(status));
data.add(datanum);
}
String[] storageID = { "oName", "oDate", "oSatus", "oContainer", "pName" };
int[] partyData = { R.id.order_name, R.id.order_date, R.id.order_status, R.id.order_container, R.id.olt_party_name };
ADAhere = new SimpleAdapter(OrderList.this, data,R.layout.orderlist_template, storageID, partyData);
lstOrder.setAdapter(ADAhere);
} catch (SQLException e) {
Toast.makeText(OrderList.this, e.getMessage(),Toast.LENGTH_LONG).show();
}
} else {
Toast.makeText(OrderList.this, "Database connection problem",Toast.LENGTH_LONG).show();
}
}
public String statusFinder(String qry) throws SQLException {
Statement statementTwo = connect.createStatement();
statusResult = statementTwo.executeQuery(qry);
int rows = 0;
int succ = 0, lose = 0;
if(!statusResult.next()){
return "No record";
}
while (statusResult.next()){
rows++;
int a = statusResult.getInt("ORD_QTY");
int b = statusResult.getInt("SAL_QTY");
if (a == b){
succ++;
}else {
lose++;
}
}
if (succ == rows){
return "Complate";
}else if (succ == 0){
return "Pending";
}else {
return "Partiallly Pending";
}
}
@Override
public void onBackPressed() {
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
if (drawer.isDrawerOpen(GravityCompat.START)) {
drawer.closeDrawer(GravityCompat.START);
} else {
super.onBackPressed();
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
@SuppressWarnings("StatementWithEmptyBody")
@Override
public boolean onNavigationItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.nav_customer_list) {
Intent intent = new Intent(OrderList.this, MainActivity.class);
startActivity(intent);
finish();
} else if (id == R.id.nav_orders) {
} else if (id == R.id.nav_outstanding) {
} else if (id == R.id.nav_setting) {
} else if (id == R.id.nav_logout) {
}
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}
}