如何findviewbyid到尚未创建的项目?

时间:2014-01-13 09:09:36

标签: android android-layout android-listview

我正在java代码中创建一个Checkbox,而程序运行如下:

void openContactsList() {
    // TODO Auto-generated method stub
    LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
            LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
    LinearLayout.LayoutParams butparams = new LinearLayout.LayoutParams(
            LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
    // getting phone number and name of the contacts
    Uri uri = ContactsContract.CommonDataKinds.Phone.CONTENT_URI;
    String[] projection = new String[] {
            ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME,
            ContactsContract.CommonDataKinds.Phone.NUMBER };

    Cursor people = getContentResolver().query(uri, projection, null, null,
            null);
    int indexName = people.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME);
    int indexNumber = people.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER);
    j = 0;
    people.moveToFirst();
    ContactsDataBase entry = new ContactsDataBase(Contacts.this);
    entry.open();
    do {
        LinearLayout row = new LinearLayout(this);
        row.setLayoutParams(params);
        for (j = 0; (j < 3) && next; j++) {
            // create the person and put it in the db table
            String name = people.getString(indexName);
            String number = people.getString(indexNumber);
            entry.createEntry(name, number);
            // create every person Image button
            CheckBox btnTag = new CheckBox(this);
            butparams.setMargins(j, 10, 0, 0);
            btnTag.setGravity(10);
            btnTag.setLayoutParams(butparams);
            btnTag.setText(number + "\n" + name);
            btnTag.setTextSize(10);
            btnTag.setTag("Button " + name);
            try {
                int num = Integer.parseInt(number);
                btnTag.setId(num);
            } catch (NumberFormatException nfe) {
                // Handle parse error.
            }
            // btnTag.setBackgroundResource(R.drawable.have_a_nice_day);
            btnTag.getLayoutParams().width = 70;
            btnTag.getLayoutParams().height = 70;
            next = people.moveToNext();
            row.addView(btnTag);
        }
        layout12.addView(row);
    } while (next);
    entry.close();
    people.close();
}

所以每个复选框都是我保存在手机中的人,而Id是电话号码 现在我想检查它们是否被检查,而不是制作一个按钮将已检查的那些移动到其他LinearLayout ...但问题是我不知道如何检查它们,如果我不能得到Id因为计算机找不到它们什么时候他们还没有创建,他们还没有在R.java课程中! 好吧,我在SQL上创建了一个Db,所以我得到了电话号码,我试着一个接一个地去。类似的东西:

{ 
    int num; //lets say that the num is the phonenumber=the Id 
    Friends = (CheckBox) findviewbyid(R.id.num)   //this it the problem
    if(Friends.isChecked()){  // and i got one by one on the Db and changing the number
        ...  //all i want to do is here so i wont write it (its not the point here)
    }
}

所以我不能使用这个数字,因为它不在R.java 如何查看这些复选框。也许我可以在程序中搜索特定LinearLayout中的所有Id 谢谢你的帮助,希望你能理解我想要的东西..

1 个答案:

答案 0 :(得分:0)

我不确定您是否可以使用10位以上的电话号码作为查看ID(整数)。但如果它有效,那么你可以使用

Friends = (CheckBox) findviewbyid(phonenumber)
相关问题