vendredi 29 mai 2015

How to get position of listitem using setonitemclicklistener and send to next activity

In my app I am getting response from server and displayint it in listview, now what I am trying is when user click on listitem it should get position of it and need to send it to next activity, but it is not working.

Following is mt snippet code

btn_go.setOnClickListener(new OnClickListener() {
    @Override
    public void onClick(View v) {
        try {
            ConnectivityManager connectivityManager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
            if (connectivityManager.getNetworkInfo(
                        ConnectivityManager.TYPE_MOBILE).getState() == NetworkInfo.State.CONNECTED
                        || connectivityManager.getNetworkInfo(
                                ConnectivityManager.TYPE_WIFI).getState() == NetworkInfo.State.CONNECTED) {
                // listView1.removeAllViews();
                listView1.setAdapter(null);
                arraylist_oper = new ArrayList<HashMap<String, String>>();

                // listView1.notify();
                new getOperationalControlList().execute();  
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });

    listView1.setOnItemClickListener(new OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) {
            // TODO Auto-generated method stub

            //qr =  ((String) listView1.getItemAtPosition(position)).toString();

            Intent intent=new Intent(OperationalControl.this,DispatchTracking.class);
            intent.putExtra("arrow_val", "2");
            intent.putExtra("qrcodes", qr);
            Toast.makeText(OperationalControl.this, qr, Toast.LENGTH_LONG).show();
            startActivity(intent);
        }
    });
}

class getOperationalControlList extends AsyncTask<String, String, String> {
    private String msg = "";
    int register_error = 1;

    JSONArray operation;
    JSONObject obc;
    String error;
    String access_token, office_name, office_id;
    String user_id;
    String name;
    private ProgressDialog progressDialog;


    @Override
    protected void onPreExecute() {
        super.onPreExecute();

        progressDialog = new ProgressDialog(OperationalControl.this);
        progressDialog.setCancelable(true);
        progressDialog.setMessage("Loading...");
        progressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
        progressDialog.setProgress(0);
        progressDialog.show();
        noresponse.setVisibility(View.GONE);

    }

    @Override
    protected String doInBackground(String... params) {
        JSONObject jsonObjSend;
        String content = null;
        arraylist_oper = new ArrayList<HashMap<String, String>>();
        try {

            consts.pref = getSharedPreferences("pref", MODE_PRIVATE);
            consts.editor = consts.pref.edit();
            String OperationalControlList_URL = ((consts.pref
                    .getString(consts.Base_URL,
                            consts.Base_URL)) + consts.OperationalControlList_URL);
            Log.d("OperationalControlList_URL url:",
                    OperationalControlList_URL);

            arraylist = new ArrayList<HashMap<String, String>>();
            HttpClient httpClient = new DefaultHttpClient();
            HttpPost httpPost = new HttpPost(OperationalControlList_URL);

            System.out.println("URL :-"
                    + consts.OperationalControlList_URL.toString());

            user_id = consts.pref.getString("user_id", "");
            access_token = consts.pref.getString("access_token", "");
            office_id = consts.pref.getString("office_id", "");
            date = date_dropdown.getText().toString();

            List<NameValuePair> nameValuePair = new ArrayList<NameValuePair>(
                    5);

            nameValuePair.add(new BasicNameValuePair("user_id", user_id));
            nameValuePair.add(new BasicNameValuePair("access_token",
                    access_token));
            nameValuePair.add(new BasicNameValuePair("filter", filter));
            nameValuePair
                    .add(new BasicNameValuePair("office_id", office_id));
            nameValuePair.add(new BasicNameValuePair("date", date));

            // Encoding POST data
            try {
                httpPost.setEntity(new UrlEncodedFormEntity(nameValuePair));

                HttpResponse response = httpClient.execute(httpPost);
                HttpEntity entity = response.getEntity();

                System.out.println("USER_ID : " + user_id.toString());
                System.out.println("access_token : "
                        + access_token.toString());
                System.out.println("filter : " + filter.toString());
                System.out.println("office_id : " + office_id.toString());
                System.out.println("date : " + date.toString());

                content = EntityUtils.toString(entity);

                Log.d("aaa", content);

                jsonObjSend = new JSONObject(content.toString());

                if (jsonObjSend.getString("status").equals("2")) {

                    register_error = 1;
                    error = jsonObjSend.getString("error");
                    if (error.equals("3")) {
                        msg = jsonObjSend.getString("message");
                    } else if (error.equals("4")) {
                        msg = jsonObjSend.getString("message");
                    } else if (error.equals("5")) {
                        msg = jsonObjSend.getString("message");
                    } else if (error.equals("6")) {
                        msg = jsonObjSend.getString("message");
                    } else if (error.equals("7")) {
                        msg = jsonObjSend.getString("message");
                    } else if (error.equals("8")) {
                        msg = jsonObjSend.getString("message");
                    } else if (error.equals("9")) {
                        msg = jsonObjSend.getString("message");
                    } else if (error.equals("10")) {
                        msg = jsonObjSend.getString("message");
                    } else if (error.equals("11")) {
                        msg = jsonObjSend.getString("message");
                    } else if (error.equals("12")) {
                        msg = jsonObjSend.getString("message");
                    } else {
                        msg = jsonObjSend.getString("message");
                    }
                    // {"status":1,"message":"There is no activity of the selected day and filtering otpions"}
                } else if (jsonObjSend.getString("status").equals("1")) {

                    if (jsonObjSend.has("message"))
                        msg = jsonObjSend.getString("message");
                    // msg = jsonObjSend.getString("message");
                    register_error = 0;
                    operation = new JSONArray();
                    if (jsonObjSend.has("list")) {
                        operation = jsonObjSend.getJSONArray("list");

                        // arraylist_oper = new ArrayList<HashMap<String,
                        // String>>();

                        for (int i = 0; i < operation.length(); i++) {
                            map = new HashMap<String, String>();
                            qr = operation.getJSONObject(i)
                                    .getString("qrcode");
                            type = operation.getJSONObject(i)
                                    .getString("type").toString();

                            Log.d("Types", type);
                            String origin = operation.getJSONObject(i)
                                    .getString("origin");
                            String destiny = operation.getJSONObject(i)
                                    .getString("destiny");
                            String stop_status = operation.getJSONObject(i)
                                    .getString("stop_status");
                            String stop_status_name = operation
                                    .getJSONObject(i).getString(
                                            "stop_status_name");
                            String stop_status_color = operation
                                    .getJSONObject(i).getString(
                                            "stop_status_color");
                            map.put("qrcode", qr);
                            map.put("type", type);
                            map.put("origin", origin);
                            map.put("destiny", destiny);
                            map.put("stop_status", stop_status);
                            map.put("stop_status_name", stop_status_name);
                            map.put("stop_status_color", stop_status_color);
                            // map.put("status_name", status_name);
                            arraylist_oper.add(map);

                            Log.d("qrcode:", qr + " type: " + type
                                    + " origine: " + origin);
                        }
                    } else {

                        msg = jsonObjSend.getString("message");
                    }

                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        } catch (Exception e1) {
            e1.printStackTrace();
        }
        return content;
    }

    @Override
    protected void onPostExecute(String result) {
        super.onPostExecute(result);
        progressDialog.dismiss();

        if (error.equals("6")) {

            Intent intent=new Intent(OperationalControl.this,LoginActivity.class);
            startActivity(intent);
            OperationalControl.this.finish();

        }
        try {
            if (arraylist_oper.size() > 0) {
                Operational_LazyAdapter adpt = new Operational_LazyAdapter(
                        getApplicationContext(), arraylist_oper);
                listView1.setAdapter(adpt);
                // Toast.makeText(getApplicationContext(), msg,
                // Toast.LENGTH_LONG).show();
            } else {
                Toast.makeText(getApplicationContext(),
                        "Office não definir corretamente ou" + msg,
                        Toast.LENGTH_LONG).show();

                noresponse.setVisibility(View.VISIBLE);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

Response

{"status":1,
    "list":[{
        "qrcode":"#00000757-00000277-700101-0000000040",
        "type":"Tipo de Opera\u00e7\u00e3o: Chegada",
        "origin":"Origem: ARMAMAR (757)",
        "destiny":"Destino: REGUA (277)",
        "stop_status":6,
        "stop_status_name":"Finalizado",
        "stop_status_color":"#cccccc"
    },
    {
        "qrcode":"#00000278-00000277-700101-0000000041",
        "type":"Tipo de Opera\u00e7\u00e3o: Chegada",
        "origin":"Origem: LAMEGO (278)",
        "destiny":"Destino: REGUA (277)",
        "stop_status":6,
        "stop_status_name":"Finalizado",
        "stop_status_color":"#cccccc"
    }]
}

Aucun commentaire:

Enregistrer un commentaire