2015-11-22 54 views
0

就這樣,我有一個導航欄佈局。當我的導航欄中的stand1按鈕被按下時,content_main中的<FrameLayout>被替換爲stand1.xml。這工作沒有任何問題。Android陣列適配器 - 在每一行插入一個複選框

Content_main.xml

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent" 
    android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin" 
    android:paddingRight="@dimen/activity_horizontal_margin" 
    android:paddingTop="@dimen/activity_vertical_margin" 
    android:paddingBottom="@dimen/activity_vertical_margin" 
    app:layout_behavior="@string/appbar_scrolling_view_behavior" tools:showIn="@layout/app_bar_main" 
    tools:context=".MainActivity"> 

    <FrameLayout 
     android:id="@+id/content_frame" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:layout_alignParentLeft="true" 
     android:layout_alignParentStart="true"></FrameLayout> 

</RelativeLayout> 

從主要活動在線

if (id == R.id.Stand1) { 
    fm.beginTransaction().replace(R.id.content_frame, new Stand1()).commit(); 
    setTitle(R.string.Stand1); 

以下教程stand1.xml

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" android:layout_height="match_parent"> 

    <ListView 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:id="@+id/Stand1list"> 

    </ListView> 

</LinearLayout> 

這是用於填充我Stand1.java文件帶有一串字符串的列表

public class Stand1 extends Fragment { 
    ListView mList; 

    @Nullable 
    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
     View root = inflater.inflate(R.layout.stand1,container,false); 
     mList = (ListView) root.findViewById(R.id.Stand1list); 
     return root; 
    } 

    public void onActivityCreated(Bundle savedInstanceState) 
    { 
     super.onActivityCreated(savedInstanceState); 
     populateListView(); 
    } 

    private void populateListView() { 
     String[] pair = {"Pair1","Pair2","Pair3","Pair4","Pair5"}; 

     //build adapter 
     ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(), R.layout.row_layout,pair); 

     mList.setAdapter(adapter); 
    } 
} 

和row_layout.xml

<?xml version="1.0" encoding="utf-8"?> 

<TextView 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:id="@+id/text1" 
    xmlns:android="http://schemas.android.com/apk/res/android" /> 

這是工作,它顯示5個字符串。不過,我想這樣做,我也可以在每一行中有一個複選框。 如果我row_layout.xml嘗試到位linear layout,我得到錯誤

ArrayAdapter requires the resource ID to be a TextView 

我下面的計算器,其整理這表明,我不得不在類級別的列表和的onCreate它充氣其他錯誤。

我知道我必須做一個自定義數組適配器,但我的問題是如何處理在創建視圖的通貨膨脹。

我不知道我該如何去做這件事。那麼有人可以從這裏告訴我,我怎麼去在每一行都有一個複選框。

感謝您的幫助

回答

1

ArrayAdapter需要知道從您的字符串數組把文字。默認情況下,它假定您傳遞的佈局是TextView,並將其文本設置爲其中一個字符串。如果你通過它更復雜的佈局,你需要告訴它你想要它使用的TextView的ID。你可以用this constructor來做到這一點。

+0

感謝您的幫助 –

相關問題