gpt4 book ai didi

Android,ListView 搜索/过滤 -> 重复条目

转载 作者:行者123 更新时间:2023-11-29 00:43:23 25 4
gpt4 key购买 nike

如标题所述,我在过滤/搜索我的 ListView 时遇到了问题。当我尝试搜索“A”时,listView 显示 A-31 两次,A-32 两次,依此类推。有谁知道为什么会发生这种情况?

package hpliu.grupp2;

import android.content.Intent;
import android.os.Bundle;
import android.os.Debug;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.TableRow;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import android.app.ListActivity;
import android.app.Activity;

import android.widget.EditText;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.Toast;

public class SearchRoomActivity extends ListActivity {
private EditText filterText = null;
SimpleAdapter menu = null;
ListView lv = null;

static final String[] AHalls = new String[] { "A-31", "A-32", "A-33",
"A-34", "A-35", "A-36", "A-37" };

String[] from = new String[] { "roomName", "buildingName" };

/** Called when the activity is first created. */

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.search_room);

createCampusList("Campus Valla, Linköping",
createHashMap(AHalls, "A Huset"));

// filterText = (EditText) findViewById(R.id.search_box);
// filterText.addTextChangedListener(filterTextWatcher);
}

private TextWatcher filterTextWatcher = new TextWatcher() {

public void afterTextChanged(Editable s) {
}

public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
}

@Override
public void onTextChanged(CharSequence s, int start, int before,
int count) {
menu.getFilter().filter(s);
}

};

protected void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);

try {
@SuppressWarnings("unchecked")
HashMap<String, String> toHash = (HashMap<String, String>) this
.getListAdapter().getItem(position); // {text1 = "A-31",
// text2 = "Herp"};

Toast.makeText(
this,
"Searching for... " + " "
+ toHash.get("roomName").toString(),
Toast.LENGTH_SHORT).show();
} catch (Exception e) {
e.printStackTrace();
}
}

public List<HashMap<String, String>> createHashMap(String[] Halls,
String Building) {
List<HashMap<String, String>> fillMaps = new ArrayList<HashMap<String, String>>();
for (int i = 0; i < Halls.length; i++) {
HashMap<String, String> map = new HashMap<String, String>();
map.put(from[0], Halls[i]);
map.put(from[1], Building);
fillMaps.add(map);
}
return fillMaps;
}

public List<HashMap<String, String>> createHashMap(String[] Halls,
String[] Buildings) {
List<HashMap<String, String>> fillMaps = new ArrayList<HashMap<String, String>>();
for (int i = 0; i < Halls.length; i++) {
HashMap<String, String> map = new HashMap<String, String>();
map.put(from[0], Halls[i]);
map.put(from[1], Buildings[i]);
fillMaps.add(map);
}
return fillMaps;
}

public void createCampusList(String campusHeader,
List<HashMap<String, String>> fillMaps) {

int[] to = new int[] { R.id.roomName, R.id.buildingName };

menu = new SimpleAdapter(this, fillMaps, R.layout.list_item, from, to);
setListAdapter(menu);
lv = getListView();
lv.setTextFilterEnabled(true);
}

@Override
protected void onDestroy() {
super.onDestroy();
filterText.removeTextChangedListener(filterTextWatcher);
}
}

最佳答案

如果您过滤每个项目中有两个字符串的适配器,过滤器将通过适配器两次。这就是为什么你有倍数。如果每个字符串中包含三个字符串,则可能会得到三个重复项。

我在网上找到的一个解决方案是创建自定义 SimpleAdapter 并在填充 ListView 之前检查重复项。

关于Android,ListView 搜索/过滤 -> 重复条目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8014919/

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