2011-11-02 132 views
1

enter image description here如何在android中動態添加文本視圖到列表視圖?

我需要兩個文本視圖添加到列表視圖,如圖figure..what我需要的是,在接觸按鈕的點擊姓名和號碼的類型自動進入我的列表視圖。 。我無法將文本視圖添加到列表視圖,因爲我只能看到手機的聯繫人列表...請好心的幫助我..請提前感謝..這裏是代碼。

import android.app.ListActivity; 

import android.os.Bundle; 
import android.provider.ContactsContract; 
import android.content.Intent; 
import android.database.Cursor; 

public class TestActivity extends ListActivity 
{ 



    private static final int PICK_CONTACT = 0; 

    /** Called when the activity is first created. */ 
    @Override 
    protected void onCreate(Bundle savedInstanceState) 
    { 
     super.onCreate(savedInstanceState);  
    //content uri provide directory of people 
     Intent intentContact = new Intent(Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI); 

     startActivityForResult(intentContact, PICK_CONTACT); 
    }//onCreate 

    public void onActivityResult(int requestCode, int resultCode, Intent intent) 
    { 

     if (requestCode ==PICK_CONTACT) 
     { 
      getContactInfo(intent);    
     } 



    }//onActivityResult 

    protected void getContactInfo(Intent intent) 
    { 
String name; 
     Cursor cursor = managedQuery(intent.getData(), null, null, null, null);  
     while (cursor.moveToNext()) 
     {   
      //contains row id 
      String contactId = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID)); 

      name = cursor.getString(cursor.getColumnIndexOrThrow(ContactsContract.Contacts.DISPLAY_NAME)); 
     //whether contact list atleast have a single contact or not 
      String hasPhone = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER)); 

      if (hasPhone.equalsIgnoreCase("1")) 
       hasPhone = "true"; 
      else 
       hasPhone = "false" ; 

      if (Boolean.parseBoolean(hasPhone)) 
      { 
      Cursor phones = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = "+ contactId,null, null); 
      while (phones.moveToNext()) 
      { 
       String phoneNumber = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER)); 
      }//end 
      phones.close(); 
      }//end 

     cursor.close(); 
     }//end while 


    }//end method 
}//end class........... 

這是xml文件

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    > 
<ListView 
    android:id="@+id/android:list" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    /> 
<TextView 
    android:id="@+id/android:empty" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    /> 
</LinearLayout>........ 


<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="?android:attr/listPreferredItemHeight" 
    android:padding="6dip"> 

    <LinearLayout 
     android:orientation="vertical" 
     android:layout_width="0dip" 
     android:layout_weight="1" 
     android:layout_height="fill_parent"> 
     <TextView 
      android:id="@+id/toptext" 
      android:layout_width="fill_parent" 
      android:layout_height="0dip" 
      android:layout_weight="1" 
      android:gravity="center_vertical" 
     /> 
     <TextView 
      android:layout_width="fill_parent" 
      android:layout_height="0dip" 
      android:layout_weight="1" 
      android:id="@+id/bottomtext" 
      android:singleLine="true" 
      android:ellipsize="marquee" 
     /> 
    </LinearLayout> 
</LinearLayout> ... 
+0

[普萊斯看看這個例子(HTTP ://developer.android.com/resources/samples/ApiDemos/src/com/example/android/apis/view/List3.html) –

+0

任何簡單的方法或更多的解釋性例子... –

回答

相關問題