如何使用setonitemclicklistener获取listitem的位置并发送到下一个活动

时间:2015-05-29 10:06:05

标签: android listview android-intent

在我的应用程序中,我收到服务器的响应,并在listview中显示它,现在我正在尝试的是当用户点击listitem它应该获取它的位置并需要将其发送到下一个活动,但它无法正常工作。

以下是mt代码段

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

响应

{"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"
    }]
}

2 个答案:

答案 0 :(得分:1)

在列表项单击侦听器上创建的Intent中,您应该添加所需的所有变量。

在你的情况下添加

intent.putExtra("position", position);

在您的DispatchTracking活动中使用

int position = getIntent().getExtras().getInt("position");

答案 1 :(得分:0)

您需要将if((a*a*i*j) <= m*n){ System.out.println("Entered if block"); if(a*i <= m){ //to check weather it is entering if() System.out.println("true"); i+=1; } if(a*j <= n ){ System.out.println("false"); //to check weather it is entering if() j+=1; } System.out.println("i is:"+ i +"j is:"+j); } HashMap传递给下一个Arraylist。像,

Activity

并在您的下一个intent.putExtra("myList",arraylist_oper); 中检索为

Activity

编辑:

您需要通过Intent intent = getIntent(); ArrayList<HashMap<String,String>> mylist = (ArrayList<HashMap<String, String>>)intent.getSerializableExtra("myList"); 下一个活动

qrCode

现在在下一个活动中检索

intent.putExtra("qrcodes", arraylist_oper.get(position).get("qrcode")));

现在使用For循环检查第二个活动中的可复原的arraylist。

String qrCode = intent.getStringExtra("qrcodes");