简体   繁体   English

Android如何选择不可见的微调器

[英]Android how to select an invisible spinner

I am having trouble getting around a problem. 我无法解决问题。 In my application i have a spinner where a user selects a answer and then that answer gets passed onto the next activity. 在我的应用程序中,我有一个微调器,用户可以在其中选择一个答案,然后将该答案传递到下一个活动中。 However, what i really want is the id of that answer to get passed on that activity...maybe seeing the code will help... 但是,我真正想要的是通过该活动传递的答案的ID ...也许看到代码会有所帮助...

db = dbs.getReadableDatabase();     
    String SQL = "SELECT * FROM answer table"; 
    Cursor cursor = db.rawQuery(SQL, null); 
    startManagingCursor(cursor);
    final int l = cursor.getCount();
    int j = l + 1;
    array_spinner = new String[j];
    invisible_array_spinner = new String[j];
    int i = 1;
    array_spinner[0] = "Please select from below";
    invisible_array_spinner[0] = "Please select from below";
    while (cursor.moveToNext()) {
     array_spinner[i]= cursor.getString(2);
     invisible_array_spinner[i]= cursor.getString(1);
     i ++;
    }
    final ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, array_spinner);
    spinner.setAdapter(adapter);

in the answer table there is id and then answer, id is at cursor 1 and answer is at cursor 2.. What i am trying here to have maybe an invisible spinner in the back which reflects what the user has selected in the real spinner...Can anyone suggest how to solve this problem?thanks 在答案表中有id,然后是答案,id在光标1处,答案在光标2处。我在这里尝试在背面设置一个不可见的微调框,以反映用户在实际微调框中选择的内容。 ..有人可以建议如何解决这个问题吗?

You ahve to implement fol. 您要执行以下操作。 code: 码:


 spinner.setOnItemSelectedListener(new MyOnItemSelectedListener());

    public class MyOnItemSelectedListener implements OnItemSelectedListener {

        public void onItemSelected(AdapterView<?> parent,
            View view, int pos, long id) {
          Toast.makeText(parent.getContext()), "The planet is " +
              parent.getItemAtPosition(pos).toString(), Toast.LENGTH_LONG).show();
cnt=pos;//declare public variable cnt abve oncreate
        }

        public void onNothingSelected(AdapterView parent) {
          // Do nothing.
        }
    }

after impl.it you get pos of selected itam of spinner in cnt var. 在impl.it之后,您将在cnt var中获得微调器的选定itam的位置。


so you can get id for same spinner by 这样您就可以通过以下方式获得同一个微调器的ID


int id=invisible_array_spinner[cnt];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM