我可以检索联系人,但他们没有正确显示

时间:2012-03-10 16:08:48

标签: android android-layout dynamic android-widget

使用此代码:

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#

...所以,联系人正在被检索,但小部件正在不知不觉地被放到模拟器屏幕上,或者看起来似乎是(并且ids /名称没有显示在复选框旁边)。

更新: 我认为将代码更改为以下代码可能会有效,但它会让我进入Eclipse中的Debug Perspective。

小部件的后续实例化不能与已创建的小部件具有相同名称(例如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();
        }
    }
}

1 个答案:

答案 0 :(得分:1)

您不应该使用多个TextView - 在您的示例中,您将始终限制为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>

我还没有测试过这种布局,但它不应该很远。