2016-01-04 21 views
0

我不知道這裏發生了什麼,我是Android新手。實際上,當我單擊微調器時,它不顯示任何內容,默認情況下選擇第一個值,當我單擊微調器時,什麼都不會發生。我已經嘗試過不同的設備,但仍然不知道該怎麼做。我也嘗試了另一種方法(實現OnItemSelectedListener),但結果仍然相同。沒有錯誤,但代碼似乎不工作,我也調試了應用程序,並且適配器有7個項目,但仍然沒有用。Android:微縮片段

結果圖像: 當我打開片段與所選擇的值和吐司微調:

import static android.R.layout.simple_spinner_dropdown_item; 
import static android.R.layout.simple_spinner_item; 

public class Attendance_fragment extends android.app.Fragment { 


public Attendance_fragment() { 
    // Required empty public constructor 
} 

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

} 

@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, 
         Bundle savedInstanceState) { 
    // Inflate the layout for this fragment 
    View view= inflater.inflate(R.layout.fragment_attendance_layout, container, false); 
    Spinner spinner; 
    spinner =(Spinner)view.findViewById(R.id.spinnerattendancedates); 
    ArrayAdapter<CharSequence> adapter=ArrayAdapter.createFromResource(getActivity(),R.array.attendance_dates, simple_spinner_item); 
    adapter.setDropDownViewResource(simple_spinner_dropdown_item); 
    adapter.setNotifyOnChange(true); 
    spinner.setAdapter(adapter); 
    spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() { 
     @Override 
     public void onItemSelected(AdapterView<?> parent, View view, int position, long id) { 
      String item = parent.getItemAtPosition(position).toString(); 
      Toast.makeText(getActivity(), "Selected " + item, Toast.LENGTH_LONG).show(); 
     } 

     @Override 
     public void onNothingSelected(AdapterView<?> parent) { 
      Toast.makeText(getActivity(), "Selected " , Toast.LENGTH_LONG).show(); 

     } 
    }); 
    return view; 
} 

fragment_attendance_layout.xml

<Spinner 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:clickable="true" 
    android:id="@+id/spinnerattendancedates" 
    /> 

<RelativeLayout 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:layout_margin="10dp" 
    android:layout_gravity="bottom" 
    android:padding="10dp" 
    > 
    <ScrollView 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content"> 
<TableLayout 
    android:layout_width="match_parent" 
    android:layout_height="440dp" 
    android:layout_gravity="bottom|center" 
    android:layout_alignParentTop="true" 
    android:layout_centerHorizontal="true"> 

    <TableRow 
     android:layout_width="match_parent" 
     android:layout_height="match_parent"> 

     <TextView 
      android:layout_width="150dp" 
      android:layout_height="50dp" 
      android:text="@string/period1" 
      android:id="@+id/textView" 
      android:layout_column="0" 
      android:layout_gravity="center" /> 

     <TextView 
      android:layout_width="150dp" 
      android:layout_height="wrap_content" 
      android:textAppearance="?android:attr/textAppearanceSmall" 
      android:text="@string/absentorpresent" 
      android:id="@+id/textView2" 
      android:layout_column="1" 
      android:layout_gravity="center_horizontal" /> 
    </TableRow> 

    <TableRow 
     android:layout_width="match_parent" 
     android:layout_height="match_parent"> 

     <TextView 
      android:layout_width="150dp" 
      android:layout_height="50dp" 
      android:text="@string/period2" 
      android:id="@+id/textView3" 
      android:layout_column="0" 
      android:layout_gravity="center" /> 

     <TextView 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:textAppearance="?android:attr/textAppearanceSmall" 
      android:text="@string/absentorpresent" 
      android:id="@+id/textView4" 
      android:layout_column="1" 
      android:layout_gravity="center_horizontal" /> 
    </TableRow> 

    <TableRow 
     android:layout_width="match_parent" 
     android:layout_height="match_parent"> 

     <TextView 
      android:layout_width="150dp" 
      android:layout_height="50dp" 
      android:text="@string/period3" 
      android:id="@+id/textView5" 
      android:layout_column="0" 
      android:layout_gravity="center_horizontal" /> 

     <TextView 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:textAppearance="?android:attr/textAppearanceSmall" 
      android:text="@string/absentorpresent" 
      android:id="@+id/textView11" 
      android:layout_column="1" 
      android:layout_gravity="center_horizontal" /> 
    </TableRow> 

    <TableRow 
     android:layout_width="match_parent" 
     android:layout_height="match_parent"> 

     <TextView 
      android:layout_width="150dp" 
      android:layout_height="50dp" 
      android:text="@string/period4" 
      android:id="@+id/textView6" 
      android:layout_column="0" 
      android:layout_gravity="center_horizontal" /> 

     <TextView 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:textAppearance="?android:attr/textAppearanceSmall" 
      android:text="@string/absentorpresent" 
      android:id="@+id/textView12" 
      android:layout_column="1" 
      android:layout_gravity="center_horizontal" /> 
    </TableRow> 

    <TableRow 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:layout_gravity="center_horizontal"> 

     <TextView 
      android:layout_width="150dp" 
      android:layout_height="50dp" 
      android:text="@string/period5" 
      android:id="@+id/textView7" 
      android:layout_column="0" 
      android:layout_gravity="center_horizontal" /> 

     <TextView 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:textAppearance="?android:attr/textAppearanceSmall" 
      android:text="@string/absentorpresent" 
      android:id="@+id/textView13" 
      android:layout_column="1" 
      android:layout_gravity="center_horizontal" /> 
    </TableRow> 

    <TableRow 
     android:layout_width="match_parent" 
     android:layout_height="match_parent"> 

     <TextView 
      android:layout_width="150dp" 
      android:layout_height="50dp" 
      android:text="@string/period6" 
      android:id="@+id/textView8" 
      android:layout_column="0" 
      android:layout_gravity="center_horizontal" /> 

     <TextView 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:textAppearance="?android:attr/textAppearanceSmall" 
      android:text="@string/absentorpresent" 
      android:id="@+id/textView14" 
      android:layout_column="1" 
      android:layout_gravity="center_horizontal" /> 
    </TableRow> 

    <TableRow 
     android:layout_width="match_parent" 
     android:layout_height="match_parent"> 

     <TextView 
      android:layout_width="150dp" 
      android:layout_height="50dp" 
      android:text="@string/period7" 
      android:id="@+id/textView9" 
      android:layout_column="0" 
      android:layout_gravity="center_horizontal" /> 

     <TextView 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:textAppearance="?android:attr/textAppearanceSmall" 
      android:text="@string/absentorpresent" 
      android:id="@+id/textView15" 
      android:layout_column="1" 
      android:layout_gravity="center_horizontal" /> 
    </TableRow> 

    <TableRow 
     android:layout_width="match_parent" 
     android:layout_height="match_parent"> 

     <TextView 
      android:layout_width="150dp" 
      android:layout_height="50dp" 
      android:text="@string/period8" 
      android:id="@+id/textView10" 
      android:layout_column="0" 
      android:layout_gravity="center_horizontal" /> 

     <TextView 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:textAppearance="?android:attr/textAppearanceSmall" 
      android:text="@string/absentorpresent" 
      android:id="@+id/textView16" 
      android:layout_column="1" 
      android:layout_gravity="center_horizontal" /> 
    </TableRow> 
</TableLayout> 
    </ScrollView> 
</RelativeLayout> 

我的strings.xml

<string-array name="attendance_dates"> 
    <item>Date 1</item> 
    <item>Date 2</item> 
    <item>Date 3</item> 
    <item>Date 4</item> 
    <item>Date 5</item> 
    <item>Date 6</item> 
    <item>Date 7</item> 
</string-array> 

回答

0

最後糾正它。我的問題是我從導航抽屜活動中的「android.support.v4.widget.NestedScrollView」從哪裏打開此片段。當我刪除這個。有效。這對於在某些情況下不可點擊的按鈕也很相似。