gpt4 book ai didi

android - CheckBox 未出现在自定义 ListView 中

转载 作者:搜寻专家 更新时间:2023-11-01 08:53:14 25 4
gpt4 key购买 nike

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 和复选框上使用权重...像这样

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

关于android - CheckBox 未出现在自定义 ListView 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21151407/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com