有了這個代碼:我可以檢索接觸,但他們沒有正確顯示
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();
}
}
}
你希望你的複選框+文本在一行嗎?如果不嘗試將'LinearLayout'的方向設置爲'VERTICAL'。 – Luksprog 2012-03-10 16:23:41
是的,我確實希望每行都有三個複選框。對於每個聯繫人,一行中應該有三個複選框和聯繫人姓名。 – 2012-03-11 03:45:05