如何在插入我的列表视图数据后显示进度对话框

时间:2017-08-10 05:10:39

标签: java android sql-server

如何在插入我的列表视图后显示进度对话框数据如何在我的代码中使用进度对话框。 在分配lisview行后,如何显示进度对话框

 public class MainActivity extends AppCompatActivity {
    Connection_Class connectionClass;
    SimpleAdapter simpleAdapter;
    List<Map<String, String>> data = null;
    String cc,a,s;
    String mira,shef,session;
    String accno1;
    List<String> ppp3 = new ArrayList<String>();
    ListView listView;
    ProgressBar progressBar;
    EditText almirah,shelf,accession_no;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        connectionClass = new Connection_Class();
        Button add=(Button)findViewById(R.id.add);
        Button submit=(Button)findViewById(R.id.submit);
        Button clear=(Button)findViewById(clear);
        almirah=(EditText)findViewById(R.id.almirah);
        shelf=(EditText)findViewById(R.id.shelf);
        accession_no=(EditText)findViewById(R.id.accession_no);
        listView =(ListView)findViewById(R.id.list);
        data = new ArrayList<Map<String, String>>();
        progressBar=(ProgressBar)findViewById(R.id.proress);


        simpleAdapter = new SimpleAdapter(getApplicationContext(),data,listview, new String[]{"A", "B", "C"}, new int[]{al, sh, acc});
        listView.setAdapter(simpleAdapter);



        add.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (almirah.length()==0||shelf.length()==0||accession_no.length()==0){
                    Toast.makeText(MainActivity.this, "Please Fill Full Details", Toast.LENGTH_SHORT).show();
                }
                else {
                    Map<String, String> datanum = new HashMap<String, String>();
                    String alm = almirah.getText().toString();
                    String she = shelf.getText().toString();
                    String accno = accession_no.getText().toString();
                    datanum.put("A", alm);
                    datanum.put("B", she);
                    datanum.put("C", accno);
                    data.add(datanum);
                    simpleAdapter.notifyDataSetChanged();
                    accession_no.setText("");
                }
            }

        });



        submit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
               submit_btn();
            }
        });

        clear.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                almirah.setText("");
                shelf.setText("");
                accession_no.setText("");
                List<Map<String, String>> data2 = null;
                data = new ArrayList<Map<String, String>>();
             SimpleAdapter   simpleAdapter2 = new SimpleAdapter(getApplicationContext(),data,listview, new String[]{"A", "B", "C"}, new int[]{al, sh, acc});
                listView.setAdapter(simpleAdapter2);
                finish();
                startActivity(getIntent());

            }
        });
    }

    public void submit_btn(){

        if (data.size() == 0) {
            Toast.makeText(MainActivity.this, "Your List Is Empty Please Fill First", Toast.LENGTH_SHORT).show();
        } else {
//                    final ProgressDialog progressdialog = new ProgressDialog(MainActivity.this);
//                    progressdialog.setMessage("Please Wait....");
//                    progressdialog.show();

            for (int i = 0; i < data.size(); i++) {
                HashMap<String, Object> obj = (HashMap<String, Object>) listView.getAdapter().getItem(i);
                a = (String) obj.get("A");
                s = (String) obj.get("B");
                cc = (String) obj.get("C");

                try {
                    Connection con = connectionClass.CONN();
                    if (con == null) {
                        Toast.makeText(getApplicationContext(), "Error in connection with SQL server", Toast.LENGTH_LONG).show();
                    } else {

                        try {
                            con = connectionClass.CONN();
                            if (con == null) {
                                Toast.makeText(getApplicationContext(), "Error in connection with SQL server", Toast.LENGTH_LONG).show();

                            } else {
                                String query = "SELECT AccessionNo FROM suigen.TransAlmiraShelf WHERE AccessionNo='" + String.valueOf(cc) + "'";
                                Statement stmt = con.createStatement();
                                ResultSet rs = stmt.executeQuery(query);
                                while (rs.next()) {
                                    ppp3.add(rs.getString("AccessionNo"));


                                    StringBuilder builder3 = new StringBuilder();
                                    for (String value : ppp3) {
                                        builder3.append(value);
                                    }
                                    accno1 = builder3.toString();
                                }
                            }
                        } catch (Exception ex) {
                            Toast.makeText(getApplicationContext(), "Exceptions", Toast.LENGTH_LONG).show();
                        }
                        if (cc.equals(accno1)) {
                            try {
                                con = connectionClass.CONN();
                                if (con == null) {
                                    Toast.makeText(getApplicationContext(), "Error in connection with SQL server", Toast.LENGTH_LONG).show();
                                } else {
                                    String query = "UPDATE suigen.TransAlmiraShelf SET Almira='"+String.valueOf(a)+"',Shelf='"+String.valueOf(s)+"' WHERE AccessionNo='" + cc + "'";
                                    Statement stmt = con.createStatement();
                                    stmt.execute(query);
                                }
                            } catch (Exception ex) {
                                Toast.makeText(getApplicationContext(), "Exceptions", Toast.LENGTH_LONG).show();
                            }
                        } else {
                            String query = "INSERT INTO suigen.TransAlmiraShelf (Almira, Shelf, AccessionNo) VALUES('" + String.valueOf(a) + "','" + String.valueOf(s) + "','" + String.valueOf(cc) + "')";
                            Statement stmt = con.createStatement();
                            stmt.execute(query);

                        }
                    }
                } catch (Exception ex) {
                    Toast.makeText(getApplicationContext(), "Exceptions", Toast.LENGTH_LONG).show();
                }
            }
            Toast.makeText(MainActivity.this, "Your Data Successfully Saved", Toast.LENGTH_LONG).show();
            almirah.setText("");
            shelf.setText("");
            accession_no.setText("");
            finish();
            startActivity(getIntent());
        }
    }
}

1 个答案:

答案 0 :(得分:0)

尝试上面这可能有帮助

 public void submit_btn(){
    AsyncTask<Integer,Void,Void> task = new AsyncTask<Integer, Void, Void>() {


        ProgressDialog progressDialog;

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            progressDialog = new ProgressDialog(MainActivity.this);
            progressDialog.setMessage("Loading Please Wait!!");
            progressDialog.setCancelable(false);
            progressDialog.show();
        }

        @Override
        protected Void doInBackground(Integer... integers) {

            if (data.size() == 0) {
                Toast.makeText(MainActivity.this, "Your List Is Empty Please Fill First", Toast.LENGTH_SHORT).show();
            } else {

                for (int i = 0; i < data.size(); i++) {
                    HashMap<String, Object> obj = (HashMap<String, Object>) listView.getAdapter().getItem(i);
                    a = (String) obj.get("A");
                    s = (String) obj.get("B");
                    cc = (String) obj.get("C");

                    try {
                        Connection con = connectionClass.CONN();
                        if (con == null) {
                            Toast.makeText(getApplicationContext(), "Error in connection with SQL server", Toast.LENGTH_LONG).show();
                        } else {

                            try {
                                con = connectionClass.CONN();
                                if (con == null) {
                                    Toast.makeText(getApplicationContext(), "Error in connection with SQL server", Toast.LENGTH_LONG).show();

                                } else {
                                    String query = "SELECT AccessionNo FROM suigen.TransAlmiraShelf WHERE AccessionNo='" + String.valueOf(cc) + "'";
                                    Statement stmt = con.createStatement();
                                    ResultSet rs = stmt.executeQuery(query);
                                    while (rs.next()) {
                                        ppp3.add(rs.getString("AccessionNo"));


                                        StringBuilder builder3 = new StringBuilder();
                                        for (String value : ppp3) {
                                            builder3.append(value);
                                        }
                                        accno1 = builder3.toString();
                                    }
                                }
                            } catch (Exception ex) {
                                Toast.makeText(getApplicationContext(), "Exceptions", Toast.LENGTH_LONG).show();
                            }
                            if (cc.equals(accno1)) {
                                try {
                                    con = connectionClass.CONN();
                                    if (con == null) {
                                        Toast.makeText(getApplicationContext(), "Error in connection with SQL server", Toast.LENGTH_LONG).show();
                                    } else {
                                        String query = "UPDATE suigen.TransAlmiraShelf SET Almira='"+String.valueOf(a)+"',Shelf='"+String.valueOf(s)+"' WHERE AccessionNo='" + cc + "'";
                                        Statement stmt = con.createStatement();
                                        stmt.execute(query);
                                    }
                                } catch (Exception ex) {
                                    Toast.makeText(getApplicationContext(), "Exceptions", Toast.LENGTH_LONG).show();
                                }
                            } else {
                                String query = "INSERT INTO suigen.TransAlmiraShelf (Almira, Shelf, AccessionNo) VALUES('" + String.valueOf(a) + "','" + String.valueOf(s) + "','" + String.valueOf(cc) + "')";
                                Statement stmt = con.createStatement();
                                stmt.execute(query);

                            }
                        }
                    } catch (Exception ex) {
                        Toast.makeText(getApplicationContext(), "Exceptions", Toast.LENGTH_LONG).show();
                    }
                }
            }
          return null;
        }

        @Override
        protected void onPostExecute(Void aVoid) {
            progressDialog.cancel();

            Toast.makeText(MainActivity.this, "Your Data Successfully Saved", Toast.LENGTH_LONG).show();
            almirah.setText("");
            shelf.setText("");
            accession_no.setText("");
            finish();
            startActivity(getIntent());

        }
    };
    task.execute();

}