gpt4 book ai didi

java - 如何让过滤后的搜索结果显示在listview(android)中

转载 作者:行者123 更新时间:2023-12-01 04:52:41 25 4
gpt4 key购买 nike

我有一个编辑文本和一个包含数据的 ListView ,因此在编辑文本中给出关键字后,应该显示 ListView 中的相关关键字。为此,我已经完成了编码。问题是过滤后的搜索项在 toast 中显示为一个再见,但我需要将其显示在 ListView 中,有人能告诉我我哪里出错了吗?

public class Home extends ListActivity {
ArrayList<HashMap<String, String>> songsList;
ListView list;
LazyAdapter adapter;
JSONArray posts;



//ArrayList thats going to hold the search results
ArrayList<HashMap<String, String>> searchResults;
LayoutInflater inflater;
// All static variables
static final String URL = "http://exm/ads/?json=get_recent_posts";


static final String KEY_POSTS = "posts";
static final String KEY_ID = "id";
static final String KEY_TITLE = "title";
static final String KEY_DATE = "date";
static final String KEY_CONTENT = "content";
static final String KEY_AUTHOR = "author";
static final String KEY_NAME = "name";
static final String KEY_ATTACHMENTS = "attachments";
static final String KEY_SLUG = "slug";
static final String KEY_THUMB_URL = "thumbnail";
static final String KEY_IMAGES = "images";
static final String KEY_URL = "url";



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

final EditText searchBox=(EditText) findViewById(R.id.search);
final ListView list=(ListView)findViewById(android.R.id.list);

//get the LayoutInflater for inflating the customomView
//this will be used in the custom adapter
inflater=(LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);





final ArrayList<HashMap<String, String>> songsList = new ArrayList<HashMap<String, String>>();


// Creating JSON Parser instance
final JSONParser jParser = new JSONParser();

// getting JSON string from URL
JSONObject json = jParser.getJSONFromUrl(URL);
try {
posts = json.getJSONArray(KEY_POSTS);

// looping through all song nodes <song>
for(int i = 0; i < posts.length(); i++){
JSONObject c = posts.getJSONObject(i);
// Storing each json item in variable
String id = c.getString(KEY_ID);
String title = c.getString(KEY_TITLE);
String date = c.getString(KEY_DATE);
String content = c.getString(KEY_CONTENT);
// to remove all <P> </p> and <br /> and replace with ""
content = content.replace("<br />", "");
content = content.replace("<p>", "");
content = content.replace("</p>", "");

//authornumber is agin JSON Object
JSONObject author = c.getJSONObject(KEY_AUTHOR);
String name = author.getString(KEY_NAME);

String url = null;
String slug = null;
try {
JSONArray atta = c.getJSONArray("attachments");
for(int j = 0; j < atta.length(); j++){
JSONObject d = atta.getJSONObject(j);

slug = d.getString(KEY_SLUG);

JSONObject images = d.getJSONObject(KEY_IMAGES);

JSONObject thumbnail = images.getJSONObject(KEY_THUMB_URL);
url = thumbnail.getString(KEY_URL);

}
} catch (Exception e) {
e.printStackTrace();

}




// creating new HashMap
HashMap<String, String> map = new HashMap<String, String>();

// adding each child node to HashMap key => value
map.put(KEY_ID, id);
map.put(KEY_TITLE, title);
map.put(KEY_DATE, date);
map.put(KEY_NAME, name);
map.put(KEY_CONTENT, content);
map.put(KEY_SLUG, slug);
map.put(KEY_URL, url);


// adding HashList to ArrayList
songsList.add(map);
}
}catch (JSONException e) {
e.printStackTrace();

}


//searchResults=OriginalValues initially
searchResults=new ArrayList<HashMap<String, String>>(songsList);


// Getting adapter by passing json data ArrayList
adapter=new LazyAdapter(this, songsList);
list.setAdapter(adapter);


searchBox.addTextChangedListener(new TextWatcher() {

public void onTextChanged(CharSequence s, int start, int before, int count) {
//get the text in the EditText
String searchString=searchBox.getText().toString();
int textLength=searchString.length();

//clear the initial data set
searchResults.clear();

for(int i=0;i<songsList.size();i++)
{
String playerName=songsList.get(i).get("title").toString();

if(textLength<=playerName.length()){

//compare the String in EditText with Names in the ArrayList
if(searchString.equalsIgnoreCase(playerName.substring(0,textLength)))
Toast.makeText(getApplicationContext(),playerName,1).show();
searchResults.add(songsList.get(i));
}
}

adapter.notifyDataSetChanged();
}

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


}

public void afterTextChanged(Editable s) {


}
});

最佳答案

解决方案

您没有更改适配器中的基础数据。您应该更改适配器的数据:例如尝试替换

adapter.notifyDataSetChanged();

与 适配器=new LazyAdapter(this, searchResults);
list.setAdapter(适配器);

告诉适配器新数据是searchResults

<小时/>

好的解决方案您应该使用 Filter在您的适配器中过滤结果。

关于java - 如何让过滤后的搜索结果显示在listview(android)中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14704342/

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