Android 我可以检索联系人,但他们没有正确显示
使用此代码:Android 我可以检索联系人,但他们没有正确显示,android,android-layout,dynamic,android-widget,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.HOR
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>
您不应该使用多个文本视图-在您的示例中,您将始终限制为4个。相反,使用ListView,您可以向其中添加零个或多个联系人。下面是一个xml示例(请注意ListView上的id): 其中mFriends是我的定制阵列适配器。在线使用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>
我还没有测试过这个布局,但它应该离我不远。是否希望复选框+文本在一行中?如果没有,请尝试将
线性布局的方向设置为垂直。是的,我希望每行有三个复选框。对于每个联系人,一行中应该有三个复选框和联系人姓名。我在布局xml中添加的四个文本视图就像一个“标题”-我总是需要这四个。在添加到ScrollView的动态创建的小部件中,每个联系人都应该有一行,每行都应该有三个复选框和一个textView(联系人名称)。我更新了我的原始帖子,但另一次尝试失败。我最初尝试使用ListView,因为这是执行类似操作的常用方法,但是因为我需要为每一行/每一联系人设置三个复选框,所以我无法使其正常工作。这就是我走这条路的原因。它似乎会起作用,但目前我仍停留在上述问题上。我编辑了我的原始答案,向您展示了如何为行使用布局,该布局应为每行提供3个复选框和一个文本视图。谢谢,我之前确实有类似的内容,但无法确定如何知道选中了哪个复选框。我需要存储数据,如“联系人1的ckbx1已选中,联系人1的ckbx2未选中”等。我想我最新的尝试需要知道的是,将每行的联系人id分配给复选框的标记属性(或类似属性;这在其他语言中称为,希望Java/Android中也有标记属性).
ListView friendList = (ListView)findViewById(android.R.id.list);
initialiseList();
friendList.setAdapter(mFriends);
mFriends = new FriendListAdapter(this, R.layout.friend_list_row, friendList);
<?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>