【问题标题】:How to get position of listitem using setonitemclicklistener and send to next activity如何使用 setonitemclicklistener 获取列表项的位置并发送到下一个活动
【发布时间】:2015-05-29 10:06:05
【问题描述】:

在我的应用程序中,我从服务器获得响应并将其显示在列表视图中,现在我正在尝试的是当用户单击列表项时,它应该获得它的位置并需要将其发送到下一个活动,但它不起作用。

下面是mt sn-p代码

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

【问题讨论】:

  • 你想要所有的 JSON 数据到下一个活动吗?
  • 然后将 json 对象传递给捆绑包或将最终的数组列表传递给下一个活动。检查我的答案。
  • @PiyushGupta 需要你的帮助stackoverflow.com/questions/30951608/…

标签: android listview android-intent


【解决方案1】:

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

在你的情况下添加

intent.putExtra("position", position);

在您的 DispatchTracking Activity 中使用

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

【讨论】:

    【解决方案2】:

    只需将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")));
    

    现在在下一个活动中检索为

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

    现在使用 For 循环在您的第二个活动中检查您的可检索数组列表。

    for(int i=0; i < myList.size ; i++) {
    
      if(qrCode.equals(myList.get(i).get("qrcode"))){
          // get your data here you can get according to qrCode. Like
    
          String density = myList.get(i).get("destiny"); // same for others
    
      }
    

    【讨论】:

    • 在我的下一个活动中,我得到了两个记录..我想要更详细的记录..我的意思是如果点击带有 qrcode #00000757-00000277-700101-0000000040 的记录..然后在下一个活动中我只想记录与#00000757-00000277-700101-0000000040..明白了吗??
    • 那么您需要将 qrcode 传递给下一个活动,例如 intent.putExtra("qrcode", arraylist_oper.get(position).get("qrcode"));然后在检索后的下一个活动中检查 if(qrcode.equals("#00000757-00000277-700101-0000000040")) { 然后只显示该数据。
    • 好的,但如果我需要发送其他记录,如类型、命运等,使用 qrcode #00000757-00000277-700101-0000000040 .. 如响应中所述..
    猜你喜欢
    • 1970-01-01
    • 2016-03-07
    • 2020-08-18
    • 2012-09-16
    • 1970-01-01
    • 2018-11-26
    • 2020-03-28
    • 2019-05-22
    • 2016-01-31
    相关资源
    最近更新 更多