1
我已經覆蓋ArrayAdapter
中的getView()
方法。我想知道在重寫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>
向方法中添加'super.getView(position,convertView,parent);'時。這是我運行時從logcat得到的錯誤。 '02-14 15:44:48.924:E/AndroidRuntime(26397):java.lang.IllegalStateException:ArrayAdapter需要資源ID爲TextView。我將它改爲一些隨機的「TextView」,但它仍然無法工作。它說'無效資源'。 – prolink007 2012-02-14 21:45:36
@ prolink007你的row_layout.xml中的複選框在哪裏? – Sid 2012-02-14 21:47:33
OoooooooooopSSSSS!忘了把複選框放在我的xml中。越多的眼睛越好。謝謝。 – prolink007 2012-02-14 21:52:47