gpt4 book ai didi

java - 向上/向下滚动时检查复选框缺失

转载 作者:行者123 更新时间:2023-12-02 00:31:08 28 4
gpt4 key购买 nike

大家好,我的应用程序遇到问题。我的复选框中的复选框,一旦我选中该复选框并向下/向上滚动,该复选框就消失了,我不知道这是怎么发生的。谁能帮我这个?谢谢您的帮助 :)这是我的代码:

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

}
});
}

最佳答案

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

添加

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

在构造函数之后。

关于java - 向上/向下滚动时检查复选框缺失,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9052069/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com