2014-01-16 41 views
2

textViews顯示正常,但CheckBoxes根本不顯示。我實現了自定義適配器從BaseAdapter我的列表視圖...複選框未出現在自定義ListView中

上的圖形佈局預覽列表項(IMG1):

Actual result

上進行測試,這是它如何出現,失蹤該複選框(IMG2):

Testing

class MarkListAdapter extends BaseAdapter implements 
    OnCheckedChangeListener { 

Context context; 
public String[] markerTitles; 
public int[] markerDists, markerIDs; 
LayoutInflater inflater; 
CheckBox[] checkBoxArray; 
LinearLayout[] viewArray; 
private boolean[] checked; 

public MarkListAdapter(Context con, String[] markTitles, int[] markDists, int[] markIDs) { 
    context = con; 
    markerTitles = markTitles; 
    markerDists = markDists; 
    markerIDs = markIDs; 
    inflater = (LayoutInflater) context 
      .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
    checked = new boolean[markerTitles.length]; 
    for (int i = 0; i < checked.length; i++) { 
     checked[i] = false; 
     // Toast.makeText(con, 
     // checked.toString(),Toast.LENGTH_SHORT).show(); 
    } 
    checkBoxArray = new CheckBox[checked.length]; 
    viewArray = new LinearLayout[checked.length]; 
} 

public int getCount() { 
    return markerTitles.length; 
} 

public Object getItem(int position) { 
    // TODO Auto-generated method stub 
    return null; 
} 

public long getItemId(int position) { 
    // TODO Auto-generated method stub 
    return 0; 
} 

public View getView(int position, View convertView, ViewGroup parent) { 

    if (viewArray[position] == null) { 

     viewArray[position] = (LinearLayout) inflater.inflate(
       R.layout.list_item, null); 

     TextView tvTitle = (TextView) viewArray[position] 
       .findViewById(R.id.title); 
     tvTitle.setText(markerTitles[position]); 

     TextView tvDist = (TextView) viewArray[position] 
       .findViewById(R.id.calcDist); 
     tvDist.setText(markerDists[position]+" mi"); 

     TextView tvID = (TextView) viewArray[position] 
       .findViewById(R.id.dcaid); 
     tvID.setText(markerIDs[position]+""); 

     checkBoxArray[position] = (CheckBox) viewArray[position] 
       .findViewById(R.id.checkBox1); 
    } 

    checkBoxArray[position].setChecked(checked[position]); 
    checkBoxArray[position].setOnCheckedChangeListener(this); 
    return viewArray[position]; 
} 

public void checkAll(boolean areChecked) { 
    for (int i = 0; i < checked.length; i++) { 
     checked[i] = areChecked; 
     if (checkBoxArray[i] != null) 
      checkBoxArray[i].setChecked(areChecked); 
    } 
    notifyDataSetChanged(); 
} 

public boolean itemIsChecked(int i) { 
    return checked[i]; 
} 

public void onCheckedChanged(CompoundButton cb, boolean isChecked) { 
    for (int i = 0; i < checked.length; i++) { 
     if (cb == checkBoxArray[i]) 
      checked[i] = isChecked; 
    } 

} 
} 

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="fill_parent" 
android:layout_height="wrap_content" 
android:background="#f1d5c1" 
android:listSelector="@android:color/transparent" 
android:orientation="horizontal" > 

<LinearLayout 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:background="#f1d5c1" 
    android:orientation="vertical" > 

    <!-- Name Label --> 

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:background="#f1d5c1" 
     android:orientation="horizontal" > 

     <CheckBox 
      android:id="@+id/checkBox1" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_gravity="left|center" 
      android:background="#00000000" 
      android:checked="true" 
      android:focusable="false" /> 

     <TextView 
      android:id="@+id/title" 
      android:layout_width="0dp" 
      android:layout_height="wrap_content" 
      android:layout_gravity="center" 
      android:layout_weight="1" 
      android:background="#f1d5c1" 
      android:gravity="center" 
      android:paddingBottom="10dip" 
      android:paddingLeft="10dip" 
      android:paddingRight="10dip" 
      android:paddingTop="5dip" 
      android:text="Loading..." 
      android:textColor="#000000" 
      android:textSize="18sp" 
      android:textStyle="bold" /> 

     <TextView 
      android:id="@+id/calcDist" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_gravity="right|center" 
      android:background="#f1d5c1" 
      android:paddingRight="10dp" 
      android:text="0 mi" 
      android:textColor="#acacac" 
      android:textStyle="bold" /> 

     <TextView 
      android:id="@+id/dcaid" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:background="#f1d5c1" 
      android:textColor="#acacac" 
      android:textStyle="bold" 
      android:visibility="gone" /> 
    </LinearLayout> 
</LinearLayout> 

在所有的TextView和複選框

回答

3

使用重量......這樣的事情

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:background="#f1d5c1" 
    android:listSelector="@android:color/transparent" 
    android:orientation="vertical" > 

    <LinearLayout 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:background="#f1d5c1" 
     android:orientation="vertical" > 

     <!-- Name Label --> 

     <LinearLayout 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:background="#f1d5c1" 
      android:weightSum="1" 
      android:orientation="horizontal" > 

      <CheckBox 
       android:id="@+id/checkBox1" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_gravity="left|center" 
       android:background="#0000" 
       android:checked="true" 
       android:layout_weight="0.1" 
       android:focusable="false" /> 

      <TextView 
       android:id="@+id/title" 
       android:layout_width="0dp" 
       android:layout_height="wrap_content" 
       android:layout_gravity="center" 
       android:layout_weight="0.7" 
       android:background="#f1d5c1" 
       android:gravity="center" 
       android:paddingBottom="10dip" 
       android:paddingLeft="10dip" 
       android:paddingRight="10dip" 
       android:paddingTop="5dip" 
       android:text="Loading..." 
       android:textColor="#000000" 
       android:textSize="18sp" 
       android:textStyle="bold" /> 

      <TextView 
       android:id="@+id/calcDist" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_gravity="right|center" 
       android:background="#f1d5c1" 
       android:paddingRight="10dp" 
       android:layout_weight="0.2" 
       android:text="0 mi" 
       android:textColor="#acacac" 
       android:textStyle="bold" /> 

      <TextView 
       android:id="@+id/dcaid" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:background="#f1d5c1" 
       android:textColor="#acacac" 
       android:textStyle="bold" 
       android:visibility="gone" /> 
     </LinearLayout> 
    </LinearLayout> 
</LinearLayout> 
+0

感謝您的適配器!它工作完美 – user2836396

0

嘗試自定義列表中添加此佈局到ListView

<CheckedTextView xmlns:android="http://schemas.android.com/apk/res/android" 
android:id="@android:id/text1" 
android:layout_width="wrap_content" 
android:layout_height="wrap_content" 
android:drawableLeft="?android:attr/listChoiceIndicatorMultiple" 
android:textAppearance="?android:attr/textAppearanceSmall" 
android:ellipsize="marquee" 
android:gravity="center_vertical" 
android:textColor="#000000" 
android:textAlignment="center" 
android:typeface="serif" 
android:paddingTop="1dp" 
android:paddingLeft="3dp" 
android:paddingBottom="1dp" 
/>