2012-05-11 14 views
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); 
+0

私はあなたが拡張可能なリストビューを使用する必要があることも可能であると思ういけません。 .. –

答えて

1

あなたの質問は少しあいまいです。あなたはCursorContactsデータをバインドしてSimpleCursorAdapterにバインドするpopulateContactList()を持っていて、別のカスタムメソッドでこのメソッドを使用したいとします(この場合はそうしないでください)。または、独自のアダプタを使用して連絡先のデータをバインドしたい場合(この場合はcursorのクエリの結果を自分のAdapterと組み合わせて使用​​します)

AdapterあなたのカスタムAdapterがどんなタイプのものであるかは言わないでください。 Cursorの場合Adapterの場合は、bindViewnewViewを使用してカーソルデータにアクセスすることができます(コンストラクタ内ののCursorをスーパークラスに渡します)。そうでない場合はCursorからgetContacts()というデータを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 
//... 
+0

私のカスタムアダプタクラスは "ArrayAdapter "を拡張しますが、私の "getContacts()"はcuですrsorは別のクラスにあります。私のチェックボックスはOKです。私はちょうど上記とは違ってリストを設定する必要があります。 – SpicyWeenie

+1

@ChocoManカスタムアダプタをインスタンス化するアクティビティから 'getContacts()'メソッドを呼び出します(別のクラスにあるとします。単に 'populateContactList'メソッドのように行います)。次に、 'ArrayList 'のカーソルから名前を抽出します。これをカスタムアダプターコンストラクターに渡し、アダプターを生成するために使用することができます。 – Luksprog

+0

あなたは何を意味するのかよく分かりません... – SpicyWeenie

関連する問題