将微调器纳入片段时不响应应用

时间:2019-07-26 17:10:30

标签: android-studio

嗨,我是android studio的新手,当我尝试放置Spinner并尝试按片段与MySQL数据库连接时,在这里选择Spinner的某些项目并将其显示在片段活动中。

公共类BusscheduleFragment扩展Fragment实现     Spinner.OnItemSelectedListener {     公共静态最终字符串DATA_URL =     “”;

//Tags used in the JSON String
public static final String TAG_USERNAME = "username";
public static final String TAG_NAME = "name";
public static final String TAG_COURSE = "course";
public static final String TAG_SESSION = "session";

//JSON array name
public static final String JSON_ARRAY = "result";
View view;
private Spinner spinner;
private ArrayList<String> students;
private JSONArray result;
private TextView textViewName;
private TextView textViewCourse;
private TextView textViewSession;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    view = inflater.inflate(R.layout.fragment_busschedule, container, false);
    spinner = view.findViewById(R.id.spinner);
    students = new ArrayList<String>();
    spinner.setOnItemSelectedListener(this);

    textViewName =   view.findViewById(R.id.textViewName);
    textViewCourse =  view.findViewById(R.id.textViewCourse);
    textViewSession =  view.findViewById(R.id.textViewSession);
    getData();

    return view;
}
private void getData(){
    //Creating a string request
    StringRequest stringRequest = new StringRequest(DATA_URL,
            new Response.Listener<String>() {
                @Override
                public void onResponse(String response) {
                    JSONObject j = null;
                    try {
                        //Parsing the fetched Json String to JSON Object
                        j = new JSONObject(response);

                        //Storing the Array of JSON String to our JSON Array
                        result = j.getJSONArray(JSON_ARRAY);

                        //Calling method getStudents to get the students from the JSON Array
                        getStudents(result);
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }
            },
            new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {

                }
            });

    //Creating a request queue
    RequestQueue requestQueue = Volley.newRequestQueue(mCtx.getApplicationContext());

    //Adding request to the queue
    requestQueue.add(stringRequest);


}

private void getStudents(JSONArray j){
    //Traversing through all the items in the json array
    for(int i=0;i<j.length();i++){
        try {
            //Getting json object
            JSONObject json = j.getJSONObject(i);

            //Adding the name of the student to array list
            students.add(json.getString(TAG_USERNAME));
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

    //Setting adapter to show the items in the spinner
    spinner.setAdapter(new ArrayAdapter<String>(getActivity(), android.R.layout.simple_spinner_dropdown_item, students));
}

//Method to get student name of a particular position
private String getName(int position){
    String name="";
    try {
        //Getting object of given index
        JSONObject json = result.getJSONObject(position);

        //Fetching name from that object
        name = json.getString(TAG_NAME);
    } catch (JSONException e) {
        e.printStackTrace();
    }
    //Returning the name
    return name;
}

//Doing the same with this method as we did with getName()
private String getCourse(int position){
    String course="";
    try {
        JSONObject json = result.getJSONObject(position);
        course = json.getString(TAG_COURSE);
    } catch (JSONException e) {
        e.printStackTrace();
    }
    return course;
}

//Doing the same with this method as we did with getName()
private String getSession(int position){
    String session="";
    try {
        JSONObject json = result.getJSONObject(position);
        session = json.getString(TAG_SESSION);
    } catch (JSONException e) {
        e.printStackTrace();
    }
    return session;
}


//this method will execute when we pic an item from the spinner
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
    //Setting the values to textviews for a selected item
    textViewName.setText(getName(position));
    textViewCourse.setText(getCourse(position));
    textViewSession.setText(getSession(position));
}

//When no item is selected this method would execute
@Override
public void onNothingSelected(AdapterView<?> parent) {
    textViewName.setText("");
    textViewCourse.setText("");
    textViewSession.setText("");
}

}

这是工作的xml文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayoutLayout 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"
android:orientation="vertical"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context=".BusscheduleFragment"
>

<Spinner
    android:id="@+id/spinner"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentStart="true"
    />

<TableLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content">
    <TableRow>
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textStyle="bold"
            android:layout_marginRight="20dp"
            android:layout_marginLeft="10dp"
            android:text="Name"
            />
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textStyle="bold"
            android:layout_marginRight="20dp"
            android:text="Course"
            />
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textStyle="bold"
            android:layout_marginRight="20dp"
            android:text="Session"
            />
    </TableRow>
    <TableRow>
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginRight="20dp"
            android:layout_marginLeft="10dp"
            android:id="@+id/textViewName"
            />
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginRight="20dp"
            android:id="@+id/textViewCourse"
            />
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginRight="20dp"
            android:id="@+id/textViewSession"
            />
    </TableRow>
</TableLayout>

0 个答案:

没有答案