2012-11-02 49 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