2012-03-10 46 views
0

有了這個代碼:我可以檢索接觸,但他們沒有正確顯示

public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
     setContentView(R.layout.ondemandandautomatic_dynamicauthorize); 
LinearLayout llay = new LinearLayout(this); 
llay.setOrientation(LinearLayout.HORIZONTAL); 

LinearLayout.LayoutParams llp = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT); 
llp.weight = 1.0f; 

// Contacts data snippet adapted from http://saigeethamn.blogspot.com/2011/05/contacts-api-20-and-above-android.html 
ContentResolver cr = getContentResolver(); 
Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null); 
if (cur.getCount() > 0) { 
    while (cur.moveToNext()) { 
      String id = cur.getString(cur.getColumnIndex(ContactsContract.Contacts._ID)); 
      String name = cur.getString(cur.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME)); 

      CheckBox cb = new CheckBox(getApplicationContext()); 
      cb.setText(id+name); 
      cb.setLayoutParams(llp); 
      llay.addView(cb); 

     } 
    } 
// >Contacts data 

ScrollView svh = (ScrollView) findViewById(R.id.scrollViewHost); 
svh.addView(llay);   

// One cat on stackOverflow said to do this, another said it would be unnecessary 
svh.invalidate(); 

}

...和佈局的xml:

<?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="horizontal" > 

     <TextView 
      android:id="@+id/textView1" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_margin="2dip" 
      android:text="@string/demand" 
      android:textAppearance="?android:attr/textAppearanceSmall" /> 

     <TextView 
      android:id="@+id/textView2" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_margin="2dip" 
      android:text="@string/time" 
      android:textAppearance="?android:attr/textAppearanceSmall" /> 

     <TextView 
      android:id="@+id/textView3" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_margin="2dip" 
      android:text="@string/space" 
      android:textAppearance="?android:attr/textAppearanceSmall" /> 

     <TextView 
      android:id="@+id/textView4" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_margin="2dip" 
      android:text="@string/contact" 
      android:textAppearance="?android:attr/textAppearanceMedium" /> 

    <ScrollView 
     android:id="@+id/scrollViewHost" 
     android:fillViewport="true" 
     android:layout_width="wrap_content" 
     android:layout_height="match_parent" > 

    </ScrollView> 

</LinearLayout> 

...我得到你可以在這裏看到:

http://warbler.posterous.com/temporary-post-to-display-a-cosmetic-problem#

...所以,聯繫人正在檢索,但小部件正在仿真屏幕上無處不在,或似乎(以及ID /名稱不顯示旁邊的複選框)。

更新: 我認爲將代碼更改爲以下代碼可能會起作用,但它會讓我進入Eclipse中的調試透視圖。

問題是小部件的後續實例化不能與已創建的實例相同(如cbOnDemand)?如果是這樣,我該如何解決這個問題?

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.ondemandandautomatic_dynamicauthorize); 

    ScrollView svh = (ScrollView) findViewById(R.id.scrollViewHost); 

    // Contacts data snippet adapted from 
    // http://saigeethamn.blogspot.com/2011/05/contacts-api-20-and-above-android.html 
    ContentResolver cr = getContentResolver(); 
    Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI, null, 
      null, null, null); 
    if (cur.getCount() > 0) { 
     while (cur.moveToNext()) { 
      String id = cur.getString(cur 
        .getColumnIndex(ContactsContract.Contacts._ID)); 
      String name = cur 
        .getString(cur 
          .getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME)); 

      // Create a Linear Layout for each contact? 
      LinearLayout llay = new LinearLayout(this); 
      llay.setOrientation(LinearLayout.HORIZONTAL); 

      LinearLayout.LayoutParams llp = new LayoutParams(
        LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT); 
      llp.weight = 1.0f; 

      CheckBox cbOnDemand = new CheckBox(getApplicationContext()); 
      cbOnDemand.setLayoutParams(llp); 
      llay.addView(cbOnDemand); 

      CheckBox cbTime = new CheckBox(getApplicationContext()); 
      cbTime.setLayoutParams(llp); 
      llay.addView(cbTime); 

      CheckBox cbSpace = new CheckBox(getApplicationContext()); 
      cbSpace.setLayoutParams(llp); 
      llay.addView(cbSpace); 

      TextView tv = new TextView(getApplicationContext()); 
      tv.setText(id + name); 
      tv.setLayoutParams(llp); 
      llay.addView(tv); 

      svh.addView(llay); 
      // One cat on stackOverflow said to do this, another said it 
      // would be unnecessary 
      svh.invalidate(); 
     } 
    } 
} 
+0

你希望你的複選框+文本在一行嗎?如果不嘗試將'LinearLayout'的方向設置爲'VERTICAL'。 – Luksprog 2012-03-10 16:23:41

+0

是的,我確實希望每行都有三個複選框。對於每個聯繫人,一行中應該有三個複選框和聯繫人姓名。 – 2012-03-11 03:45:05

回答

1

你不應該使用一些TextViews的 - 在你的榜樣,你會總是侷限於4而是使用一個ListView,你可以添加零所或多次接觸到它。下面是XML的例子(注意在ListView控件的ID):

<?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="fill_parent" 
android:orientation="vertical">  

<ListView android:id="@id/android:list" 
    android:layout_width="fill_parent" 
    android:layout_height="0dip" 
    android:layout_weight="0.7" /> 

<TextView android:id="@android:id/empty" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_weight="0.7"    
     android:text="@string/noFriendsList"/> 

</LinearLayout> 

TextView的僅僅是有顯示一些文本時,列表視圖是空的(也是特殊的ID就可以了)。在你的代碼,你需要創建擴展一個ArrayAdapter類並將其設置爲適配器列表視圖,例如:

ListView friendList = (ListView)findViewById(android.R.id.list); 
    initialiseList(); 
    friendList.setAdapter(mFriends); 

其中mFriends是我的自定義ArrayAdapter。有很多在線使用ArrayAdapter和ListView的例子。

編輯:當您在活動創建自定義列表適配器,就像這樣:

mFriends = new FriendListAdapter(this, R.layout.friend_list_row, friendList); 

你可以使用它描述了每一行應該如何被擺出來,你的情況的XML佈局應是這樣的:

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content"> 

<CheckBox android:id="@+id/cb1" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignParentLeft="true"/> 

<CheckBox android:id="@+id/cb2" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:paddingLeft="5dip" 
    android:layout_toRightOf="@+id/cb1"/> 

<CheckBox android:id="@+id/cb3" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:paddingLeft="5dip" 
    android:layout_toRightOf="@+id/cb2"/> 

<TextView android:id="@+id/friendMobile" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content"   
    android:textSize="12sp"    
    android:textStyle="bold" 
    android:paddingLeft="5dip" 
    android:layout_toRightOf="@+id/cb3" /> 


</RelativeLayout> 

我沒有測試過這種佈局,但它不應該是不遠了。

+0

我在佈局xml中添加的四個textViews就像一個「標題」 - 我總是隻需要那四個點。在動態創建的窗口小部件中,將其添加到ScrollView中,每個聯繫人都應該有一行,並且每行都應該有三個複選框和一個textView(聯繫人姓名)。 – 2012-03-11 03:49:14

+0

我更新了原始帖子,並嘗試了另一次失敗的嘗試。 – 2012-03-11 04:05:58

+0

我最初嘗試使用ListView,因爲這是做這種事的常用方式,但因爲我需要每個行/行/聯繫人都有三個複選框,所以我無法使其工作。這就是我走下這條道路的原因。它似乎會工作,但現在我堅持在上面的問題。 – 2012-03-11 04:09:25