PeteH
PeteH

Reputation: 1930

Custom ArrayAdapter getView Not Being Called--Why Not?

I used this same pattern in another Activity class and it works perfectly. But in this class (also an Activity) the getView is never called. I confirmed that there are 9 item in the adapter by logging the fbFriendMatchAdapter.getCount(). I've tried changing the data source to a String[] variable but this had no impact on the (lack of) getView.

I would appreciate any suggestions! I've already researched SO thoroughly. There are a variety of flavors of this question but none have solved my issue. That's why I'm posting a new question.

//Here is the ArrayList data source definition.
//It is loaded using a for loop with 9 values.
private List<String> fbFriendMatchAdapter=new ArrayList<String>();


// Here is the Custom Array Adapter
ArrayAdapter<String> fbFriendMatchAdapter = new ArrayAdapter<String>(this,
        R.layout.row_left, R.id.ListViewName, fbFriendPotentialMatchArrayList) { 

    @Override
    public View getView(final int dialogPosition, View convertView, ViewGroup listParent) {
        LayoutInflater inflater = getLayoutInflater();
        View fbFriendMatchDialogViewRow = inflater.inflate(R.layout.row_left, listParent, false); 
        Log.d(BBTAG, String.format("BBSetup getView[%s] name=%s", dialogPosition, buddyDisplayName ));

        return fbFriendMatchDialogViewRow;
    }  // [END getView]

    @Override
    public String getItem(int position) {
        return fbFriendPotentialMatchArrayList.get(position);
    }

};

//Here is the dialog that includes the ArrayAdapter:
AlertDialog fbFriendsMatchDialog = new AlertDialog.Builder(new ContextThemeWrapper(context, R.style.PetesSpinnerReplacement))
        .setTitle("Select Correct Facebook Friend")  //FYI overridden by custom title
        .setAdapter(fbFriendMatchAdapter, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int selectedFBFriend) {
                buddyFBUIDs[buddyNumber] = fbFriendUIDsList.get(selectedFBFriend);  
            }
        })
        .setMessage("No matches found")
        .setCancelable(true)
        .setPositiveButton("Set as Facebook Friend", new DialogInterface.OnClickListener() {
             @Override
             public void onClick(DialogInterface dialog, int iFriend) {
                 dialog.dismiss();
             } 
        })
        .setNegativeButton("Friend Not Listed", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int id) {
                dialog.dismiss();
                buddyFBUIDs[buddyNumber] = "n/a";  //record lack of FB uid
            } 
        })  
        .create();  //build dialog
fbFriendsMatchDialog.show();  // now show dialog 

Upvotes: 3

Views: 8605

Answers (1)

Kevin Coppock
Kevin Coppock

Reputation: 134664

I would advise against trying to inline an ArrayAdapter subclass for this, as I believe ArrayAdapter makes some assumptions about the structure of your layout and data that may be throwing you off. Writing your own custom adapter is so dead simple that I would almost recommend never using ArrayAdapter (except maybe for the simplest of use cases). Just subclass BaseAdapter and the four required methods, and use that. Something like this:

private class MatchAdapter extends BaseAdapter {
    private List<String> mItems;
    private LayoutInflater mInflater;

    public MatchAdapter (Context c, List<String> items) {
        mItems = items;

        //Cache a reference to avoid looking it up on every getView() call
        mInflater = LayoutInflater.from(c); 
    }

    @Override
    public int getCount () {
        return mItems.size();
    }

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

    @Override
    public Object getItem (int position) {
        return mItems.get(position);
    }

    @Override
    public View getView (int position, View convertView, ViewGroup parent) {
        //If there's no recycled view, inflate one and tag each of the views
        //you'll want to modify later
        if (convertView == null) {
            convertView = mInflater.inflate (R.layout.row_left, parent, false);

            //This assumes layout/row_left.xml includes a TextView with an id of "textview"
            convertView.setTag (R.id.textview, convertView.findViewById(R.id.textview));
        }

        //Retrieve the tagged view, get the item for that position, and
        //update the text
        TextView textView = (TextView) convertView.getTag(R.id.textview);
        String textItem = (String) getItem(position);
        textView.setText(textItem);

        return convertView;
    }
}

Upvotes: 6

Related Questions