2014-10-17 3 views
0

내 작업 표시 줄에 체크 표시를하고 있습니다. 이 확인란은 내 listactivity의 모든 항목을 선택하는 데 사용됩니다. butsetOnCheckedChangeListener가이 체크 박스에서 작동하지 않습니다. 내 작업 표시 줄에 대한 별도의 레이아웃을 만들고 Java 코드를 통해 동적으로 확장합니다 (oncreate()). 아래 ListActivity에 대한 내 코드는 다음과 같습니다. 또한 webservice를 액션 버튼의 사용자 정의 레이아웃에서 보내기 버튼 클릭으로 호출해야합니다. 누구든지 가지고 있다면 해결책을주십시오. 난 그냥이 문제를 충족OnCheckedChangeListener가 체크 박스로 작동하지 않습니다.

<?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

:

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); 
     } 

    } 

} 

여기 내 사용자 지정 레이아웃입니다. 이 방법으로 CheckBox의 뷰를 가져올 수 없습니다. 내가 당신을 위해 일할 수 있기를 바랍니다

View mCustomView=getActionBar().getCustomView(); 
selectAll = (CheckBox)mCustomView.findViewById(R.id.selectAll); 

:

LayoutInflater mInflater = LayoutInflater.from(this); 
View mCustomView = mInflater.inflate(R.layout.custom_app_share_action_bar, null); 
selectAll = (CheckBox)mCustomView.findViewById(R.id.selectAll); 

당신은 그것으로 변경해야합니다.

+0

널 포인터 예외 –

관련 문제