2017-02-17 131 views
0

我在創建android項目時使用了itemlist視圖和分頁。同時點擊該特定項目我想獲得該項目id.but但我沒有得到唯一的ID。Android中的項目列表中獲取唯一的項目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); 
      } 
     } 


    } 

enter image description here

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

      } 
     }); 

回答

0

如果要保持頁面的數量,那麼你可以添加頁碼項目位置。 這會給你每個項目的唯一編號。

+0

如何獲取listview.setOnItemClickListener方法中的頁碼?你能解釋一下嗎? –

+0

創建自定義數組適配器,它接受數據列表和當前頁碼。並將該適配器用於您的列表。 –