无法从Spinner进入setOnItemSelectedListener

时间:2018-03-26 11:49:12

标签: java android

基于Android: Create spinner programmatically from array的信息。我可以根据ArrayList从spinner中选择房间。但是,它永远不会进入setOnItemSelectedListener进程。我无法弄清楚它为什么会发生。

protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_edit_room);
SpaceRoomTable = (TableLayout) findViewById(R.id.tvSpaceRoomTable);
AddroomButton = (Button)findViewById(R.id.btLinkAddRoom);
Backtomenu = (Button) findViewById(R.id.bBackToMenu_DogTrack);
Delete = ContextCompat.getDrawable(this, R.drawable.bin);
dropdown = (Spinner) findViewById(R.id.spinListRoom);
summitDelete  = (Button) findViewById(R.id.btDeleteSummit);


AddroomButton.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
        Intent LinkToEditroom = new Intent (EditRoom.this,AddRoom.class);
        startActivity(LinkToEditroom);
    }
});

Backtomenu.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
        Intent LinkToHomeMenu = new Intent(EditRoom.this,HomeMenu.class);
        EditRoom.this.startActivity(LinkToHomeMenu);
    }
});


showRoom();


ArrayAdapter<String> spinnerArrayAdapter = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item,showitemName);
spinnerArrayAdapter .setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
dropdown.setAdapter(spinnerArrayAdapter);

dropdown.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
    @Override
    public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
        //   Log.d("TEST","selection is ");
        Object Itemselected =    adapterView.getItemAtPosition(i);
        Toast.makeText(getApplicationContext(),"Inside"+String.valueOf(Itemselected),Toast.LENGTH_SHORT).show();

    }

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

    }
});


}

3 个答案:

答案 0 :(得分:1)

在XML布局中: -

<Spinner
    android:id="@+id/dropdown"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" />

然后,在你的活动findViewById中这样: -

dropdown= (Spinner) findViewById(R.id.dropdown);

然后使用字符串数组和默认的ArrayAdapter布局创建spinner: -

ArrayAdapter<String> spinnerArrayAdapter = ArrayAdapter<String>(this, android.R.layout.simple_spinner_item,showitemName);
spinnerArrayAdapter .setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
dropdown.setAdapter(spinnerArrayAdapter);

OnIemClickListener使用如下: -

public class MainActivity extends AppCompatActivity implements AdapterView.OnItemSelectedListener{ 

Spinner dropdown;
String[] showitemName = {"A","B","C","D","E"};

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);



   dropdown= (Spinner) findViewById(R.id.dropdown);
   dropdown.setOnItemSelectedListener(this);

    ArrayAdapter<String> spinnerArrayAdapter = ArrayAdapter<String>(this, android.R.layout.simple_spinner_item,showitemName);
    spinnerArrayAdapter .setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    dropdown.setAdapter(spinnerArrayAdapter);

}

  //Performing action onItemSelected and onNothing selected
  @Override
  public void onItemSelected(AdapterView<?> parent, View arg1, int position,long id) {
  Toast.makeText(getApplicationContext(),"Inside" + parent.getItemAtPosition(position) , Toast.LENGTH_LONG).show();
}

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

}
}

更多信息:

您可以从以下链接查看详细信息: -

hash_update() https://developer.android.com/guide/topics/ui/controls/spinner.html https://developer.android.com/reference/android/widget/AdapterView.OnItemSelectedListener.html https://android--code.blogspot.in/2015/08/android-spinner-onitemselected.html http://www.java2s.com/Code/Android/UI/SpinnerItemSelectedListener.htm

答案 1 :(得分:1)

有关陈列室方法的更多信息

public void showRoom()
{
    refroom.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(final DataSnapshot dataSnapshot) {
            int Length_Record = dataSnapshot.child("CurrentUser").child("Tracking").child("ListRoom").child("LIST_ROOM_LENGTH").getValue(int.class);
            int RunRecord =0;
            int printRoom =0;
            final String userId = dataSnapshot.child("CurrentUser").child("UserId").getValue(String.class);


            for (DataSnapshot zonesnapshot : dataSnapshot.child("CurrentUser").child("Tracking").child("ListRoom").getChildren())
            {
                if(printRoom!=Length_Record)
                {
                    zonesnapshot.getValue();
                    String[] parts = String.valueOf(zonesnapshot.getValue()).split(",");
                    String part1 = parts[0]; // Room Id
                    String part2 = parts[1]; // Room Name
                    String part3 = parts[2]; // TypeRoom
                    String OnRoom = dataSnapshot.child("CurrentUser").child("Tracking").child("Location").getValue(String.class);

                    itemsName.add(part2+","+part3);  // put to arraylist to prepare a room choice for deletion
                    showitemName.add(part2+"("+part3+")");
                    itemOfUser.put(userId+"Room"+printRoom,part2+","+part3);  // keep room for each user

                    inRow(dataSnapshot.child("CurrentUser").child("UserId").getValue(String.class), RunRecord, part2, OnRoom, part3);

                }
                printRoom++;

            }



            summitDelete.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {

                    storageReference = storage.getInstance().getReferenceFromUrl("gs://********-*****.appspot.com/imagesRoom").child(userId).child(selection);

                    storageReference.delete().addOnSuccessListener(new OnSuccessListener<Void>() {
                        @Override
                        public void onSuccess(Void aVoid) {

                        }
                    }).addOnFailureListener(new OnFailureListener() {
                        @Override
                        public void onFailure(@NonNull Exception e) {
                           Toast.makeText(getApplicationContext(),"Error Can not delete file",Toast.LENGTH_SHORT).show();
                        }
                    });
                }
            });


        }

        @Override
        public void onCancelled(DatabaseError databaseError) {

        }
    });
}

答案 2 :(得分:0)

以下代码将自动获取所选项目文本

 public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
     //   Log.d("TEST","selection is ");
      String item = adapterView.getItemAtPosition(i).toString();
        Toast.makeText(getApplicationContext(),"Inside"+item,Toast.LENGTH_SHORT).show();

    }