2014-01-18 77 views
0

您好我傳遞了一些ArrayLists到一個自定義的ArrayAdapter,我想在arrayAdaper的位置檢索一個字符串,當我點擊一個項目。此刻我試圖adapter.getItem(position),但這只是返回名稱字符串,我想返回字符串singleObject。從CustomArrayAdapter返回特定的字符串

我的繼承人onClickItemListener

docsList.setOnItemClickListener(new OnItemClickListener() { 

    @Override 
    public void onItemClick(AdapterView<?> parent, View view, int position, long duration) 
     { 
     String singleObjectArray = (String) adapter.getItem(position); 
     Log.v("Cat2","Array item = " + singleObjectArray); 
     } 
    }); 

我的繼承人CustomArrayAdapter

private ArrayList<String> contactName; 
    private ArrayList<String> accessLevel; 
    private ArrayList<String> singleObjectsArray; 
    private ArrayList<String> imageURLs; 
    private String bgColor; 
    public Typeface myTypeFace; 
    /* here we must override the constructor for ArrayAdapter 
    * the only variable we care about now is ArrayList<Item> objects, 
    * because it is the list of objects we want to display. 
    */ 
    public ContactArrayAdapter(Context context, int layoutResourceId, ArrayList<String> singleObjectsArray, ArrayList<String> accessLevel, ArrayList<String> ImageURL, String bgColor,Typeface font, ArrayList<String> contactName) { 
     super(context, layoutResourceId, contactName); 
     this.singleObjectsArray = singleObjectsArray; 
     this.contactName = contactName; 
     this.accessLevel = accessLevel; 
     this.imageURLs = ImageURL; 
     this.bgColor = bgColor; 

     myTypeFace = font; 

    } 




    public View getView(int position, View convertView, ViewGroup parent){ 

     // assign the view we are converting to a local variable 
     View v = convertView; 

     // first check to see if the view is null. if so, we have to inflate it. 
     // to inflate it basically means to render, or show, the view. 
     if (v == null) { 
      LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
      v = inflater.inflate(R.layout.contact_cell, null); 

     } 


     /* 
     * Recall that the variable position is sent in as an argument to this method. 
     * The variable simply refers to the position of the current object in the list. (The ArrayAdapter 
     * iterates through the list we sent it) 
     * 
     * Therefore, i refers to the current Item object. 
     */ 

     String singleObject = singleObjectsArray.get(position); 
     String name = contactName.get(position); 
     String accesslevel = accessLevel.get(position); 
     Log.v("CAA", "ImageURL= " + imageURLs); 
     String image = imageURLs.get(position); 

     Log.v("CAA", "image= " + image); 



     if (name != null) { 

      // This is how you obtain a reference to the TextViews. 
      // These TextViews are created in the XML files we defined. 

      TextView docTitle = (TextView) v.findViewById(R.id.name); 
      docTitle.setTypeface(myTypeFace); 
      docTitle.setTextColor(Color.parseColor(bgColor)); 
      TextView access = (TextView) v.findViewById(R.id.doctype); 
      access.setTypeface(myTypeFace); 
      TextView docMod = (TextView) v.findViewById(R.id.modified); 
      docMod.setTypeface(myTypeFace); 

      // check to see if each individual textview is null. 
      // if not, assign some text! 

      if (docTitle != null){ 
       docTitle.setText(name); 
      } 

      if (access != null){ 

       if(accesslevel.equals("1")){ 
        access.setText("Administrator"); 
       }else if(accesslevel.equals("2")){ 
        access.setText("User"); 
       } 
      } 

      ImageView contactImage = (ImageView) v.findViewById(R.id.docicon); 


      if(imageURLs != null){ 

       if(!(image.equals("null"))){ 
       ImageLoader imageLoader = ImageLoader.getInstance(); 
       imageLoader.init(ImageLoaderConfiguration.createDefault(getContext())); 
       imageLoader.displayImage(SiriusControl_Config.image_location + image, contactImage); 

       }else{ 

       contactImage.setBackgroundResource(R.drawable.sc_user); 
       } 

      } 





     } 

     StateListDrawable drawable = new StateListDrawable(); 
     drawable.addState(new int[] { android.R.attr.state_pressed }, new ColorDrawable(Color.parseColor(bgColor))); 
     v.setBackgroundDrawable(drawable); 




     return v; 
    } 

回答

1

只寫你自己的方法一樣

public String getMyString(int position) { 
    return singleObjectsArray.get(position); 
} 
+0

非常感謝你這工作一種享受。 –