2012-11-02 47 views
0

我有兩個列表(List1, List2)。我實現了拖放功能(從List1到List2)。對於List1,我使用this(setOnItemSelectedListener)偵聽器來選擇項目。它工作正常意味着它的返回列表項的位置,如果你不滾動。android - setOnItemSelectedListener不返回確切位置

假設如果您正在滾動然後嘗試選擇該項目,那麼它會返回可見項目的列表項目位置(例如,您的列表項目在滾動後有1到10個項目是您的列表中的第一項,則用戶假設選擇列表中的第7項然後它將返回位置爲4.)如何獲得列表的確切位置。這個聽衆也是一樣的setOnItemReceiverListener

code 
/** 
* Set selected Listener to know what item must be drag 
*/ 
lvStudies.setOnItemSelectedListener(mOnItemSelectedListener); 

/** 
* Listener to know on what position the new item must be insert 
*/ 
lvAddedContacts.setOnItemReceiverListener(listenerReceivePicture);  

/** 
* Save selected item 
*/ 
private AdapterView.OnItemSelectedListener mOnItemSelectedListener = new AdapterView.OnItemSelectedListener() { 

    @Override 
    public void onItemSelected(AdapterView<?> arg0, View arg1, int position, 
      long arg3) { 

     /** 
     * retrieve selected item from adapterview 
     */ 
     nStudiesMovePosition = position; 
     System.out.println("position"+position);    
    } 

    @Override 
    public void onNothingSelected(AdapterView<?> arg0) { 
     // TODO Auto-generated method stub   
    } 
}; 

private OnItemClickListener listenerReceivePicture = new OnItemClickListener() { 

    public void onItemClick(AdapterView<?> arg0, View arg1, int position,long arg3) 
    {  
     System.out.println("Position:"+position); 
    } 
    }; 

//Adapter for studies list 
private class StudiesListViewAdapter extends BaseAdapter 
{ 
    private LayoutInflater mInflater;   

    public StudiesListViewAdapter(Context context) { 
     // Cache the LayoutInflate to avoid asking for a new one each time. 
     mInflater = LayoutInflater.from(context); 
    } 

    public int getCount() 
    { 
     int nListSize = DH_Constant.StudiesList_obj.response.size(); 
     if(nListSize > 0) 
     { 
      return nListSize; 
     } 
     else 
     { 
      return 0; 
     }    
    } 

    public Object getItem(int position) 
    { 
     return position; 
    } 

    public long getItemId(int position) { 
     return position; 
    } 

    public View getView(final int position, View convertView, final ViewGroup parent) 
    {    
     ViewHolder holder;      
     convertView = mInflater.inflate(R.layout.studieslist_item, null); 
     holder = new ViewHolder(); 

     //getting the id's 
     holder.tvPatientName = (TextView) convertView.findViewById(R.id.SLI_PatientName_tv); 
     holder.tvStudy = (TextView) convertView.findViewById(R.id.SLI_Study_tv);   

     //Patient Name    
     String strTotal = DH_Constant.StudiesList_obj.response.get(position).PatientName; 
     String strPName = strTotal.substring(0, strTotal.indexOf("(Dt:")); 
     holder.tvPatientName.setText(strPName); 

     //Study 
     String strStudy = strTotal.substring(strTotal.indexOf("(Dt:")); 
     strStudy = strStudy.replace("Dt: ", ""); 
     strStudy = strStudy.replace("Study: ", "");   
     holder.tvStudy.setText(strStudy); 

     return convertView; 
    } 
    class ViewHolder 
    { 
     TextView tvPatientName,tvStudy;     
    } 
} 

//Adapter for added contacts 
private class ContactsListViewAdapter extends BaseAdapter 
{ 

    private LayoutInflater mInflater;   

    public ContactsListViewAdapter(Context context) { 
     // Cache the LayoutInflate to avoid asking for a new one each time. 
     mInflater = LayoutInflater.from(context); 
    } 

    public int getCount() 
    { 
     int nListSize = DH_Constant.AddedContactsList_obj.response.size(); 
     if(nListSize > 0) 
     { 
      return nListSize; 
     } 
     else 
     { 
      return 0; 
     }    
    } 

    public Object getItem(int position) 
    { 
     return position; 
    } 

    public long getItemId(int position) { 
     return position; 
    } 

    public View getView(final int position, View convertView, final ViewGroup parent) 
    {    
     ViewHolder holder;   

//   if (convertView == null) 
//   { 
      convertView = mInflater.inflate(R.layout.added_contacts_list, null); 
      holder = new ViewHolder(); 
//     convertView.setTag(holder); 
//   } 
//   else 
//   { 
//    holder = (ViewHolder) convertView.getTag(); 
//   } 

     //getting the id's 
     holder.tvName = (TextView) convertView.findViewById(R.id.xrays_Name_tv); 
     holder.btnRemove = (Button) convertView.findViewById(R.id.xrays_removebtn); 

     //Name    
     String strUserName = DH_Constant.AddedContactsList_obj.response.get(position).Email; 
     System.out.println("strUserName:"+strUserName); 

     //Change the color for differentiate the dicom and non dicom users 
     if(DH_Constant.AddedContactsList_obj.response.get(position).IsDicomUser) 
     { 
      holder.tvName.setTextColor(Color.rgb(0, 135, 137)); 
      String strrr =strUserName.substring(0, strUserName.indexOf('-')); 
      strUserName = DH_Constant.AddedContactsList_obj.response.get(position).Name +"("+ strrr+")";    
     } 
     else 
     { 
      holder.tvName.setTextColor(Color.BLUE);    
     } 

     holder.tvName.setText(strUserName); 

     //Remove button Listener 
     holder.btnRemove.setBackgroundResource(R.layout.xrays_contact_removebtn_widget); 
     holder.btnRemove.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) 
      { 
       lnIcontactId = DH_Constant.AddedContactsList_obj.response.get(position).ImportedContactsID; 
       nDicomUser = DH_Constant.AddedContactsList_obj.response.get(position).IsDicomUser?1:0; 
       //Alert for remove the contact 
       showDialog(DIALOG_removebtnalert); 
      } 
     });            
     return convertView; 
    } 

    class ViewHolder 
    { 
     TextView tvName; 
     Button btnRemove;   
    } 
} 
+0

偉大的代碼,通過使用此鏈接改善小錯誤: - http://stackoverflow.com/questions/13189354/i-want-to-let-user-add-multiple-items-by-action-sequence –

+0

結帳This SO回答 http://stackoverflow.com/questions/8362702/onitemselected-listener – kingori

回答

0

我不知道它是否是一個錯誤,但是當你滾動一個ListView的位置搞砸了。
在你的情況下,如果你可以使用id(long arg3 in your source code),請使用它。
有很多關於滾動時的位置的工作,但它可能是混亂的。

+0

它不工作 – naresh