Android - 如何在不选择ListView中的项目的情况下检查它?
我在 ListView(simple_list_item_multiple_choice)
中显示正在运行的任务,并使用按钮的 onClick
事件从列表中删除选中的任务,因为我们无法在 Android 中完全终止任务,所以我设置了Timer
每 5 秒重新加载这些任务。
我正在使用一个简单的 CheckBox
小部件来一次检查 Listview
的所有这些 CheckBox
。
所以我的问题是,如果我的 CheckBox
小部件设置为选中,我希望在重新加载时检查新项目,但我无法做到这一点。
这是我的代码:-
public void reloadTasks()
{
int listInitSize = list.size();
try
{
List<ActivityManager.RunningAppProcessInfo> tasks = am.getRunningAppProcesses();
int numOfTasks = tasks.size();
for(int i = 0; i < numOfTasks; i++)
{
ActivityManager.RunningAppProcessInfo task = tasks.get(i);
boolean doAdd = true;
HashMap<String, String> item = new HashMap<String, String>();
item.put("Process", task.processName);
try
{
PackageInfo myPInfo = getPackageManager().getPackageInfo(task.processName, 0);
item.put("Name", myPInfo.applicationInfo.loadLabel(getPackageManager()).toString());
}
catch (PackageManager.NameNotFoundException ne)
{
}
if(!list.isEmpty())
{
if(list.contains(item))
{
doAdd = false;
}
else
{
doAdd = true;
}
}
if(filter == true)
{
int size = SystemProcessList.length;
for (int j = 0; j < size; j++)
{
if(task.processName.indexOf(SystemProcessList[j]) > -1)
{
doAdd = false;
}
}
}
if(doAdd==true)
{
addItem(item);
}
}
}
catch (SecurityException se)
{
}
notes.notifyDataSetChanged();
int size = list.size();
int numOfLoop = list.size() - listInitSize;
for(int i = 1; i >= numOfLoop; i++)
{
if(cb.isChecked())
{
lv.setItemChecked(size-i, true);
}
}
}
我尝试了一些方法来检查重新加载时的项目,但它不起作用,并且此尝试不允许我的应用程序在模拟器上加载(不是强制关闭只是一个空白屏幕)。
I am displaying running tasks in a ListView(simple_list_item_multiple_choice)
and removing checked tasks from the list using button's onClick
event as we can not kill the tasks completely in android so i have set the Timer
to reload these tasks after every 5 seconds.
I am using one simple CheckBox
widget to check all these CheckBox
of Listview
at once.
So my problem is that i want new items to be checked at the time of reload if my CheckBox
widget is set to checked but i am unable to do that.
Here is my code :-
public void reloadTasks()
{
int listInitSize = list.size();
try
{
List<ActivityManager.RunningAppProcessInfo> tasks = am.getRunningAppProcesses();
int numOfTasks = tasks.size();
for(int i = 0; i < numOfTasks; i++)
{
ActivityManager.RunningAppProcessInfo task = tasks.get(i);
boolean doAdd = true;
HashMap<String, String> item = new HashMap<String, String>();
item.put("Process", task.processName);
try
{
PackageInfo myPInfo = getPackageManager().getPackageInfo(task.processName, 0);
item.put("Name", myPInfo.applicationInfo.loadLabel(getPackageManager()).toString());
}
catch (PackageManager.NameNotFoundException ne)
{
}
if(!list.isEmpty())
{
if(list.contains(item))
{
doAdd = false;
}
else
{
doAdd = true;
}
}
if(filter == true)
{
int size = SystemProcessList.length;
for (int j = 0; j < size; j++)
{
if(task.processName.indexOf(SystemProcessList[j]) > -1)
{
doAdd = false;
}
}
}
if(doAdd==true)
{
addItem(item);
}
}
}
catch (SecurityException se)
{
}
notes.notifyDataSetChanged();
int size = list.size();
int numOfLoop = list.size() - listInitSize;
for(int i = 1; i >= numOfLoop; i++)
{
if(cb.isChecked())
{
lv.setItemChecked(size-i, true);
}
}
}
I tried something to check the items at reload but it's not working and this attempt is not letting my app to get load on emulator(not force close just a blank screen).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您应该使用 listadapter 并创建一个扩展 ArrayAdapter 的类,然后在 getView 函数中您应该实现每一行。这并不容易,您可以在这里看到一个工作示例
http://www.softwarepassion.com/android-series-custom-listview-items-and-adapters/
You should use a listadapter and create a class that extends ArrayAdapter and then in your getView function you should implement each row. It is not so much easy, you can see a working example here
http://www.softwarepassion.com/android-series-custom-listview-items-and-adapters/