复选框搞砸整个listView

时间:2013-10-24 20:29:52

标签: android listview checkbox android-listview

我正在尝试在每个listView项中实现一个复选框。 listView是所有用户安装的应用程序的集合。问题是,当我运行我的应用程序并转到我的listView时,我看到的只是一行(应用程序图标,然后是它旁边的复选框),而不是每行都有一个复选框。这是我定义listView项目的布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="?android:attr/listPreferredItemHeight"
android:padding="5dip" 
>

<ImageView
  android:id="@+id/ivIcon"
  android:layout_width="wrap_content"
  android:layout_height="fill_parent"
  android:layout_marginRight="5dip"
  android:scaleType="center"
  android:contentDescription="@string/desc"
/>

<LinearLayout
  android:orientation="vertical"
  android:layout_width="0dip"
  android:layout_height="fill_parent"
  android:layout_weight="1"    
>

  <TextView
      android:id="@+id/tvName"
      android:layout_width="fill_parent"
      android:layout_height="0dip"
      android:layout_weight="1"
      android:gravity="center_vertical"         
  />

  <TextView
      android:id="@+id/tvPack"
      android:layout_width="fill_parent"
      android:layout_height="0dip"
      android:layout_weight="1"
      android:singleLine="true"
      android:ellipsize="marquee"         
  />

 </LinearLayout>

 <CheckBox
  android:id="@+id/addCheckbox"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"
  android:layout_weight="1"
  android:gravity="center_vertical" 
 />

</LinearLayout>

然后这是我的自定义适配器:

package com.example.awesomefilebuilderwidget;

IMPORTS

public class AppInfoAdapter extends BaseAdapter implements Filterable {
private Context mContext;
private List<ApplicationInfo> mListAppInfo;
private PackageManager mPackManager;
private List<ApplicationInfo> originalListAppInfo;
private Filter filter;

public AppInfoAdapter(Context c, List<ApplicationInfo> listApp, PackageManager pm) {
    mContext = c;
    this.originalListAppInfo = this.mListAppInfo = listApp;
    mPackManager = pm;
    }

@Override
public int getCount() {
    return mListAppInfo.size();
}

@Override
public Object getItem(int position) {
    return mListAppInfo.get(position);
}

@Override
public long getItemId(int position) {
    return position;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    // get the selected entry
    ApplicationInfo entry = (ApplicationInfo) mListAppInfo.get(position);

    // reference to convertView
    View v = convertView;

    // inflate new layout if null
    if(v == null) {
        LayoutInflater inflater = LayoutInflater.from(mContext);
        v = inflater.inflate(R.layout.layout_appinfo, null);
    }

    // load controls from layout resources
    ImageView ivAppIcon = (ImageView)v.findViewById(R.id.ivIcon);
    TextView tvAppName = (TextView)v.findViewById(R.id.tvName);
    TextView tvPkgName = (TextView)v.findViewById(R.id.tvPack);
    CheckBox addCheckbox = (CheckBox)v.findViewById(R.id.addCheckbox);

    // set data to display
    ivAppIcon.setImageDrawable(entry.loadIcon(mPackManager));
    tvAppName.setText(entry.loadLabel(mPackManager));
    tvPkgName.setText(entry.packageName);
    addCheckbox.setChecked(true);
    checkButtonClick();

    // return view
    return v;
}

private void checkButtonClick() {
    // do nothing
}

我的列表工作正常,直到我添加了复选框。为什么要这样做?

1 个答案:

答案 0 :(得分:0)

由于您使用的是weight,因此您应该"0dp"使用width。将您的CheckBox更改为

<CheckBox
   android:id="@+id/addCheckbox"
   android:layout_width="0dp"
   android:layout_height="fill_parent"   
   android:layout_weight="1"
   android:gravity="center_vertical" 
   />