Android - 选项卡中的动态微调器 - 添加项失败

时间:2012-03-13 05:47:05

标签: android tabs spinner android-tabhost

我有一个标准的TabHost,其中包含一个包含Spinner的选项卡。我试图通过联系信息填充微调器选项。一切似乎都在起作用,除了当步骤更新微调器的内容时​​,它不会发生,因此微调器始终保持空白。我想我需要帮助以允许添加发生的方式正确地抓住Spinner适配器的句柄。这是我的代码:

    public class Page2Activity extends Activity 
implements OnClickListener, OnLongClickListener {

private RadioButton rb1;  
private RadioButton rb2;    
private RadioButton rb3;
private RadioButton rb4;

private TextView t1;
private Spinner s1;
private ArrayAdapter<CharSequence> m_adapterForSpinner;

private static final int CONTACT_PICKER_RESULT = 1001;

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.tabpage2);

    rb1 = (RadioButton) this.findViewById(R.id.DE);
    rb2 = (RadioButton) this.findViewById(R.id.DL);     
    rb3 = (RadioButton) this.findViewById(R.id.DLL);
    rb4 = (RadioButton) this.findViewById(R.id.DI);     

    t1 = (TextView) this.findViewById(R.id.textView1);
    s1 = (Spinner) this.findViewById(R.id.spinner1);

m_adapterForSpinner = new ArrayAdapter(this, android.R.layout.simple_spinner_item);
m_adapterForSpinner.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
s1.setAdapter(m_adapterForSpinner);

    s1.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() { 

        @Override 
        public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3) { 
            String selection = s1.getSelectedItem().toString();
            String[] number = selection.split(" -> ");
            number[1] = number[1].replace("-", "");
            t1.setText(number[1]);
        } 

        @Override 
        public void onNothingSelected(AdapterView<?> arg0) { 
        // TODO Auto-generated method stub 

        } 
        }); 

}

上面的代码设置了微调器和微调器选择处理程序,当我在代码中手动填充微调器的内容时​​,它似乎工作正常。但是,我试图以编程方式将联系人选择器中的联系信息列为微调器中的项目。这是Picker的处理程序。这里的所有内容似乎都是基于通过日志验证来查找感兴趣的联系人项目,但由于某种原因,它实际上并没有在选项卡上更新Spinner中的选项。我认为这是我在下面使用m_adapterForSpinner导致问题,但我不确定或如何解决它。建议?

        protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (resultCode == RESULT_OK) {
        switch (requestCode) {
        case CONTACT_PICKER_RESULT:
            Uri result = data.getData();
            Log.v("D", "Got a result: " + result.toString());
            Uri myURI = Uri.parse(result.toString()) ;
            ContentResolver cr = getContentResolver(); 
            Cursor cur = cr.query(myURI, 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));
                Toast.makeText(this, name, 500).show();

                if (Integer.parseInt(cur.getString(cur.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0) {
                    //Query phone here.  Covered next
                    if (Integer.parseInt(cur.getString(
                            cur.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0) {
                            Cursor pCur = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = ?", new String[]{id}, null);

                            final int contactNumberColumnIndex = pCur.getColumnIndex(Phone.NUMBER);
                            final int contactTypeColumnIndex = pCur.getColumnIndex(Phone.TYPE);

                            while (pCur.moveToNext()) {
                                // Do something with phones
                                String typename = null;
                                final String number = pCur.getString(contactNumberColumnIndex);
                                final int type = pCur.getInt(contactTypeColumnIndex);
                                switch (type) {
                                    case Phone.TYPE_ASSISTANT:
                                        typename= "Assistant";
                                        break;
                                    case Phone.TYPE_CALLBACK:
                                        typename = "Callback";
                                        break;
                                        break;
                                    default:
                                        typename = "Undefined";
                                        break;
                                }

//将项目添加到OnActivityResult回调中的Spinner中的选项

    m_adapterForSpinner.add(typename + " -> " + number);
                            } 
                    pCur.close();
                }}}}
            break;
        }} 
    else {
        // gracefully handle failure
        Toast.makeText(this, "Contact Selection Failed", 5000);
    }
}

1 个答案:

答案 0 :(得分:0)

致电m_adapterForSpinner.notifyDataSetChanged()更新用户界面。

或致电setNotifyOnChange(boolean notifyOnChange)这会自动更新用户界面。所以你不应该明确地致电m_adapterForSpinner.notifyDataSetChanged()