lundi 27 janvier 2020

sql query did not work in checkbox looping

I've listview from the database with checkbox. My Objectives is to insert the checked listview into a table and update another table based on the checked listview. Unfortunately, my sql query did not work in the section.

Here is my full code:

package com.example.myapplication;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;


import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.app.Activity;

import android.content.Context;
import android.text.Html;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.Toast;

public class DataModel extends Activity  {
    ConnectionClass connectionClass;
    ResultSet rs;
    CheckBox checkBox;
    UserSessionManager session;
    List<Map<String, String>> MyArrList = null;
    String PFNO;
    String z = "";
    Boolean isSuccess = false;
    String a_status = "Active";
    String b_status = "Break";
    TextView lblname;



    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.checkboxtest);

        session = new UserSessionManager(getApplicationContext());
        TextView lblName = (TextView) findViewById(R.id.lblName);




        // Check user login (this is the important point)
        // If User is not logged in , This will redirect user to LoginActivity
        // and finish current activity from activity stack.
        if(session.checkLogin())
            finish();

        // get user data from session
        HashMap<String, String> user = session.getUserDetails();

        // get name
        String pfno = user.get(UserSessionManager.KEY_PF);
        pfno=pfno.replaceAll("[^0-9]","");

        // get email
        String password  = user.get(UserSessionManager.KEY_EMAIL);

        // Show user data on activity
        lblName.setText(Html.fromHtml(""+pfno+"" ));

        // listView1
        final ListView lisView1 = (ListView)findViewById(R.id.listView1);
        connectionClass = new ConnectionClass();
        Connection con = connectionClass.CONN();
        lblname = (TextView) findViewById(R.id.lblName);
        final String PIC = lblname.getText().toString();

        String dates = new SimpleDateFormat("MM/dd/yyyy", Locale.ENGLISH).format(Calendar.getInstance().getTime());
        String querycmd = "select Pf_no,Start_date,status from WORKTR where Scan_by ='"+PIC+"'and Start_date='"+dates+"' and status='"+a_status+"' group by Pf_no,Start_date,status ";

//                where Start_date='"+dates+"' and Scan_by='"+lblname+"'
        try {
            Statement statement = con.createStatement();
            rs = statement.executeQuery(querycmd);


            MyArrList = new ArrayList<Map<String, String>>();

            while (rs.next()) {

                Map<String, String> map = new HashMap<String, String>();



                /*** Rows 1 ***/
                map = new HashMap<String, String>();
                map.put("ID", rs.getString("Pf_no"));

                MyArrList.add(map);


            }


        } catch (SQLException e) {

//            Toast.makeText(.this, e.getMessage().toString(),
//                    Toast.LENGTH_LONG).show();

        }


        lisView1.setAdapter(new CountryAdapter(this));

        // Check All
        Button btnCheckAll = (Button) findViewById(R.id.btnCheckAll);
        btnCheckAll.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                int count = lisView1.getAdapter().getCount();
                for (int i = 0; i < count; i++) {
                    LinearLayout itemLayout = (LinearLayout)lisView1.getChildAt(i); // Find by under LinearLayout
                    CheckBox checkbox = (CheckBox)itemLayout.findViewById(R.id.ColChk);
                    checkbox.setChecked(true);
                }
            }
        });

        // Clear All
        Button btnClearAll = (Button) findViewById(R.id.btnClearAll);
        btnClearAll.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                int count = lisView1.getAdapter().getCount();

                for(int i = 0; i < count; i++) {
                    LinearLayout itemLayout = (LinearLayout)lisView1.getChildAt(i); // Find by under LinearLayout
                    CheckBox checkbox = (CheckBox)itemLayout.findViewById(R.id.ColChk);
                    checkbox.setChecked(false);
                }
            }
        });

        // Get Item Checked
        Button btnGetItem = (Button) findViewById(R.id.btnGetItem);
        btnGetItem.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                int count = lisView1.getAdapter().getCount();
                for (int i = 0; i < count; i++) {
                    LinearLayout itemLayout = (LinearLayout)lisView1.getChildAt(i); // Find by under LinearLayout
                    CheckBox checkbox = (CheckBox)itemLayout.findViewById(R.id.ColChk);
                    if(checkbox.isChecked()) {
                        PFNO = checkbox.getTag().toString();


                        Log.d("Item " + String.valueOf(i), checkbox.getTag().toString());
                        Toast.makeText(DataModel.this, checkbox.getTag().toString() + " Recorded for break", Toast.LENGTH_SHORT).show();



                        Break addPro = new Break();
                        addPro.execute();
//                        Log.d("Item "+String.valueOf(i), checkbox.getTag().toString());
//
//                        Toast.makeText(DataModel.this,checkbox.getTag().toString() ,Toast.LENGTH_LONG).show();
                    }
                }
                Toast.makeText(DataModel.this,"Breaktime Recorded", Toast.LENGTH_LONG).show();

            }


        });


    }

    public class Break extends AsyncTask<String, String, String> {


        String z = "";
        Boolean isSuccess = false;

        final ListView lisView1 = (ListView)findViewById(R.id.listView1);

        String PIC = lblname.getText().toString();
        //        String IPCpf = txt_IPC.getText().toString();
        String a_status = "Active";
        String b_status = "Break";

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

        protected void onPostExecute(String r) {
            //bbar.setVisibility(View.GONE);
            Toast.makeText(DataModel.this, r, Toast.LENGTH_SHORT).show();

            if (isSuccess) {
//                Intent i = new Intent(Operation.this, Start_Page.class);
//                startActivity(i);
//                finish();
            }

        }


        protected String doInBackground(String... params) {



            try {
                Connection con = connectionClass.CONN();
                if (con == null) {
                    z = "Error in connection with SQL server";
                } else {
                    Statement st = con.createStatement();
                    String ReasonAll="Break-All";
                    String dates = new SimpleDateFormat("MM/dd/yyyy", Locale.ENGLISH).format(Calendar.getInstance().getTime());
                    String currentTime = DateFormat.getDateTimeInstance().format(new Date());
                    String query = "insert into BRTR (PF_No,start_break,work_date, reason_break) values('"+ PFNO +"','"+currentTime+"','"+dates+"', '"+ReasonAll+"' )";
                    st.executeQuery(query);
                    String query2 = ("Update WORKTR set status ='" + b_status + "' where Start_date='" + dates + "' and Pf_no='" + PFNO + "' and Scan_by='"+PIC+"' and status='"+a_status+"'");
                    st.executeUpdate(query2);

                    if( st.executeUpdate(query)==1){
                        Toast.makeText(DataModel.this,"Breaktime Recorded", Toast.LENGTH_LONG).show();

                    }else{
                        Toast.makeText(DataModel.this,"Breaktime fail", Toast.LENGTH_LONG).show();
                    }

                }
            }
            catch (Exception ex)
            {

            }


            return z;
        }


    }



    public class CountryAdapter extends BaseAdapter
    {
        private Context context;

        public CountryAdapter(Context c)
        {
            //super( c, R.layout.activity_column, R.id.rowTextView, );
            // TODO Auto-generated method stub
            context = c;
        }

        public int getCount() {
            // TODO Auto-generated method stub
            return MyArrList.size();
        }
        public Object getItem(int position) {
            // TODO Auto-generated method stub
            return position;
        }

        public long getItemId(int position) {
            // TODO Auto-generated method stub
            return position;
        }
        public View getView(final int position, View convertView, ViewGroup parent) {
            // TODO Auto-generated method stub

            LayoutInflater inflater = (LayoutInflater) context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

            if (convertView == null) {
                convertView = inflater.inflate(R.layout.activity_column, null);

            }

            // ColID
            TextView txtID = (TextView) convertView.findViewById(R.id.ColID);
            txtID.setText(MyArrList.get(position).get("ID") +".");

            // ColCode

            // ColChk
            CheckBox Chk = (CheckBox) convertView.findViewById(R.id.ColChk);
            Chk.setTag(MyArrList.get(position).get("ID"));

            return convertView;

        }

    }


}

The application result to not responding when it comes to Break operation. Please help me to view and solve this problems. If I try to put the query in the checkbox loop itself, it will only run the insert query but won't run the update query.




Aucun commentaire:

Enregistrer un commentaire