向上/向下滚动时检入复选框缺失

时间:2012-01-29 09:03:40

标签: java android

大家好,我的申请表有问题。检查我的复选框,一旦我勾选复选框并向上/向上滚动检查已经消失,我不知道这是怎么回事。谁能帮我这个?谢谢您的帮助 :) 这是我的代码:

public class TaskKiller extends Activity {
    private static final String TAG = "TaskKillerActivity";
    TaskListAdapter adapter;
    ListView lv;

    private List<TaskObject> getTasksToKill() {
        List<TaskObject> tol = new ArrayList<TaskObject>();

        for (int i = 0; i < adapter.getCount(); i++) {
            TaskObject to = adapter.getItem(i);
            if (to.isToKill()) {
                tol.add(to);
            }
        }

        return tol;
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();

    }

    public void loadRunningProcesses() {

        ActivityManager activityManager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);

        List<RunningAppProcessInfo> appinfolist = activityManager
                .getRunningAppProcesses();

        Log.d(TAG, "AppInfoList Size: " + appinfolist.size());

        for (ActivityManager.RunningAppProcessInfo runningAppProcessInfo : appinfolist) {
            TaskObject runningtask = new TaskObject();
            runningtask.setPid(runningAppProcessInfo.pid);
            runningtask.setProcessName(runningAppProcessInfo.processName);
            adapter.addTask(runningtask);
        }
    }

    class TaskObject {
        int pid;
        String processName;
        private boolean toKill;

        public int getPid() {
            return pid;
        }

        public void setPid(int pid) {
            this.pid = pid;
        }

        public String getProcessName() {
            return processName;
        }

        public void setProcessName(String processName) {
            this.processName = processName;
        }

        public boolean isToKill() {
            return toKill;
        }

        public void setToKill(boolean toKill) {
            this.toKill = toKill;
        }

    }

    class TaskListAdapter extends BaseAdapter {

        private static final String TAG = "TaskListAdapter";

        ArrayList<TaskObject> list;
        Context context;

        public TaskListAdapter(Context context) {
            Log.d(TAG, "created new task list adapter");
            this.context = context;
            if (list == null) {
                list = new ArrayList<TaskKiller.TaskObject>();
            }
        }

        public void addTask(TaskObject taskObject) {
            list.add(taskObject);
        }

        public void clearTasks() {
            list.clear();
            Log.d(TAG, "list size:" + list.size());
            this.notifyDataSetChanged();
        }

        public int getCount() {
            return list.size();
        }

        public TaskObject getItem(int position) {
            return list.get(position);
        }

        public long getItemId(int position) {
            return position;
        }

        public View getView(final int position, View convertView,
                ViewGroup parent) {
            RelativeLayout rl = new RelativeLayout(context);
            TextView textPid = new TextView(context);
            textPid.setId(222222);
            textPid.setText(Integer.toString(getItem(position).getPid()));

            TextView textName = new TextView(context);
            textName.setId(333333);
            textName.setText(getItem(position).getProcessName());

            CheckBox chckKill = new CheckBox(context);
            chckKill.setOnClickListener(new View.OnClickListener() {

                public void onClick(View v) {
                    // is chkIos checked?
                    if (((CheckBox) v).isChecked()) {
                        getItem(position).setToKill(true);
                    }

                }
            });
}

1 个答案:

答案 0 :(得分:3)

我认为问题在于您没有将项目的杀戮状态用于复选框。滚动时,会使用getView重新绘制 - 并重置为空。

添加

chckKill.setChecked( ((TaskObject) getItem(position) ).isToKill());

在构造函数之后。