CheckBox未出现在自定义ListView中

时间:2014-01-16 00:50:06

标签: android android-layout listview checkbox

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>

2 个答案:

答案 0 :(得分:3)

在所有textview和复选框上使用权重......就像这样

<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>

答案 1 :(得分: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" 
/>