gpt4 book ai didi

android - 过滤 ListView 突出显示不正确的项目

转载 作者:行者123 更新时间:2023-12-05 07:50:57 25 4
gpt4 key购买 nike

我有一个 ListView ,我在其中通过 SearchView 过滤项目。在激活某个项目的状态时,它没有获得正确的项目,而是从该位置获得它。为了更清楚,请引用以下屏幕截图:

  1. 搜索关键字 com 并选择筛选项(即 activated_state)

enter image description here

  1. 清除过滤器时,当项目的位置发生变化时,它不会跟踪所选项目,即 com.android.gesture.builder:

enter image description here

无论位置如何变化,我都希望选择是正确的。

我在 MainActivity 中的这部分代码:

 apps.setChoiceMode(apps.CHOICE_MODE_MULTIPLE_MODAL);
apps.setMultiChoiceModeListener(new AbsListView.MultiChoiceModeListener() {
@Override
public void onItemCheckedStateChanged(ActionMode mode, int position, long id, boolean checked) {


if (apps.isItemChecked(position)) {


Adapter.getItem(position);
Toast.makeText(getApplicationContext(), "CHECKED", Toast.LENGTH_LONG).show();

count = count + 1;
mode.setTitle(count + "items selected");


list_apps.add(Adapter.filteredData.get(position).packageName);



list_apps.trimToSize();

}

else {
Toast.makeText(getApplicationContext(), "UNCHECKED" , Toast.LENGTH_LONG).show();


count--;
mode.setTitle(count + "items selected");

list_apps.remove(Adapter.filteredData.get(position).packageName);


list_apps.trimToSize();

}

我正在使用扩展的 baseAdapter,如果您也需要查看该代码,请告诉我。

更新:

我在代码中有 OnItemClick 监听器:

    apps.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
/*
for (int i = 0; i < packageList.size(); i++) {
TextView txtview = ((TextView) arg1.findViewById(R.id.textView1));

product = txtview.getText().toString();

list_apps.add(Adapter.filteredData.get(arg2).packageName);



//Toast.makeText(MainActivity.this, product,
// Toast.LENGTH_SHORT).show();
//for
}
*/
String selection;
selection = Adapter.filteredData.get(arg2).packageName;

string = (String) Adapter.getItem(arg2);

//list_apps.trimToSize();
Toast.makeText(MainActivity.this, selection,
Toast.LENGTH_SHORT).show();

I am using activated_state for the item selected on filtering and maintaining that selection.

最佳答案

如果我没有弄错,那么你需要使用 OnItemClick 监听器,并像这样获取项目。

搜索 View 更改列表项的位置,但是当我们从适配器获取项目时,它返回当前项目。

lv.setOnItemClickListener(new OnItemClickListener()
{
@Override
public void onItemClick(AdapterView<?> adapter, View v, int position,
long arg3)
{
SString itemName = (String) adapter.getAdapter().getItem(position);

// assuming string and if you want to get the value on click of list item
// do what you intend to do on click of listview row
}
});

关于android - 过滤 ListView 突出显示不正确的项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35649339/

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