我创建了一个自定义列表视图,其中列出了设备上所有已安装的应用,此列表中包含应用图标,应用名称和复选框。我想编码onCheckboxClicked方法,但我不知道在哪里放置它,我已经读过它应该在适配器内,但我一直在看的其他示例在主要活动中有代码,我似乎无法让它以任何方式工作。
public class MainActivity extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//retrieve currently installed apps
final Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
final List pkgAppsList = getPackageManager().queryIntentActivities( mainIntent, 0);
List<AppDetails> appList = new ArrayList<>();
for (Object object : pkgAppsList)
{
ResolveInfo info = (ResolveInfo) object;
Drawable icon = getBaseContext().getPackageManager().getApplicationIcon(info.activityInfo.applicationInfo);
String strAppName = info.activityInfo.applicationInfo.publicSourceDir.toString();
String strPackageName = info.activityInfo.applicationInfo.packageName.toString();
final String title = (String)((info != null) ? getBaseContext().getPackageManager().getApplicationLabel(info.activityInfo.applicationInfo) : "???");
AppDetails tmp = new AppDetails(title, icon);
appList.add(tmp); //add title and icon into list
}
final ListView listview = (ListView) findViewById(R.id.listView);
final AppDetailsAdapter adapter = new AppDetailsAdapter(this, appList);
listview.setAdapter(adapter);
}
适配器:
public class AppDetailsAdapter extends BaseAdapter {
private List<AppDetails> data;
private Context context;
public AppDetailsAdapter(Context context, List<AppDetails> data) {
this.context = context;
this.data = data;
}
@Override
public int getCount() {
return data.size();
}
@Override
public Object getItem(int position) {
return data.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = convertView;
if (view == null) {
LayoutInflater vi = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = vi.inflate(R.layout.custom_row_layout, null);
}
ImageView icon = (ImageView) view.findViewById(R.id.icon);
TextView text = (TextView) view.findViewById(R.id.text);
final AppDetails item = data.get(position);
text.setText(item.name);
icon.setImageDrawable(item.icon);
return view;
}
}
自定义布局:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="60dp"
android:clickable="true"
android:orientation="horizontal"
android:weightSum="1">
<ImageView
android:id="@+id/icon"
android:layout_width="39dp"
android:layout_height="39dp"
android:layout_gravity="center_vertical"
android:layout_marginLeft="16dp" />
<TextView
android:id="@+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_gravity="center_vertical"
android:layout_marginLeft="16dp"
android:textSize="17sp" />
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="right" >
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/checkBox"
android:onClick="onCheckboxClicked"/>/>
</LinearLayout>
我还读过需要修改getView方法来处理复选框但是我似乎无法用到目前为止尝试过的方法做到这一点
答案 0 :(得分:0)
使用以下代码替换适配器的获取视图代码:
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = convertView;
if (view == null) {
LayoutInflater vi = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = vi.inflate(R.layout.custom_row_layout, null);
}
ImageView icon = (ImageView) view.findViewById(R.id.icon);
TextView text = (TextView) view.findViewById(R.id.text);
CheckBox checkBox = (CheckBox) view.findViewById(R.id.checkBox);
final AppDetails item = data.get(position);
text.setText(item.name);
icon.setImageDrawable(item.icon);
checkBox .setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
// Handle your conditions here
}
});
return view;
}
此处setOnCheckedChangeListener
是您必须处理的方法。