让2个微调器进入活动而不是1?

时间:2018-02-11 11:12:58

标签: java android spinner android-spinner

The problem

嘿那里。我曾经遇到过一个问题,我在1个活动中得到了2个微调器,即使我只编写了1个代码。我已经在操作栏中显示了旋转器。似乎有某种奇怪的问题。有人可以看看我的代码,并给我一个暗示为什么会发生这种情况?我的活动代码:

public class OrderDetailsDisplayActivity extends AppCompatActivity {
    public static final int ORDER_RECEIVED = 0;
    public static final int ORDER_CONFIRMED = 1;
    public static final int ORDER_COMPLETED = 2;
    static final String TAG = "DEBUG";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_order_details_display);
        final ArrayList<OrderDetails> recyclerViewArrayList = new ArrayList<>();
        String ORDER_ID = getIntent().getExtras().getString("orderid");
        Log.d(TAG, "onCreate: " + ORDER_ID);
        final RecyclerView orderRecyclerView = findViewById(R.id.orderDetailsRecyclerView);
        ParseQuery<ParseObject> parseQuery = new ParseQuery<>("Orders");
        Log.d(TAG, "getOrderDetailsArrayList: " + ORDER_ID);
        parseQuery.whereEqualTo("orderid", ORDER_ID);
        parseQuery.findInBackground(new FindCallback<ParseObject>() {
            @Override
            public void done(List<ParseObject> objects, ParseException e) {
//                if (e != null) {
                for (ParseObject parseObject : objects) {
                    Log.d(TAG, "done: " + parseObject.getString("name"));
                    recyclerViewArrayList.add(new OrderDetails(parseObject.getInt("number"), parseObject.getInt("price"), parseObject.getString("name")));
                }
                Log.d(TAG, "onCreate: " + recyclerViewArrayList.toString());
                OrderDetailsAdapter recyclerViewAdapter = new OrderDetailsAdapter(recyclerViewArrayList);
                LinearLayoutManager manager = new LinearLayoutManager(OrderDetailsDisplayActivity.this);
                orderRecyclerView.setLayoutManager(manager);
                orderRecyclerView.addItemDecoration(new DividerItemDecoration(OrderDetailsDisplayActivity.this, DividerItemDecoration.VERTICAL));
                orderRecyclerView.setAdapter(recyclerViewAdapter);
                /*} else {
                    e.printStackTrace();
                }*/
            }
        });

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.menu_layout, menu);
        getMenuInflater().inflate(R.menu.menu_layout, menu);
        MenuItem item = menu.findItem(R.id.spinner);
        Spinner spinner = (Spinner) getActionView(item);
        ArrayAdapter<CharSequence> adapter = new ArrayAdapter<>(OrderDetailsDisplayActivity.this, android.R.layout.simple_spinner_dropdown_item, getResources().getTextArray(R.array.status_names));
        spinner.setAdapter(adapter); // set the adapter to provide layout of rows and content
        spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
                switch (i) {
                    case ORDER_RECEIVED:
                        Toast.makeText(OrderDetailsDisplayActivity.this, "Order recieved", Toast.LENGTH_SHORT).show();
                        break;
                    case ORDER_CONFIRMED:
                        Toast.makeText(OrderDetailsDisplayActivity.this, "Order confirmed", Toast.LENGTH_SHORT).show();
                        break;
                    case ORDER_COMPLETED:
                        Toast.makeText(OrderDetailsDisplayActivity.this, "Order completed", Toast.LENGTH_SHORT).show();
                        break;
                }
            }

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

            }
        });
        return true;
    }
}

菜单布局文件:

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">
    <item
        android:id="@+id/spinner"
        android:title="ActionBar Spinner"
        android:actionViewClass="android.widget.Spinner"
        android:background="#ff00"
        android:showAsAction="always" />
</menu>

活动布局文件:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.ranjusrecipebox.nalapaka.OrderDetailsDisplayActivity">

    <android.support.v7.widget.RecyclerView
        android:id="@+id/orderDetailsRecyclerView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:paddingBottom="8dp"
        android:paddingTop="8dp" />

</RelativeLayout>

1 个答案:

答案 0 :(得分:0)

您正在onCreateOptionsMenu中对菜单进行2次充气尝试删除一个仅显示一个微调器的菜单。

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.menu_layout, menu);
    //getMenuInflater().inflate(R.menu.menu_layout, menu); you can remove this inflate
    MenuItem item = menu.findItem(R.id.spinner);
    Spinner spinner = (Spinner) getActionView(item);
    ArrayAdapter<CharSequence> adapter = new ArrayAdapter<>(OrderDetailsDisplayActivity.this, android.R.layout.simple_spinner_dropdown_item, getResources().getTextArray(R.array.status_names));
    spinner.setAdapter(adapter); // set the adapter to provide layout of rows and content
    spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
            switch (i) {
                case ORDER_RECEIVED:
                    Toast.makeText(OrderDetailsDisplayActivity.this, "Order recieved", Toast.LENGTH_SHORT).show();
                    break;
                case ORDER_CONFIRMED:
                    Toast.makeText(OrderDetailsDisplayActivity.this, "Order confirmed", Toast.LENGTH_SHORT).show();
                    break;
                case ORDER_COMPLETED:
                    Toast.makeText(OrderDetailsDisplayActivity.this, "Order completed", Toast.LENGTH_SHORT).show();
                    break;
            }
        }

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

        }
    });
    return true;
}