2012-09-22 3 views
1

목록보기에서 연락처를 가져 오려고합니다. 이제 simple_list_item_multiple_choice을 사용하면 여러 연락처를 선택할 수 있지만 번호가없는 이름 만 볼 수 있다는 것을 알고 있습니다.Android : 복수 선택 및 하위 항목을 사용할 수있는 ListView

simple_list_item_2은 이름과 번호를 모두 표시하는 데 사용할 수 있지만 하나의 연락처 만 선택할 수 있습니다.

둘 모두를 결합한 템플릿이 있습니까? 그렇지 않은 경우 어떻게 두 기능을 모두 갖춘 사용자 정의 목록을 만들 수 있습니까?

편집 : 이것은 내가 여기

CursorLoader cl = new CursorLoader(this,ContactsContract.CommonDataKinds.Phone.CONTENT_URI, PROJECTION, null, null, ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME+" ASC"); 
Cursor c = cl.loadInBackground(); 
SimpleCursorAdapter adapter = new SimpleCursorAdapter(this, android.R.layout.simple_list_item_multiple_choice, // Use a template 
                 // that displays a 
                 // text view 
        c, // Give the cursor to the list adapter 
        new String[] { ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME}, 
        new int[] { android.R.id.text1},0); 

setListAdapter(adapter); 

을 사용하고 코드입니다, SimpleCursorAdapter의 두 번째 매개 변수는 simple_list_item_multiple_choice하지만 그것은 단지 android.R.id.text1 처리를 지원합니다. 따라서 하위 항목이 아닌 항목 만 사용할 수 있습니다.

그러나 다음 코드

SimpleCursorAdapter adapter = new SimpleCursorAdapter(this, android.R.layout.simple_expandable_list_item_2, // Use a template 
                 // that displays a 
                 // text view 
        c, // Give the cursor to the list adapter 
        new String[] { ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME, ,ContactsContract.CommonDataKinds.Phone.NUMBER}, 
        new int[] { android.R.id.text1,android.R.id.text2},0); 

나는 그것을 ContactsContract.CommonDataKinds.Phone.DISPLAY_NAMEandroid.R.id.text1android.R.id.text2에 기록 될 NUMBER 모두를 줄 수 있지만, 다중 선택 기능을 사용할 수 없습니다

.

+1

사용하는 코드를 넣을 수 있습니까? 그것은 많은 도움이 될 것입니다. – Heejin

+1

요구 사항에 따라 레이아웃을 사용자 지정해야합니다. – Dilip

+0

희진, 끝났습니다. 감사합니다 :) –

답변

1

Dipu가 말했듯이 사용자 정의 된 레이아웃을 만들어야합니다. 이름과 연락처를 표시하려면 두 개의 텍스트보기와 확인을위한 하나의 확인란이 필요합니다.

이 튜토리얼에서 코딩을 시작할 수 있습니다

http://www.mysamplecode.com/2012/07/android-listview-checkbox-example.html

이 문제를 해결할 수 country_info.xml 하나 개 이상의 텍스트보기를 추가합니다.

, 당신은 당신의 자신의 어댑터를 구현해야 사용자 지정 목록보기 레이아웃을 사용하려면

을 추가했습니다.

이 자습서 (2. 사용자 지정 ArrayAdapter 예제)는 어떻게 수행하는지 파악하는 데 도움이됩니다. 김희진에서 제공

http://www.mkyong.com/android/android-listview-example/

0

대답은 우수하지만, 사용자 정의 ArrayAdaptor을 구현하는 것이 중요하지 않습니다. 내가해야 할 일은 커스텀 레이아웃을 작성하여 SimpleCursorAdaptor 생성자로 보내면된다.

사용자 정의 레이아웃은 목록보기에서 각 항목의 레이아웃을 나타냅니다. 각 행에 CheckedTextView을 포함하고 또 다른 작은 행 TextView을 하위 항목으로 포함해야합니다. 그래서 row_view라는 레이아웃을 만들었습니다.다음과 같은 내용

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" > 

    <CheckedTextView 
     android:id="@+id/checkedTextView" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:checkMark="?android:attr/listChoiceIndicatorMultiple" 
     android:text="CheckedTextView" /> 

    <TextView 
     android:id="@+id/textView" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:text="Small Text" 
     android:textAppearance="?android:attr/textAppearanceSmall" /> 

</LinearLayout> 

와 XML은 그럼 난 그냥 생성자

SimpleCursorAdapter adapter = new SimpleCursorAdapter(this, R.layout.row_view, // Use a template 
                 // that displays a 
                 // text view 
        c, // Give the cursor to the list adapter 
        new String[] { ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME, ContactsContract.CommonDataKinds.Phone.NUMBER}, 
        new int[] { R.id.checkedTextView, R.id.textView},0); 

setListAdapter(adapter); 

이 전체 코드

public class MultipleContacts extends ListActivity implements OnItemClickListener { 
    private static final String[] PROJECTION = new String[] { 
     ContactsContract.CommonDataKinds.Phone._ID 
     ,ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME 
     ,ContactsContract.CommonDataKinds.Phone.NUMBER 
    }; 

    SimpleCursorAdapter adapter; 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.multiple_contacts); 
     getListView().setChoiceMode(ListView.CHOICE_MODE_MULTIPLE); 
     getListView().setOnItemClickListener(this); 
     // Get a cursor with all people 

     CursorLoader cl = new CursorLoader(this,ContactsContract.CommonDataKinds.Phone.CONTENT_URI, 
       PROJECTION, null, null, ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME+" ASC"); 

     Cursor c = cl.loadInBackground(); 
     adapter = new SimpleCursorAdapter(this, 
       R.layout.row_view, // Use a template 
                // that displays a 
                // text view 
       c, // Give the cursor to the list adapter 
       new String[] { ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME, ContactsContract.CommonDataKinds.Phone.NUMBER}, 
       new int[] { R.id.checkedTextView, R.id.textView},0); 

     setListAdapter(adapter); 
    } 

    @Override 
    public void onItemClick(AdapterView<?> parent, View v, int position, long id) { 
     //what to do when an item is clicked 
     CheckedTextView checkedTextView = (CheckedTextView) v.findViewById(R.id.checkedTextView); 
     Toast.makeText(this, checkedTextView.getText(), Toast.LENGTH_SHORT).show(); 
    } 
} 

나는 두 개의 레이아웃을했습니다 있습니다

입니다 그것을 사용했습니다 하나는 목록보기 자체 ( multiple_contacts)이고 여기에 제공된 레이아웃 ( row_view)은 목록보기의 각 항목에 대한 레이아웃입니다. 내가 무엇을 필요로하는지 multiple_contacts에서 작성하는 것입니다 setContentView(R.layout.multiple_contacts);

관련 문제