0

嘿伙计们,我的代码如下所示,它应该从第一个微调器中选择项目 - 诗人,然后根据输入诗 1,2 或 3 显示另一个微调器。一旦从第二个微调器中选择了一个项目,它应该显示文本视图。

我的问题是,有时它可以工作,有时它会随机选择要显示的微调器,几乎每次当我从微调器 2 中选择一个项目时,文本视图几乎从不显示,而当它通常是标题为树的文本视图时.

谁能帮忙?我没有看到任何错误,所以我很困惑

代码:

public class Poets extends Activity implements  OnItemSelectedListener{

//defining variables
Spinner PoetSpinner, PoemSpinner1, PoemSpinner2, PoemSpinner3;
String Poets[] = {"","Robert Burns", "James Beattie", "Duncan MacGregor Crerar"};
String Poems1[] ={"", "Tam O Shanter", "Auld Lang Syne"};
String Poems2[] ={"", "Judgement Of Paris", "The Minstrel"};
String Poems3[] ={"", "A Poem Commemorating Robbie Burns", "My Bonnie Rowan Tree"};

TextView Tam, Auld, Judge, TheMin, rob, tree ;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    //set to fullscreen mode
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN );
    //set content view 
    setContentView(R.layout.poet);

    initialize(); //call this method
}

public void initialize(){

    //initializing items that are in the class
    PoetSpinner = (Spinner) findViewById(R.id.spPoet);
    PoemSpinner1 = (Spinner) findViewById(R.id.spPoem1);
    PoemSpinner2 = (Spinner) findViewById(R.id.spPoem2);
    PoemSpinner3 = (Spinner) findViewById(R.id.spPoem3);
    Tam = (TextView) findViewById(R.id.tvSelectPoem1);
    Auld = (TextView) findViewById(R.id.tvSelectPoem2);
    Judge = (TextView) findViewById(R.id.tvSelectPoem3);
    TheMin = (TextView) findViewById(R.id.tvSelectPoem4);
    rob = (TextView) findViewById(R.id.tvSelectPoem5);
    tree = (TextView) findViewById(R.id.tvSelectPoem6);
    //setting up array adapters
    //
    //for each spinner
    ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this, R.array.poet_arrays, android.R.layout.simple_spinner_item);
    ArrayAdapter<CharSequence> adapter2 = ArrayAdapter.createFromResource(this, R.array.poem_arrays1, android.R.layout.simple_spinner_item);
    ArrayAdapter<CharSequence> adapter3 = ArrayAdapter.createFromResource(this, R.array.poem_arrays2, android.R.layout.simple_spinner_item);
    ArrayAdapter<CharSequence> adapter4 = ArrayAdapter.createFromResource(this, R.array.poem_arrays3, android.R.layout.simple_spinner_item);
    // Specify the layout to use when the list of choices appears
    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    adapter2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    adapter3.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    adapter4.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    // Apply the adapter to the spinner
    PoetSpinner.setAdapter(adapter);
    PoetSpinner.setOnItemSelectedListener(this);

    PoemSpinner1.setAdapter(adapter2);
    PoemSpinner1.setOnItemSelectedListener(this);

    PoemSpinner2.setAdapter(adapter3);
    PoemSpinner2.setOnItemSelectedListener(this);

    PoemSpinner3.setAdapter(adapter4);
    PoemSpinner3.setOnItemSelectedListener(this);

}

@Override
public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {

      String val = Poets[pos];
      String val2 = Poems1[pos];
      String val3 = Poems2[pos];
      String val4 = Poems3[pos];
       if (val.equals("Robert Burns")) {
          PoemSpinner2.setVisibility(Spinner.GONE);
          PoemSpinner1.setVisibility(Spinner.VISIBLE);
          PoemSpinner3.setVisibility(Spinner.GONE);
       }
       if (val.equals("James Beattie")) {
              PoemSpinner2.setVisibility(Spinner.VISIBLE);
              PoemSpinner1.setVisibility(Spinner.GONE);
              PoemSpinner3.setVisibility(Spinner.GONE);
           }
       if (val.equals("Duncan MacGregor Crerar")) {
            PoemSpinner2.setVisibility(Spinner.GONE);
            PoemSpinner1.setVisibility(Spinner.GONE);
            PoemSpinner3.setVisibility(Spinner.VISIBLE);
           }
       if (val2.equals("Tam O Shanter")) {
            Tam.setVisibility(View.VISIBLE);

            Auld.setVisibility(View.GONE);
            Judge.setVisibility(View.GONE);
            TheMin.setVisibility(View.GONE);
            tree.setVisibility(View.GONE);
            rob.setVisibility(View.GONE);
           }
       if (val2.equals("Auld Lang Syne")) {
            Auld.setVisibility(View.VISIBLE);

            Judge.setVisibility(View.GONE);
            TheMin.setVisibility(View.GONE);
            tree.setVisibility(View.GONE);
            Tam.setVisibility(View.GONE);
            rob.setVisibility(View.GONE);
           }
       if (val3.equals("Judgment Of Paris")) {
            Judge.setVisibility(View.VISIBLE);

            TheMin.setVisibility(View.GONE);
            tree.setVisibility(View.GONE);
            Tam.setVisibility(View.GONE);
            Auld.setVisibility(View.GONE);
            rob.setVisibility(View.GONE);
           }
       if (val3.equals("The Minstrel")) {
            TheMin.setVisibility(View.VISIBLE);

            tree.setVisibility(View.GONE);
            Tam.setVisibility(View.GONE);
            Auld.setVisibility(View.GONE);
            Judge.setVisibility(View.GONE);
            rob.setVisibility(View.GONE);
           }
       if (val4.equals("A Poem Commemorating Robbie Burns")) {
            rob.setVisibility(View.VISIBLE);

            tree.setVisibility(View.GONE);
            Tam.setVisibility(View.GONE);
            Auld.setVisibility(View.GONE);
            Judge.setVisibility(View.GONE);
            TheMin.setVisibility(View.GONE);
           }
       if (val4.equals("My Bonnie Rowan Tree")) 
            tree.setVisibility(View.VISIBLE);

            Tam.setVisibility(View.GONE);
            Auld.setVisibility(View.GONE);
            Judge.setVisibility(View.GONE);
            TheMin.setVisibility(View.GONE);
            rob.setVisibility(View.GONE);
           }


@Override
public void onNothingSelected(AdapterView<?> parent) {


}
}
4

1 回答 1

1

我有一种感觉,你把事情复杂化了一点。你可以用 2 个微调器和一个 textview 来制作你想要的东西。第一个微调器将包含诗人,根据在此微调器中选择的诗人,您可以修改第二个微调器的内容,您可以在其中拥有所选诗人的诗歌。然后,当在第二个微调器中选择诗歌时,您可以在文本视图中显示所选诗歌的名称。我会尝试这样的事情:

    public class Poets extends Activity implements
OnItemSelectedListener {

  // defining variables
  Spinner PoetSpinner, PoemSpinner;
  String Poets[] = { "", "Robert Burns", "James Beattie",
  "Duncan MacGregor Crerar" };
  String Poems1[] = { "", "Tam O Shanter", "Auld Lang Syne" };
  String Poems2[] = { "", "Judgement Of Paris", "The Minstrel" };
  String Poems3[] = { "", "A Poem Commemorating Robbie Burns",
  "My Bonnie Rowan Tree" };

  TextView selectedPoem;

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    // set to fullscreen mode
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
      WindowManager.LayoutParams.FLAG_FULLSCREEN);
    // set content view
    setContentView(R.layout.poet);

    initialize(); // call this method
  }

  public void initialize() {

    // initializing items that are in the class
    PoetSpinner = (Spinner) findViewById(R.id.spPoet);
    PoemSpinner = (Spinner) findViewById(R.id.spPoem);
    // Hide the second spinner
    PoemSpinner.setVisibility(View.GONE);
    selectedPoem = (TextView) findViewById(R.id.tvSelectPoem1);
    // setting up array adapters
    //
    // for each spinner
    ArrayAdapter<CharSequence> adapter = ArrayAdapter
    .createFromResource(this, R.array.poet_arrays,
      android.R.layout.simple_spinner_item);
    // Specify the layout to use when the list of choices appears
    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    // Apply the adapter to the spinner
    PoetSpinner.setAdapter(adapter);
    PoetSpinner.setOnItemSelectedListener(this);

    PoemSpinner.setOnItemSelectedListener(new OnItemSelectedListener() {

      public void onItemSelected(AdapterView<?> parent, View view,
        int position, long id) {
        Object poem = parent.getItemAtPosition(position);
        selectedPoem.setText(poem.toString());
      }

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

      }
    });
  }

  public void onItemSelected(AdapterView<?> parent, View view, int pos,
    long id) {
    ArrayAdapter<CharSequence> adapter = null;
    switch (pos) {
      case 1:
      adapter = ArrayAdapter.createFromResource(this,
        R.array.poem_arrays1, android.R.layout.simple_spinner_item);
      break;
      case 2:
      adapter = ArrayAdapter.createFromResource(this,
        R.array.poem_arrays2, android.R.layout.simple_spinner_item);
      break;
      case 3:
      adapter = ArrayAdapter.createFromResource(this,
        R.array.poem_arrays3, android.R.layout.simple_spinner_item);
      break;
      default:
      // Hide the poem spinner
      PoemSpinner.setVisibility(View.GONE);
      break;
    }

    if (adapter != null) {
      // Set the poem spinners content and show the spinner
      adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
      PoemSpinner.setAdapter(adapter);
      PoemSpinner.setVisibility(View.VISIBLE);
    }
  }

  public void onNothingSelected(AdapterView<?> parent) {

  }

}

当然,您必须从 xml 中删除未使用的视图。我希望我足够清楚,并且代码可以按照您的意愿工作。

于 2013-03-14T21:47:32.377 回答