我正在给我的操作栏添加一个复选框。此复选框将用于选择listactivity的所有项目。 butsetOnCheckedChangeListener不适用于此复选框。 我为我的操作栏创建了一个单独的布局,并通过java代码动态地膨胀它。(在oncreate()中)。 下面是我的ListActivity代码:我还必须在我的自定义操作栏布局中点击发送按钮上的webservice。如果有人有,请给我一个解决方案。
package com.logiquemantra.frapps;
import java.util.ArrayList;
import java.util.List;
import com.logiquemantra.frapps.R;
import com.logiquemantra.frapps.adapter.ApplicationAdapter;
import android.annotation.SuppressLint;
import android.app.ActionBar;
import android.app.ListActivity;
import android.app.ProgressDialog;
import android.content.ActivityNotFoundException;
import android.content.Intent;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.View;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ListView;
import android.widget.Toast;
import android.widget.CompoundButton.OnCheckedChangeListener;
public class AllAppsActivity extends ListActivity{
private PackageManager packageManager = null;
private List<ApplicationInfo> applist = null;
private ApplicationAdapter listadaptor = null;
CheckBox selectAll;
@SuppressLint("NewApi")
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.listofapps);
packageManager = getPackageManager();
new LoadApplications().execute();
ActionBar mActionBar = getActionBar();
mActionBar.setDisplayShowHomeEnabled(true);
mActionBar.setDisplayShowTitleEnabled(false);
LayoutInflater mInflater = LayoutInflater.from(this);
View mCustomView = mInflater.inflate(R.layout.custom_app_share_action_bar, null);
selectAll = (CheckBox)mCustomView.findViewById(R.id.selectAll);
mActionBar.setCustomView(mCustomView);
mActionBar.setDisplayShowCustomEnabled(true);
selectAll.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
// TODO Auto-generated method stub
if(isChecked){
for(int i = 0 ; i<applist.size(); i++){
getListView().setItemChecked(i, true);
}
}else{
for(int i = 0 ; i<applist.size(); i++){
getListView().setItemChecked(i, false);
}
}
}
});
}
/*@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu items for use in the action bar
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.share_app_info, menu);
return super.onCreateOptionsMenu(menu);
} */
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
ApplicationInfo app = applist.get(position);
try {
Intent intent = packageManager
.getLaunchIntentForPackage(app.packageName);
if (null != intent) {
startActivity(intent);
}
} catch (ActivityNotFoundException e) {
Toast.makeText(AllAppsActivity.this, e.getMessage(),
Toast.LENGTH_LONG).show();
} catch (Exception e) {
Toast.makeText(AllAppsActivity.this, e.getMessage(),
Toast.LENGTH_LONG).show();
}
}
private List<ApplicationInfo> checkForLaunchIntent(List<ApplicationInfo> list) {
ArrayList<ApplicationInfo> applist = new ArrayList<ApplicationInfo>();
for (ApplicationInfo info : list) {
try {
if (null != packageManager.getLaunchIntentForPackage(info.packageName)) {
applist.add(info);
}
} catch (Exception e) {
e.printStackTrace();
}
}
return applist;
}
class LoadApplications extends AsyncTask<Void, Void, Void>{
private ProgressDialog progress = null;
@Override
protected Void doInBackground(Void... params) {
applist = checkForLaunchIntent(packageManager.getInstalledApplications(PackageManager.GET_META_DATA));
listadaptor = new ApplicationAdapter(AllAppsActivity.this,R.layout.listofapps, applist);
return null;
}
@Override
protected void onCancelled() {
super.onCancelled();
}
@Override
protected void onPostExecute(Void result) {
setListAdapter(listadaptor);
progress.dismiss();
super.onPostExecute(result);
}
@Override
protected void onPreExecute() {
progress = ProgressDialog.show(AllAppsActivity.this, null,
"Loading application info...");
super.onPreExecute();
}
@Override
protected void onProgressUpdate(Void... values) {
super.onProgressUpdate(values);
}
}
}
这是我的自定义布局:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="50dp"
android:background="#000"
android:gravity="center_vertical">
<TextView
android:id="@+id/title_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:textColor="#fff"
android:text="Applications"
android:textSize="18sp"
android:paddingTop="3dp"
/>
<LinearLayout android:id="@+id/ll"
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:layout_alignParentRight="true"
>
<CheckBox android:id="@+id/selectAll"
android:text="@string/selectall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:checked="true"
android:textColor="#fff"
android:paddingRight="15dp"
/>
<ImageButton
android:id="@+id/imageButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignRight="@id/selectAll"
android:layout_centerVertical="true"
android:layout_marginRight="8dp"
android:background="@null"
android:src="@drawable/ic_action_send_now"
/>
</LinearLayout>
</RelativeLayout>
答案 0 :(得分:0)
我刚遇到这个问题。你无法通过这种方式获得CheckBox的视图。
LayoutInflater mInflater = LayoutInflater.from(this);
View mCustomView = mInflater.inflate(R.layout.custom_app_share_action_bar, null);
selectAll = (CheckBox)mCustomView.findViewById(R.id.selectAll);
您应将其更改为:
View mCustomView=getActionBar().getCustomView();
selectAll = (CheckBox)mCustomView.findViewById(R.id.selectAll);
我希望它能为你效劳。