覆盖ArrayAdapter中的getView并仍保留复选框?

时间:2012-02-14 21:29:23

标签: android checkbox android-arrayadapter

我已经覆盖了getView()中的ArrayAdapter方法。我想知道在覆盖getView()时是否有办法保留复选框功能?

使用我当前的实现,它只显示每行的图像和文本。它不再显示覆盖之前的复选框。

以下是我的代码:

public class ShortcutsArrayAdapter extends ArrayAdapter<ResolveInfo> {

    private final Activity context;
    private final List<ResolveInfo> objects;
    private PackageManager pm = null;

    public ShortcutsArrayAdapter(Activity context, int textViewResourceId,
            List<ResolveInfo> objects, PackageManager pm) {
        super(context, textViewResourceId, objects);
        this.context = context;
        this.objects = objects;
        this.pm = pm;
    }

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

        LayoutInflater inflator = context.getLayoutInflater();
        View rowView = inflator.inflate(R.layout.rowlayout, null, true);
        TextView textView = (TextView) rowView.findViewById(R.id.label);
        ImageView imageView = (ImageView) rowView.findViewById(R.id.icon);
        textView.setText(objects.get(position).loadLabel(pm));
        try {
            imageView.setImageBitmap(((BitmapDrawable) pm.getApplicationIcon(objects.get(position).activityInfo.applicationInfo.packageName)).getBitmap());
        } catch (NameNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        return rowView;
    }

}

以下是我创建适配器的方法:

    appList = getApplicationList();
    setListAdapter(new ShortcutsArrayAdapter(this, android.R.layout.simple_list_item_multiple_choice, appList, pm));
    getListView().setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);

这是行布局xml:

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

    <ImageView 
        android:id="@+id/icon"
        android:layout_width="40dp"
        android:layout_height="40dp"/>

    <TextView 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/label"
        android:text="@+id/label"/>

</LinearLayout>

1 个答案:

答案 0 :(得分:1)

为什么不调用super.getView(),然后执行一些你想做的自定义操作? 无论如何,我不明白为什么复选框不应该显示。也许它隐藏在另一个组件后面。你检查过布局了吗?尝试在文本框之前或之后移动它。

您可能需要row_layout.xml中的复选框。如下所示:

<CheckBox
        android:id="@+id/check"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_marginLeft="4px"
        android:layout_marginRight="10px" >
    </CheckBox>