在Android中拥有多个微调功能 [英] Having Multiple spinners in android

查看:23
本文介绍了在Android中拥有多个微调功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

谁能告诉我如何在只有一个onItemSelected()的情况下添加多个微调工具。 现在我正在做这样的事情: 微调对象s[]=新微调对象[5]; 我正在使用它来显示微调器中的层次结构。

对于index=0;它工作得很好。但下一个索引出现问题,因为只有一个onItemSelected()。

您对此有什么建议?

注意:我正在编写代码,以便动态创建微调控件并填充相应的光标。到目前为止,游标不是问题。

谢谢!

推荐答案

Public class HomePage extends Activity implements OnClickListener, OnItemSelectedListener{

int orientation;
DataBaseHelper myDbHelper;
Cursor c3, c2;
SimpleCursorAdapter adapterData;
int index = 0;
Spinner s[] = new Spinner[5];
TableRow row;

@SuppressWarnings("static-access")
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    myDbHelper = new DataBaseHelper(this);

    //-----------Customized Adapter for Specialties----Database binded//
    String[] columns2 =  {myDbHelper.KEY_ID, myDbHelper.KEY_TITLE};
    String table2 = myDbHelper.DB_TABLE_NAME;
    c2 = myDbHelper.getHandle().query(table2, columns2, "type = 'SuperTab'", null, myDbHelper.KEY_TITLE, null, myDbHelper.KEY_TITLE);
    startManagingCursor(c2);
    String[] from = new String[]{myDbHelper.KEY_TITLE};
    int[] to = new int[]{android.R.id.text1};
    adapterData =
          new SimpleCursorAdapter(this, android.R.layout.simple_spinner_item, c2, from, to );
    adapterData.setDropDownViewResource( android.R.layout.simple_spinner_dropdown_item );


    //-----------Customized Adapter for Location-----Database binded--//
        String[] columns3 =  {myDbHelper.KEY_ID, myDbHelper.KEY_LOCATION};
        String table3 = myDbHelper.DB_TABLE_NAME_LOCATION;
        c3 = myDbHelper.getHandle().query(table3, columns3, null, null, myDbHelper.KEY_LOCATION, null, null);
        startManagingCursor(c3);
        String[] from3 = new String[]{myDbHelper.KEY_LOCATION};
        int[] to3 = new int[]{android.R.id.text1};
        SimpleCursorAdapter adapterData3 =
              new SimpleCursorAdapter(this, android.R.layout.simple_spinner_item, c3, from3, to3 );
            adapterData3.setDropDownViewResource( android.R.layout.simple_spinner_dropdown_item );
    //--------------------------------------------------------------//

    Spinner spinner = (Spinner) findViewById(R.id.spinner);
    ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
            this, R.array.specialty_array, android.R.layout.simple_spinner_item);
    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

    spinner.setAdapter(adapterData);      

    Spinner spinner2 = (Spinner) findViewById(R.id.spinner2);
    adapter = ArrayAdapter.createFromResource(
            this, R.array.office_array, android.R.layout.simple_spinner_item);
    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    spinner2.setAdapter(adapterData3);

    Button search = (Button)this.findViewById(R.id.Search);
    search.setOnClickListener(this);

    Button searchNearMe = (Button)this.findViewById(R.id.Near_Me);
    searchNearMe.setOnClickListener(this);

    spinner.setOnItemSelectedListener(this);  

    orientation = getResources().getConfiguration().orientation;

    s[index] =  new Spinner(this);
// s[index].setOnItemSelectedListener(this);
    row = new TableRow(this);
}

@SuppressWarnings("static-access")

public void onItemSelected(AdapterView<?> parent, View view, int pos,
        long id) {
    TextView tv = (TextView)this.findViewById(R.id.spinnerText2);   


    Log.w("ID: ", "index=" + adapterData.getItemId(pos) + " " + c2.getCount() + " " + id + " callee " + view);

    String sql = "sql" +                     id                                                                 ;

    c2 = myDbHelper.getHandle().rawQuery(sql, null);
    Log.w("ID Adapter: ", "index=" + adapterData.getItemId(pos) + " " + c2.getCount());

    int thisOrientation = getResources().getConfiguration().orientation;

    if (thisOrientation != orientation) {
        return;
    }       

    //--------sets hierarchy spinner to corresponding database-----//
    String[] from = new String[]{myDbHelper.KEY_TITLE};
    int[] to = new int[]{android.R.id.text1};
    SimpleCursorAdapter adapterDataNew =
          new SimpleCursorAdapter(this, android.R.layout.simple_spinner_item, c2, from, to );
    adapterDataNew.setDropDownViewResource( android.R.layout.simple_spinner_dropdown_item );
    Log.w("Adapter Data New", "index=" + adapterDataNew.getItemId(pos) + " " + adapterDataNew.getCount());
    //------------------------------------------------------------//

    //-----------------Table Row for Landscape mode---------------//

    TableLayout tl = (TableLayout)this.findViewById (R.id.Table);
    TableLayout.LayoutParams tlp = new TableLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT); 
    tlp.weight = 1;
    tlp.setMargins(20, 15, 20, 0);
    row.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));       

    //------------------------------------------------------------//

    //------------------find Relative Layout by ID----------------//
    RelativeLayout relative = (RelativeLayout)this.findViewById(R.id.rlay);
    RelativeLayout.LayoutParams p = new RelativeLayout.LayoutParams(
            android.view.ViewGroup.LayoutParams.FILL_PARENT, 
            android.view.ViewGroup.LayoutParams.WRAP_CONTENT);
    p.addRule(RelativeLayout.BELOW, R.id.spinner);
    p.setMargins(15, 5, 15, 5);
    //------------------------------------------------------------//

    if (orientation != getResources().getConfiguration().ORIENTATION_LANDSCAPE) {
        if (c2.getCount() > 0) {                
            relative.removeView(s[index]);
            relative.addView(s[index], android.view.ViewGroup.LayoutParams.MATCH_PARENT, 
                    android.view.ViewGroup.LayoutParams.MATCH_PARENT);
            s[index].setVisibility(view.VISIBLE);
            s[index].setPromptId(R.string.Sub);
            s[index].setAdapter(adapterDataNew);
            s[index].setLayoutParams(p);
            tv.setPadding(10, 50, 10, 0);

            Log.w("Index value ", " " + index);

        }
        else {
            s[index].setVisibility(view.GONE);
            relative.removeView(s[index]);
            tv.setPadding(0, 0, 0, 0);
        }
    }
    else {
            if (c2.getCount() > 0) {
                tl.removeView(row); 
                row.removeView(s[index]);                   
                Log.w("Row Parent " + row.getParent()," tl child " + tl.getChildCount());
                tl.addView(row, index + 4 , tlp);
                row.addView(s[index]);
                Log.w("Row Parent After Add" + row.getParent()," tl child After Add" + tl.getChildCount());
                row.setVisibility(view.VISIBLE);                    
                s[index].setVisibility(view.VISIBLE);
                s[index].setPromptId(R.string.Sub);
                s[index].setAdapter(adapterDataNew);

                Log.w("ID: Landscape mode", "index=" + adapterDataNew.getItemId(pos) + " " + adapterDataNew.getCount());
            }   
            else {
                s[index].setVisibility(view.GONE);
                row.setVisibility(view.GONE);
                row.removeView(s[index]);
                tl.removeView(row);                 
            }

    }

}


public void onNothingSelected(AdapterView<?> arg0) {        

}

@Override
public void onClick(View v) {
    EditText et1 = (EditText)this.findViewById(R.id.entry);
    EditText et2 = (EditText)this.findViewById(R.id.entry2);
    String lastName = et1.getText().toString();
    String firstName = et2.getText().toString();
    if (lastName.length() == 0) {
        lastName = "%";
    }
    if (firstName.length() == 0) {
        firstName = "%";
    }
    Bundle bundle = new Bundle();
    bundle.putString("LastName", lastName);
    bundle.putString("FirstName", firstName);
    switch(v.getId()) {
        case R.id.Search:
            Intent i = new  Intent(this, Search.class);
            i.putExtras(bundle);
            startActivity(i);
            c2.close();
            c3.close();
            break;
        case R.id.Near_Me:
            Intent j = new  Intent(this, SearchNearMe.class);
            startActivity(j);

    }       
}
}

这篇关于在Android中拥有多个微调功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆