setVisibility无法在片段中工作

时间:2014-09-10 06:01:01

标签: java android android-fragments

当我选择搜索方法时,我无法让我的主Spinner更新ui。

在我将应用程序切换到片段的使用之前,我已经完成了所有工作。

非常感谢任何帮助。


OrderLookUpver2.java


package net.***.android.COA;

import android.app.Activity;

import android.content.Context;

import android.content.Intent;

import android.os.Bundle;

import android.support.v4.app.Fragment;

import android.support.v4.app.FragmentActivity;

import android.support.v4.app.FragmentManager;

import android.util.Log;

import android.view.LayoutInflater;

import android.view.View;

import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.Spinner;
import android.widget.Toast;

import java.util.List;

import static net.***.android.COA.R.id.spOrderSearchTopic;



public class OrderLookUpver2 extends FragmentActivity {
private FragmentActivity myContext;


android.support.v4.app.FragmentManager fragmentManager = getSupportFragmentManager();

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

}

public static class YourFragment extends Fragment implements    AdapterView.OnItemSelectedListener,View.OnClickListener {

private final String TAG = YourFragment.class.getSimpleName();
Spinner SpnrOrderTopic;
Spinner SpnrSearchByCustomer;
Spinner SpnrSearchByEvent;
LinearLayout LL;
EditText SearchByField;

Button bLast50, bPending, bSearch, bClear;

List<String> Label = null;

int mNum;

String inputText = "";
String SearchString = "";
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    Log.i("SwA", "HERE" );
    if(savedInstanceState != null) {
        mNum = savedInstanceState.getInt("number");
    } else {
        mNum = 25;
    }


}

@Override
public void onActivityCreated(Bundle savedInstanceState) {
    Log.d(TAG, "onActivityCreated");
    if(savedInstanceState != null) {
        Log.d(TAG, "saved variable number: " + savedInstanceState.getInt("number"));
    }
    super.onActivityCreated(savedInstanceState);
}

@Override
public void onSaveInstanceState(Bundle outState) {
    Log.d(TAG, "onSaveInstanceState saving: " + mNum);
    outState.putInt("number", mNum);
    super.onSaveInstanceState(outState);
}
public YourFragment() {
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    int fragmentLayout = getArguments().getInt("fragmentLayout");

    View rootView = inflater
            .inflate(R.layout.orderlookupver2, container, false);
    LL = (LinearLayout)rootView. findViewById(R.id.ll1);

    SearchByField = (EditText)rootView. findViewById(R.id.etSearchByField);

    bLast50 = (Button)rootView. findViewById(R.id.bLast50Orders);

    bPending = (Button)rootView. findViewById(R.id.bPendingOrders);

    bSearch = (Button)rootView. findViewById(R.id.bSearchOrders);

    bClear = (Button)rootView. findViewById(R.id.bClearOrderSearch);


    SpnrOrderTopic = (Spinner)getView().findViewById(spOrderSearchTopic);

    SpnrSearchByEvent = (Spinner)getView(). findViewById(R.id.spSearchByEvent);

    SpnrSearchByCustomer = (Spinner)getView(). findViewById(R.id.spSearchByCustomer);

    // Initialize the Buttons, EditText and Spinner
    Log.d("SwA", "onCreateView");

    // Populate Spinners
    // Populate Topic Spinner
    // Link XML with Java
    SpnrOrderTopic = (Spinner)rootView.findViewById(R.id.spOrderSearchTopic);
    // Create an ArrayAdapter using the string array and a default spinner
    // layout
    ArrayAdapter<CharSequence> adapterTopic = ArrayAdapter
            .createFromResource(getActivity(), R.array.order_search_array,
                    android.R.layout.simple_spinner_item);
    // Specify the layout to use when the list of choices appears
    adapterTopic
            .setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    // Apply the adapter to the spinner
    SpnrOrderTopic.setAdapter(adapterTopic);


    // Populate Topic Spinner
    // Link XML with Java
    SpnrSearchByEvent = (Spinner)rootView.findViewById(R.id.spSearchByEvent);
    // Create an ArrayAdapter using the string array and a default spinner
    // layout
    ArrayAdapter<CharSequence> adapterEvent = ArrayAdapter
            .createFromResource(getActivity(), R.array.type_array,
                    android.R.layout.simple_spinner_item);
    // Specify the layout to use when the list of choices appears
    adapterEvent
            .setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    // Apply the adapter to the spinner
    SpnrSearchByEvent.setAdapter(adapterEvent);


    SpnrSearchByCustomer = (Spinner)rootView.findViewById(R.id.spSearchByCustomer);
    // Populate Customer Spinner
    // database handler
    DatabaseHandler db = new DatabaseHandler(getActivity());

    // Spinner Drop down elements
    db.open();
    Label = db.getAllNames();

    // Creating adapter for spinner
    ArrayAdapter<String> adapterCus = new ArrayAdapter<String>(getActivity(),
            android.R.layout.simple_spinner_item, Label);

    // Drop down layout style - list view with radio button
    adapterCus
            .setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

    // attaching data adapter to spinner
    SpnrSearchByCustomer.setAdapter(adapterCus);
    db.close();


    SpnrOrderTopic.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
        public void onItemSelected(AdapterView<?> parent, View view,
                                   int position, long id) {


            switch (position) {

                case 0:
                    SearchByField.setVisibility(View.GONE);
                    SpnrSearchByEvent.setVisibility(View.GONE);
                    SpnrSearchByCustomer.setVisibility(View.VISIBLE);

                    break;

                case 1:
                    SearchByField.setVisibility(View.GONE);
                    SpnrSearchByCustomer.setVisibility(View.GONE);
                    SpnrSearchByEvent.setVisibility(View.VISIBLE);
                    break;

                case 2:
                    SearchByField.setVisibility(View.VISIBLE);
                    SpnrSearchByCustomer.setVisibility(View.GONE);
                    SpnrSearchByEvent.setVisibility(View.GONE);
                    break;

                case 3:
                    SearchByField.setVisibility(View.VISIBLE);
                    SpnrSearchByCustomer.setVisibility(View.GONE);
                    SpnrSearchByEvent.setVisibility(View.GONE);
                    break;

                case 4:
                    SearchByField.setVisibility(View.VISIBLE);
                    SpnrSearchByCustomer.setVisibility(View.GONE);
                    SpnrSearchByEvent.setVisibility(View.GONE);
                    break;

                case 5:
                    SearchByField.setVisibility(View.VISIBLE);
                    SpnrSearchByCustomer.setVisibility(View.GONE);
                    SpnrSearchByEvent.setVisibility(View.GONE);
                    break;

                case 6:
                    SearchByField.setVisibility(View.VISIBLE);
                    SpnrSearchByCustomer.setVisibility(View.GONE);
                    SpnrSearchByEvent.setVisibility(View.GONE);
                    break;
            }
        }

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

        }
    });
    SpnrSearchByCustomer.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
        public void onItemSelected(AdapterView<?> parent, View view,
                                   int position, long id) {
        }

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

        }
    });
    SpnrSearchByEvent.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
        public void onItemSelected(AdapterView<?> parent, View view,
                                   int position, long id) {
        }

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

        }
    });
    // Setup Button Listeners
    bLast50.setOnClickListener(new View.OnClickListener(){
      @Override
      public void onClick(View v) {
      }
    });
    bPending.setOnClickListener(new View.OnClickListener(){
        @Override
        public void onClick(View v) {
        }
    });
    bSearch.setOnClickListener(new View.OnClickListener(){
        @Override
        public void onClick(View v) {
        }
    });
    bClear.setOnClickListener(new View.OnClickListener(){
        @Override
        public void onClick(View v) {
        }
    });




    if (fragmentLayout == R.layout.activitymain) {





    } else {
    }

   return rootView;
}



@Override
public void onClick(View arg0) {
    // TODO Auto-generated method stub
    int item = SpnrOrderTopic.getSelectedItemPosition();
    Intent i = new Intent(getActivity(), OrderQueryList.class);
    SearchString = SearchByField.getText().toString();



    switch (arg0.getId()) {

        case R.id.bLast50Orders:
            inputText = "LAST_50";
            i.putExtra("keyinput", inputText);

            startActivity(i);
            break;
        case R.id.bPendingOrders:
            inputText = "PENDING_ORDERS";

            i.putExtra("keyinput", inputText);
            startActivity(i);
            break;

        case R.id.bSearchOrders:

            switch (item) {

                case 0:
                    inputText = "KEY_NAME";
                    i.putExtra("keyinput", inputText);
                    SearchString = SpnrSearchByCustomer.getSelectedItem().toString();
                    i.putExtra("SearchString", SearchString);
                    startActivity(i);
                    break;

                case 1:
                    inputText = "KEY_TYPEOFEVENT";
                    i.putExtra("keyinput", inputText);
                    SearchString = SpnrSearchByEvent.getSelectedItem().toString();
                    i.putExtra("SearchString", SearchString);
                    startActivity(i);
                    break;

                case 2:
                    inputText = "KEY_DETAIL";
                    i.putExtra("keyinput", inputText);
                    i.putExtra("SearchString", SearchString);
                    startActivity(i);
                    break;

                case 3:
                    inputText = "KEY_DATEOFEVENT";
                    i.putExtra("keyinput", inputText);
                    i.putExtra("SearchString", SearchString);
                    startActivity(i);
                    break;

                case 4:
                    inputText = "KEY_PRICE";
                    i.putExtra("keyinput", inputText);
                    i.putExtra("SearchString", SearchString);
                    startActivity(i);
                    break;

                case 5:
                    inputText = "KEY_ORDERCOMPLETED";
                    i.putExtra("keyinput", inputText);
                    i.putExtra("SearchString", SearchString);
                    startActivity(i);
                    break;

                case 6:
                    inputText = "KEY_TYPEOFEVENT";
                    i.putExtra("keyinput", inputText);
                    i.putExtra("SearchString", SearchString);
                    startActivity(i);
                    break;
            }

            break;

        case R.id.bClearOrderSearch:

            SearchByField.setText("");
            SpnrSearchByCustomer.setSelection(0);

            break;

    }

}


@Override
public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
                           long arg3) {
    // TODO Auto-generated method stub

}

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

}

}
}

0 个答案:

没有答案