从Android中的商品列表中获取唯一商品ID

时间:2017-02-17 07:28:15

标签: android list pagination android-arrayadapter

我正在创建android项目,因为我正在使用itemlist视图和分页。点击该特定项目时我想获得该项目ID。但我没有获得唯一ID。

当我使用位置时,每个页面都会形成0-9。

我有字段' audit_id'。我想将此值指定为项目ID,我想得到。是否有可能?

我的代码是:

private class AsyncLogin extends AsyncTask<String, String, StringBuilder> {
        ProgressDialog pdLoading = new ProgressDialog(Tblview.this);
        HttpURLConnection conn;
        URL url = null;

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

            //this method will be running on UI thread
            pdLoading.setMessage("\tLoading...");
            pdLoading.setCancelable(false);
            pdLoading.show();

        }

        @Override
        protected StringBuilder doInBackground(String... params) {

            try {

                // Enter URL address where your php file resides
                url = new URL("http://192.168.1.99/ashwad/ims/webservices/alldata.php");

            } catch (MalformedURLException e) {
                // TODO Auto-generated catch block
                Toast.makeText(getApplicationContext(), "URL Exception", Toast.LENGTH_LONG).show();

                e.printStackTrace();

                return null;
            }
            try {
                // Setup HttpURLConnection class to send and receive data from php and mysql
                conn = (HttpURLConnection) url.openConnection();
                conn.setReadTimeout(READ_TIMEOUT);
                conn.setConnectTimeout(CONNECTION_TIMEOUT);
                conn.setRequestMethod("POST");

                // setDoInput and setDoOutput method depict handling of both send and receive
                conn.setDoInput(true);
                conn.setDoOutput(true);

                // Append parameters to URL
                Uri.Builder builder = new Uri.Builder()
                        .appendQueryParameter("user_id", "sdfa")
                        .appendQueryParameter("password", "asffs");
                String query = builder.build().getEncodedQuery();

                // Open connection for sending data
                OutputStream os = conn.getOutputStream();
                BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(os, "UTF-8"));
                writer.write(query);
                writer.flush();
                writer.close();
                os.close();
                conn.connect();

            } catch (IOException e1) {
                // TODO Auto-generated catch block

                e1.printStackTrace();

                return null;
            }

            try {

                int response_code = conn.getResponseCode();

                // Check if successful connection made
                if (response_code == HttpURLConnection.HTTP_OK) {

                    // Read data sent from server
                    InputStream input = conn.getInputStream();
                    BufferedReader reader = new BufferedReader(new InputStreamReader(input));


                    BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
                    result = new StringBuilder();
                    String next1;
                    while ((next1 = bufferedReader.readLine()) != null) {

                        result.append(next1 + "\n");

                    }

                    Log.e("dfasf",result.toString());

                }

            } catch (IOException e) {

                e.printStackTrace();


            }  finally {
                conn.disconnect();
            }

            return result;

        }

        @Override

        protected void onPostExecute(StringBuilder s) {
            super.onPostExecute(s);

            try {
                JSONArray login;
                JSONObject obj = new JSONObject(s.toString());


                if(s.toString().contains("Result")) {

                    data = new ArrayList<String>();
                    login = obj.getJSONArray("Result");



                    for(int i=0;i<login.length();i++) {

                        JSONObject c = login.getJSONObject(i);

                        productsArray = c.getJSONArray(Latest_Products);



                        TOTAL_LIST_ITEMS=productsArray.length();


                        int val = TOTAL_LIST_ITEMS%NUM_ITEMS_PAGE;

                        val = val==0?0:1;
                        pageCount = (TOTAL_LIST_ITEMS/NUM_ITEMS_PAGE)+val;




                        for (int j = 0; j < productsArray.length(); j++) {

                            JSONObject cc = productsArray.getJSONObject(j);

                            //------------------------------------------------------------------------

                                Log.e("audit",cc.getString("phone_name"));
                            String audit_id_str = cc.getString("audit_id");
                            int audit_id =Integer.parseInt(audit_id_str);

                                listview.setSelection(audit_id);
                                data.add(cc.getString("phone_name") +"\n\n"+cc.getString("audit_status")  );

                            loadList(0);


                            btn_next.setOnClickListener(new View.OnClickListener() {

                                public void onClick(View v) {

                                    increment++;
                                    loadList(increment);
                                    CheckEnable();
                                }
                            });

                            btn_prev.setOnClickListener(new View.OnClickListener() {

                                public void onClick(View v) {

                                    increment--;
                                    loadList(increment);
                                    CheckEnable();
                                }
                            });

                            //------------------------------------------------------------------------

                        }

                    }

                    pdLoading.dismiss();
                    //CheckEnable();
                }

            } catch (JSONException e) {
                e.printStackTrace();
            }

            }

        private void loadList(int number)
        {
            ArrayList<String> sort = new ArrayList<String>();
            title.setText("Page "+(number+1)+" of "+pageCount);

            int start = number * NUM_ITEMS_PAGE;
            for(int i=start;i<(start)+NUM_ITEMS_PAGE;i++)
            {
                if(i<data.size())
                {
                    sort.add(data.get(i));
                }
                else
                {
                    break;
                }
            }
            sd = new ArrayAdapter<String>(Tblview.this,android.R.layout.simple_list_item_1,sort);
            listview.setAdapter(sd);
        }



        private void CheckEnable()
        {

            if(increment+1 == pageCount)
            {

                btn_next.setEnabled(false);
                btn_prev.setEnabled(true);
            }
            else if(increment == 0)
            {
                btn_prev.setEnabled(false);
                btn_next.setEnabled(true);
            }
            else
            {
                btn_prev.setEnabled(true);
                btn_next.setEnabled(true);
            }
        }


    }

w3c specification

listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

                    int positon1 =position;
                    String a1 = Integer.toString(positon1);

                Toast.makeText(getApplicationContext(),a1,Toast.LENGTH_SHORT).show();

            }
        });

1 个答案:

答案 0 :(得分:0)

如果您要保留页数,则可以将页码添加到项目位置。 这将为每个项目提供唯一的编号。