繁体   English   中英

如何在加载列表视图之前显示加载栏?

[英]How to show Loading bar before loading listview?

这是我的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”,但是当我从主活动中单击“按钮”时,它只是停留在该主活动上,直到数据忙碌并且“在”加载OrderList活动之后才显示,但我不想要它。 当我单击MainActivity中的按钮时,第一整个空白页是否会显示带有加载栏,而当数据完全加载时,它会显示出来。

我不是Android的专业人士,我不知道我做对与错,所以如果有其他更好的有效方法来添加ListView,您可以在那里指导我。

我在您的代码中添加了几行代码,希望对您有所帮助...

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;
}
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM