setOnCheckedChangeListener用于片段中的复选框

时间:2014-03-30 09:35:26

标签: android listview android-fragments android-checkbox

所以我对Android开发很新,我在这里学习。我一直在网上为列表视图做一些教程。

我现在就在这一个:http://www.survivingwithandroid.com/2013/02/android-listview-adapter-checkbox-item_7.html

现在,我已经按照教程进行了操作,但我添加了一些自己的改动。我没有按照教程所说的那样在MainActivity中执行所有操作,而是使用片段来扩充我的视图,注册上下文菜单等。fragment是我内部的内部类 MainActivity.class

我正处于教程中,我需要在CheckBox中添加listview。这似乎相当容易,所以我去实现OnCheckedChangeListener类中的fragment

public class MainActivity extends ActionBarActivity  {  
//PlaceholderFragment is an inner class of MainActivity
    public static class PlaceholderFragment extends Fragment implements android.widget.CompoundButton.OnCheckedChangeListener {

            ListView lv;
            public PlaceholderFragment() {
            }

            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                // TODO Auto-generated method stub
                 int pos = lv.getPositionForView(buttonView);
                 System.out.println("Pos ["+pos+"]");


         if (pos != ListView.INVALID_POSITION) {
                 Planet p = planetsList.get(pos);         
                 p.setChecked(isChecked);
        }
        }
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.fragment_main, container,
            false);
    ListView lv = (ListView) rootView.findViewById(R.id.listView);
    aAdpt = new PlanetAdapter(planetsList, getActivity());
    lv.setAdapter(aAdpt);
    registerForContextMenu(lv);
    lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {

         public void onItemClick(AdapterView<?> parentAdapter, View view, int position,
                                 long id) {


             Toast.makeText(getActivity(), "Item with id ["+id+"] - Position ["+position+"] - Genre ["+aAdpt.getItem(position).getDescr()+"]", Toast.LENGTH_SHORT).show();
         }
    });
    return rootView;
}

在getView方法的PlanetAdapter.class中,我有以下代码(根据教程):

public View getView(int position, View convertView, ViewGroup parent) {
    View v = convertView;

    PlanetHolder holder = new PlanetHolder();


    if (convertView == null) {
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        v = inflater.inflate(R.layout.checkbox_layout, parent, false);

        TextView tv = (TextView) v.findViewById(R.id.name);
        TextView distView = (TextView) v.findViewById(R.id.dist);
        CheckBox chk = (CheckBox) v.findViewById(R.id.chk);

        holder.chk = chk;
        holder.planetNameView = tv;
        holder.distView = distView;
        **chk.setOnCheckedChangeListener((MainActivity) context);** 
        v.setTag(holder);
    }
    else 
        holder = (PlanetHolder) v.getTag();

    Planet p = planetList.get(position);
    holder.planetNameView.setText(p.getName());
    holder.distView.setText("" + p.getDescr());
    holder.chk.setChecked(p.getChecked());


    return v;
} 

}

现在,当我尝试在Eclipse上运行代码时,会出现错误,因为监听器不再位于MainActivity chk.setOnCheckedChangeListener((MainActivity)context)中; - 我把它移到片段内部类。

现在,我将代码更改为 chk.setOnCheckedChangeListener((MainActivity.PlaceholderFragment)context); ,它表示我无法从Context转换为MainActivity.PlaceholderFragment

有谁知道如何解决这个问题?

1 个答案:

答案 0 :(得分:2)

更改此

 chk.setOnCheckedChangeListener((MainActivity) context)

 chk.setOnCheckedChangeListener(PlaceholderFragment.this)

您的Fragment实现了界面CompoundButton.OnCheckedChangeListener而不是您的活动

 public static class PlaceholderFragment extends Fragment implements android.widget.CompoundButton.OnCheckedChangeListener {

编辑:

似乎你在Adapter类中有复选框

因此,您需要PlanetAdapter来实施CompoundButton.OnCheckedChangeListener

更改为

chk.setOnCheckedChangeListener(PlanetAdapter.this);

并且Fragment不需要实现接口。所以改为

PlaceholderFragment extends Fragment

示例:

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        if (savedInstanceState == null) {
            getFragmentManager().beginTransaction()
                    .replace(R.id.container, new PlaceholderFragment()).commit();
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {

        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    /**
     * A placeholder fragment containing a simple view.
     */
    public static class PlaceholderFragment extends Fragment {

        public PlaceholderFragment() {
        }


        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {
            View rootView = inflater.inflate(R.layout.fragment_main, container,
                    false);

               ApplicationInfo applicationInfo = getActivity().getApplicationInfo();
                PackageManager pm = getActivity().getPackageManager();
                List<PackageInfo> pInfo = new ArrayList<PackageInfo>();
                pInfo.addAll(pm.getInstalledPackages(0));
                final AppInfo[] app_info = new AppInfo[pInfo.size()];

                int counter = 0;
                for(PackageInfo item: pInfo){
                    try{

                        applicationInfo = pm.getApplicationInfo(item.packageName, 1);

                        app_info[counter] = new AppInfo(pm.getApplicationIcon(applicationInfo), 
                                String.valueOf(pm.getApplicationLabel(applicationInfo)));

                        System.out.println(counter);

                    }
                    catch(Exception e){
                         e.printStackTrace();
                    }

                    counter++;
                }

            ListView listApplication = (ListView)rootView.findViewById(R.id.listView1);
            final AppInfoAdapter adapter = new AppInfoAdapter(getActivity(), app_info);
            listApplication.setAdapter(adapter);
            Button b= (Button)rootView.findViewById(R.id.button1);
            b.setOnClickListener(new OnClickListener()
            {

                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub

                    StringBuilder result = new StringBuilder();
                    for(int i=0;i<app_info.length;i++)
                    {
                        if(adapter.mCheckStates.get(i)==true)
                        {

                                           result.append(app_info[i].applicationName);
                            result.append("\n");
                        }

                    }
                    Toast.makeText(getActivity(), result, 1000).show();
                }

            });


            return rootView;
        }
    }

}

AppInfo.java

public class AppInfo {
    public Drawable icon;
    public String applicationName;

    public AppInfo(){
        super();
    }

    public AppInfo(Drawable icon, String applicationName){
        super();
        this.icon = icon;
        this.applicationName = applicationName;
    }


}

AppInfoAdapter.java

public class AppInfoAdapter extends ArrayAdapter<AppInfo> implements CompoundButton.OnCheckedChangeListener
{  SparseBooleanArray mCheckStates; 

    Context context;
    AppInfo  data[] = null;
    LayoutInflater mInflater;
    public AppInfoAdapter(Context context, AppInfo[] data){
        super(context, 0,data);
        this.context = context;
        this.data = data;
        mInflater = LayoutInflater.from(context);
        mCheckStates = new SparseBooleanArray(data.length);
    }

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

        View row = convertView;
        AppInfoHolder holder= null;

        if (row == null){
            row = mInflater.inflate(R.layout.list_item, parent, false);

            holder = new AppInfoHolder();

            holder.imgIcon = (ImageView) row.findViewById(R.id.imageView1);
            holder.txtTitle = (TextView) row.findViewById(R.id.textView1);
            holder.chkSelect = (CheckBox) row.findViewById(R.id.checkBox1);

            row.setTag(holder);

        }
        else{
            holder = (AppInfoHolder)row.getTag();
        }


        AppInfo appinfo = data[position];
        holder.txtTitle.setText(appinfo.applicationName);
        holder.imgIcon.setImageDrawable(appinfo.icon);
       // holder.chkSelect.setChecked(true);
        holder.chkSelect.setTag(position);
        holder.chkSelect.setChecked(mCheckStates.get(position, false));
        holder.chkSelect.setOnCheckedChangeListener(this);
        return row;

    }
    public boolean isChecked(int position) {
        return mCheckStates.get(position, false);
    }

    public void setChecked(int position, boolean isChecked) {
        mCheckStates.put(position, isChecked);

    }

    public void toggle(int position) {
        setChecked(position, !isChecked(position));

    }
@Override
public void onCheckedChanged(CompoundButton buttonView,
        boolean isChecked) {

     mCheckStates.put((Integer) buttonView.getTag(), isChecked);    

}
static class AppInfoHolder
{
    ImageView imgIcon;
    TextView txtTitle;
    CheckBox chkSelect;

}
}

fragment_main.xml

 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.listviewcheckbox.MainActivity$PlaceholderFragment" >

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:text="Button" />

    <ListView
        android:id="@+id/listView1"
        android:layout_above="@+id/button1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_marginTop="16dp" >
    </ListView>

</RelativeLayout>

list_item.xml

<?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="match_parent" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_marginLeft="34dp"
        android:layout_marginTop="21dp"
        android:text="TextView" />

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/textView1"
        android:layout_below="@+id/textView1"
        android:layout_marginTop="32dp"
        android:src="@drawable/ic_launcher" />

    <CheckBox
        android:id="@+id/checkBox1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_below="@+id/textView1"
        android:layout_marginRight="14dp"
        android:text="CheckBox" />

</RelativeLayout>

activity_main.xml中

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.listviewcheckbox.MainActivity"
    tools:ignore="MergeRootFrame" />

对齐

enter image description here