1

我有一个自定义适配器。在我的getView()中,我试图用我的联系人列表填充“holder.contact”,但没有成功。我确实有这种ListView使用联系人填充我的方法,但它破坏了我的CheckBoxes. 这段代码不在我的CustomAdapter课上:

public void populateContactList() {
    // Build adapter with contact entries
    Cursor cursor = getContacts();
    String[] fields = new String[] {
            ContactsContract.Data.DISPLAY_NAME       
    };

    SimpleCursorAdapter adapter = new SimpleCursorAdapter(this, R.layout.contact_entry, cursor,
            fields, new int[] {R.id.contactEntryText});    
    lv.setAdapter(adapter);    
} // END POPULATECONTACTLIST

public Cursor getContacts()
{ 
    // Run query
    Uri uri = ContactsContract.Contacts.CONTENT_URI;
    String[] projection = new String[] {
            ContactsContract.Contacts._ID,
            ContactsContract.Contacts.DISPLAY_NAME        
    };

    String selection = ContactsContract.Contacts.IN_VISIBLE_GROUP + " = '" +
            (false ? "0" : "1") + "'";
    String[] selectionArgs = null;
    String sortOrder = ContactsContract.Contacts.DISPLAY_NAME + " COLLATE LOCALIZED ASC";

    return managedQuery(uri, projection, selection, selectionArgs, sortOrder);
} // END GETCONTACTS 

但我试图在这里定义它,但没有成功(自定义适配器类):

@Override
public View getView(final int position, View convertView, ViewGroup parent)
{
    View rowView = convertView;
    ViewHolder holder = null;

    if (rowView == null) {
        LayoutInflater inflater = (LayoutInflater)context.getSystemService(
                                           Context.LAYOUT_INFLATER_SERVICE);
        // HERE I AM INFLATING LISTVIEW LAYOUT.
        rowView = inflater.inflate(R.layout.contact_entry, null, false);
        holder = new ViewHolder();
        holder.photo = (ImageView) rowView.findViewById(R.id.iv_contactPic);
        holder.contact = (TextView) rowView.findViewById(R.id.contactEntryText);
        holder.tv = (TextView) rowView.findViewById(R.id.SMSCounttext);
        holder.cb = (CheckBox) rowView.findViewById(R.id.cb_contactHistoryChk);


        rowView.setTag(holder);

    } else {
        holder = (ViewHolder) rowView.getTag();
    }

有没有办法在里面应用我的 populateContactList()方法getView()

我的构造函数看起来像这样。“元素”已被另一个文本视图使用:

public CustomAdapter(Context context, int type, ArrayList<String> elements){
    super(context, type, elements);
4

1 回答 1

0

你的问题有点模棱两可。您有将带有数据的 apopulateContactList()绑定到 a并且您想在另一个自定义的方法中使用此方法(如果是这种情况,请不要这样做)?或者您想使用自己的适配器并将数据与联系人绑定到它(如果是这种情况,则使用查询的结果,,与您自己的)?CursorContactsSimpleCursorAdaptergetView()AdaptercursorAdapter

您没有说Adapter您的自定义Adapter扩展的类型。如果它是Cursorbased Adapter,事情就简单了,您可以使用bindViewandnewView来访问游标数据(并且您将CursorfromgetContacts()在构造函数中传递(到超类))。否则,将Cursor您获得的数据放入getContacts()类似 an 的结构中,ArrayList并将其用作适配器的数据。(我建议使用第一个选项)。

此外,维护行布局的状态是您的工作CheckBoxes(这里有很多关于如何执行此操作的问题)。

编辑:

    ArrayList<String> names = new ArrayList<String>();
    Cursor cursor = getContacts();
    while (cursor.moveToNext()) {
         names.add(cursor.getString(cursor.getColumnIndex(ContactsContract.Data.DISPLAY_NAME )));

}

编辑:
使用元素 ArrayList并将其绑定到行:

    //...
    private ArrayList<String> data;

    public CustomAdapter(Context context, int type, ArrayList<String> elements){
        super(context, type, elements);
        this.data = elements;
    }

    public int getCount() {
        return data.size();  
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        View rowView = convertView;
        ViewHolder holder = null;

        if (rowView == null) {
            LayoutInflater inflater = (LayoutInflater)context.getSystemService(
                                               Context.LAYOUT_INFLATER_SERVICE);
            // HERE I AM INFLATING LISTVIEW LAYOUT.
            rowView = inflater.inflate(R.layout.contact_entry, parent, false); //<= use parent instead of null
            holder = new ViewHolder();
            holder.photo = (ImageView) rowView.findViewById(R.id.iv_contactPic);
            holder.contact = (TextView) rowView.findViewById(R.id.contactEntryText);
            holder.tv = (TextView) rowView.findViewById(R.id.SMSCounttext);
            holder.cb = (CheckBox) rowView.findViewById(R.id.cb_contactHistoryChk);
            rowView.setTag(holder);
        } else {
            holder = (ViewHolder) rowView.getTag();
        }
        //bind the data to the row views
        String contactName = data.get(position);
        holder.contact.setText(contactName);
        // other stuff you might do to build thw row
//...
于 2012-05-11T09:29:04.937 回答