在片段中显示联系人列表

时间:2014-01-27 06:08:57

标签: android android-listview android-fragments android-listfragment

我是android的新手。所以这是我的问题。我需要在fragment中生成联系人列表。我已经尝试过扩展listActivity和listFragment。在列表活动中我得到了结果但是在使用listFragment时没有错误,但是应用程序崩溃了。这是我的完整代码。

Invite.java

    package com.kicka;


import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;

import com.kicka.R;

    public class Invite extends Activity {

        /** Called when the activity is first created. */
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);

            setContentView(R.layout.invite_friends);



        }

        public void fbInvite(View view)
        {
            Intent intent = new Intent(getApplicationContext(),FindFbFriends.class);
            startActivity(intent);
        }

        public void contactInvite(View view)
        {
            Intent intent = new Intent(getApplicationContext(),FindPhnFriends.class);
            startActivity(intent);
        }
    }

FindPhnFriends.java

    package com.kicka;


import android.os.Bundle;
import android.support.v4.app.FragmentActivity;

public class FindPhnFriends extends FragmentActivity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.fragment_contact_find_friend);

    }    


}

fragment_contact_find_friend.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" >

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical" >

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="vertical" >

                <TextView
                    android:id="@+id/textView1"
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:background="#ff5240"
                    android:gravity="center_horizontal"
                    android:padding="14dp"
                    android:text="Invite Your Contacts"
                    android:textAppearance="?android:attr/textAppearanceLarge"
                    android:textSize="24sp"
                    android:textStyle="bold"
                    android:typeface="serif" />

                <TextView
                    android:id="@+id/textView2"
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:gravity="center_horizontal"
                    android:paddingBottom="5dp"
                    android:paddingTop="10dp"
                    android:text="Add friends to share Perks and earn rewards!"
                    android:textAppearance="?android:attr/textAppearanceMedium" />
            </LinearLayout>

             <fragment
                android:id="@+id/phn_contacts"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                class="com.kicka.PhnContact"
                /> 

        </LinearLayout>
    </ScrollView>

</RelativeLayout>

PhnContact.java

package com.kicka;


import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.support.v4.app.ListFragment;
import android.support.v4.widget.SimpleCursorAdapter;
import android.widget.ListView;

public class PhnContact extends ListFragment{



    @Override
    @SuppressWarnings("deprecation")
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        Cursor cur = getContacts();

        ListView lv = getListView();

       String[] fields = new String[] {ContactsContract.Data.DISPLAY_NAME};


    SimpleCursorAdapter adapter = 
                new SimpleCursorAdapter(getActivity(), 
                                        R.layout.phone_contacts_list,
                                        cur,
                                        fields,
                                        new int[] {R.id.phn_contact_name});
          lv.setAdapter(adapter);         
    } 





    private Cursor getContacts() {  
        // Run query     
        Uri uri = ContactsContract.Contacts.CONTENT_URI;

        String[] projection = 
                new String[]{ ContactsContract.Contacts._ID,
                              ContactsContract.Contacts.DISPLAY_NAME }; 
            String selection = null;
            String[] selectionArgs = null;  
            String sortOrder = ContactsContract.Contacts.DISPLAY_NAME + 
                " COLLATE LOCALIZED ASC";  
            return getActivity().managedQuery(uri, projection, selection, selectionArgs, sortOrder);
    }

}

phone_contacts_list.xml

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:paddingBottom="5dp"
    android:background="#f7f7f8" >

    <ImageView
        android:id="@+id/phn_contact_img"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/kick_a"
        android:padding="8dp"
        android:layout_weight="0" />

    <TextView
        android:id="@+id/phn_contact_name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_vertical|left"
        android:lines="2"
        android:layout_weight="3"
        android:paddingLeft="8dp"
        android:text="FirstName LastName"
        android:textAppearance="?android:attr/textAppearanceButton" />

    <CheckBox
        android:id="@+id/phn_contact_select"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="8dp"
        android:layout_gravity="center_vertical"
        android:checked="false"
        android:button="@drawable/plus" />
</LinearLayout>

0 个答案:

没有答案