使用自定义适配器实现复选框

时间:2013-03-29 18:55:23

标签: android listview checkbox adapter

我想在自定义适配器工作中获取我的复选框。但是,仍然有一小部分我没有得到,我的程序不断关闭每种类型,我试图测试它。

这是一个文件管理器,列出了SD卡中的所有图像文件。

public class FileBrowserMusic extends ListActivity {

    private File currentDir;
    private FileArrayAdapter adapter;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.music_main);

        currentDir = new File (Environment.getExternalStorageDirectory().getPath());
        List<Option>flss = new ArrayList<Option>();
        listFiles (currentDir, flss);
        Collections.sort (flss);
        ArrayAdapter<Option> adapter = new FileArrayAdapter(this, flss);
            setListAdapter(adapter);
        registerForContextMenu(getListView());
    }

    // Recursively search for all image files.
    private void listFiles (File f, List<Option> flss) {
        if(f.isDirectory()){
            File[] files = f.listFiles();

            try {
                for (File ff : files){
                    if(!ff.isDirectory()) {
                        if(ff.getName().endsWith(".jpg")) {
                            Log.d(" FILES",ff.getName());
                            flss.add(new Option (ff.getName(),"Files Size: "+ff.length(),ff.getAbsolutePath()));
                        }
                    }
                    else {
                        listFiles(ff, flss);
                    }
                }
            } catch (Exception e) {
                // TODO Auto-generated catch block
                //e.printStackTrace();
            } 
        }
    }

}

这是我的适配器类。

public class FileArrayAdapter extends ArrayAdapter<Option> {

    private Context c;
    private int id;
    private List<Option>items;
    private Activity context;

    public FileArrayAdapter(Activity context, List<Option> objects) {
        super(context, R.layout.music_view, objects);

        this.context = context;
        items = objects;
    }

    public Option getItem (int i) {
        return items.get (i);
    }

    static class ViewHolder {
        protected TextView name, size;
        protected CheckBox checkbox;
    }

    @Override
public View getView (int position, View convertView, ViewGroup parent) {
    // View v = convertView;
    final Option o = items.get(position);
    /*if (v == null) {
        LayoutInflater vi = (LayoutInflater)c.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        v = vi.inflate(R.layout.music_view, null);*/
    View v = null;
    final ViewHolder viewHolder = new ViewHolder();
    if (convertView == null) {
        LayoutInflater inflator = context.getLayoutInflater();
        v = inflator.inflate(R.layout.music_view, null);

        //TextView t1 = (TextView) v.findViewById(R.id.backDot);
        //TextView t2 = (TextView) v.findViewById(R.id.backPDir);
        viewHolder.name = (TextView) v.findViewById(R.id.name);
        viewHolder.size = (TextView) v.findViewById(R.id.size);
        viewHolder.checkbox = (CheckBox) v.findViewById(R.id.check);
        viewHolder.checkbox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                // TODO Auto-generated method stub
                Option element = (Option) viewHolder.checkbox.getTag();
                element.setSelected(buttonView.isChecked());
                Log.v("Checked : ", ""+ element.getName());
            }

        });

        v.setTag(viewHolder);
        viewHolder.checkbox.setTag(items.get(position));

    } else {
        v = convertView;
        ((ViewHolder) v.getTag()).checkbox.setTag(items.get(position));
    }

    ViewHolder holder = (ViewHolder) v.getTag();
    holder.name.setText(o.getName());
    holder.size.setText(o.getPath());
    holder.checkbox.setChecked(o.isSelected());

    return v;
}

}

我的选项类。

public class Option implements Comparable<Option>{

    private String name;
    private String data;
    private String path;
    boolean selected = false;

    public Option (String n, String d, String p) {
        name = n;
        data = d;
        path = p;       
    }

    public Option (String n, String d, String p, boolean s) {
        name = n;
        data = d;
        path = p;       
        selected = s;       
    }

    public void setName (String n) {
        name = n;
    }

    public void setData (String d) {
        data = d;
    }

    public void setPath (String p) {
        path = p;
    }

    public String getName() {
        return name;
    }

    public String getData() {
        return data;
    }

    public String getPath() {
        return path;
    }

    public boolean isSelected() {
        return selected;
    }

    public void setSelected (boolean s) {
        this.selected = s;
    }

    public int compareTo(Option o) {
        if (this.name != null) {
            return this.name.toLowerCase().compareTo(o.getName().toLowerCase());
        } else {
            throw new IllegalArgumentException();
        }
    }

}

最后,我的XML。

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent" android:layout_height="wrap_content">

    <TextView android:text="@+id/name" android:layout_width="wrap_content"
        android:layout_height="wrap_content" android:id="@+id/label"
        android:layout_marginLeft="15dp"
        android:layout_marginTop="5dp"
        android:textStyle="bold"
        android:textSize="20sp"></TextView>

    <TextView android:text="@+id/size"
        android:layout_below="@+id/label"
            android:layout_width="wrap_content"
        android:layout_height="wrap_content" android:id="@+id/sub"
        android:layout_marginLeft="15dp"
        android:textSize="15sp"></TextView>

    <CheckBox android:id="@+id/check" android:layout_width="wrap_content"
        android:layout_height="wrap_content" android:layout_marginLeft="4dp"
        android:layout_marginRight="10dp" android:layout_alignParentRight="true"
        ></CheckBox>
</RelativeLayout>

我哪里出错了?最初,我的代码仅用于列出listview中的图像文件。之后,我试图实现复选框但无法实现。

欣赏它。

由于所有的测试和内容,代码现在非常混乱。

T1和T2是我以前执行文件管理器时的代码。可以忽略它,不应该乱用我当前的代码我猜。我会将其删除并进行测试。

这是我的日志。

03-29 18:37:56.086: E/AndroidRuntime(4299): FATAL EXCEPTION: main
03-29 18:37:56.086: E/AndroidRuntime(4299): java.lang.NullPointerException
03-29 18:37:56.086: E/AndroidRuntime(4299):     at sg.joogle.safeit.FileArrayAdapter.getView(FileArrayAdapter.java:98)
03-29 18:37:56.086: E/AndroidRuntime(4299):     at android.widget.AbsListView.obtainView(AbsListView.java:2033)
03-29 18:37:56.086: E/AndroidRuntime(4299):     at android.widget.ListView.measureHeightOfChildren(ListView.java:1244)
03-29 18:37:56.086: E/AndroidRuntime(4299):     at android.widget.ListView.onMeasure(ListView.java:1155)
03-29 18:37:56.086: E/AndroidRuntime(4299):     at android.view.View.measure(View.java:12723)
03-29 18:37:56.086: E/AndroidRuntime(4299):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4698)
03-29 18:37:56.086: E/AndroidRuntime(4299):     at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1369)
03-29 18:37:56.086: E/AndroidRuntime(4299):     at android.widget.LinearLayout.measureVertical(LinearLayout.java:660)
03-29 18:37:56.086: E/AndroidRuntime(4299):     at android.widget.LinearLayout.onMeasure(LinearLayout.java:553)
03-29 18:37:56.086: E/AndroidRuntime(4299):     at android.view.View.measure(View.java:12723)
03-29 18:37:56.086: E/AndroidRuntime(4299):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4698)
03-29 18:37:56.086: E/AndroidRuntime(4299):     at android.widget.FrameLayout.onMeasure(FrameLayout.java:293)
03-29 18:37:56.086: E/AndroidRuntime(4299):     at android.view.View.measure(View.java:12723)
03-29 18:37:56.086: E/AndroidRuntime(4299):     at android.widget.LinearLayout.measureVertical(LinearLayout.java:812)
03-29 18:37:56.086: E/AndroidRuntime(4299):     at android.widget.LinearLayout.onMeasure(LinearLayout.java:553)
03-29 18:37:56.086: E/AndroidRuntime(4299):     at android.view.View.measure(View.java:12723)
03-29 18:37:56.086: E/AndroidRuntime(4299):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4698)
03-29 18:37:56.086: E/AndroidRuntime(4299):     at android.widget.FrameLayout.onMeasure(FrameLayout.java:293)
03-29 18:37:56.086: E/AndroidRuntime(4299):     at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2092)
03-29 18:37:56.086: E/AndroidRuntime(4299):     at android.view.View.measure(View.java:12723)
03-29 18:37:56.086: E/AndroidRuntime(4299):     at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1064)
03-29 18:37:56.086: E/AndroidRuntime(4299):     at android.view.ViewRootImpl.handleMessage(ViewRootImpl.java:2442)
03-29 18:37:56.086: E/AndroidRuntime(4299):     at android.os.Handler.dispatchMessage(Handler.java:99)
03-29 18:37:56.086: E/AndroidRuntime(4299):     at android.os.Looper.loop(Looper.java:137)
03-29 18:37:56.086: E/AndroidRuntime(4299):     at android.app.ActivityThread.main(ActivityThread.java:4424)
03-29 18:37:56.086: E/AndroidRuntime(4299):     at java.lang.reflect.Method.invokeNative(Native Method)
03-29 18:37:56.086: E/AndroidRuntime(4299):     at java.lang.reflect.Method.invoke(Method.java:511)
03-29 18:37:56.086: E/AndroidRuntime(4299):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
03-29 18:37:56.086: E/AndroidRuntime(4299):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
03-29 18:37:56.086: E/AndroidRuntime(4299):     at dalvik.system.NativeStart.main(Native Method)

1 个答案:

答案 0 :(得分:0)

你检查o! - null,但是在那个if块之外你分配了setText 使用o.getName。那个时候o真的可能是空的吗?