从CustomArrayAdapter返回特定的字符串

时间:2014-01-18 20:44:19

标签: android

您好我将一些ArrayLists传递给Custom ArrayAdapter,我想在点击某个项目时在arrayAdaper的位置检索一个String。目前我正在尝试adapter.getItem(position),但这只返回我想要返回String singleObject的名称字符串。

继承我的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 个答案:

答案 0 :(得分:1)

只需编写自己的方法,如

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