gpt4 book ai didi

android - 可搜索 ListView

转载 作者:行者123 更新时间:2023-11-30 02:08:42 25 4
gpt4 key购买 nike

所以我正在尝试使用自定义数组适配器制作可搜索的 ListView 。

我终于让搜索工作了,但结果并不完全准确。

这实际上是一种碰运气的事情,例如搜索 Drupal 让我得到了 Google Plus,你知道我在说什么吗?

这是我的适配器

public class CustomListAdapter extends ArrayAdapter<String> implements Filterable {

private final Activity context;
private final String[] web;
private final String[] imageIdUrl;


public CustomListAdapter(Activity context, String[] web, String[] imageId) {
super(context, R.layout.list_single, web);
this.context = context;
this.web = web;
this.imageIdUrl = imageId;

}

@Override
public View getView(int position, View view, ViewGroup parent) {
LayoutInflater inflater = context.getLayoutInflater();
//find row view
View rowView= inflater.inflate(R.layout.list_single, null, true);
//find text
TextView txtTitle = (TextView) rowView.findViewById(R.id.txt);
//find image
ImageView imageView = (ImageView) rowView.findViewById(R.id.img);
//set text
txtTitle.setText(web[position]);
//set image traditional
//imageView.setImageResource(imageIdUrl[position]);
//set image with picasso
Picasso.with(this.context).load(imageIdUrl[position]).into(imageView);
//return row
return rowView;
}
}

这是我的主要 Activity 类

 public class MainActivity extends ActionBarActivity {

ListView list;
EditText editsearch;

String[] web = {
"Google Plus",
"Twitter",
"Windows",
"Bing",
"Itunes",
"Wordpress",
"Drupal"} ;
String[] imageIdUrl = {
"http://www.h3dwallpapers.com/wp-content/uploads/2015/05/Google_Logo_04.jpg",
"http://www.h3dwallpapers.com/wp-content/uploads/2015/05/Google_Logo_04.jpg" ,
"http://www.h3dwallpapers.com/wp-content/uploads/2015/05/Google_Logo_04.jpg",
"http://www.h3dwallpapers.com/wp-content/uploads/2015/05/Google_Logo_04.jpg",
"http://www.h3dwallpapers.com/wp-content/uploads/2015/05/Google_Logo_04.jpg",
"http://www.h3dwallpapers.com/wp-content/uploads/2015/05/Google_Logo_04.jpg",
"http://www.h3dwallpapers.com/wp-content/uploads/2015/05/Google_Logo_04.jpg"};

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

final CustomListAdapter adapter = new CustomListAdapter(MainActivity.this, web, imageIdUrl);
list=(ListView)findViewById(R.id.list);
list.setAdapter(adapter);
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(MainActivity.this, "You Clicked at " + web[+position], Toast.LENGTH_SHORT).show();
}
});

// Locate the EditText in listview_main.xml
editsearch = (EditText) findViewById(R.id.editText);

editsearch.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence cs, int arg1, int arg2, int arg3) {
String text = editsearch.getText().toString().toLowerCase(Locale.getDefault());
adapter.getFilter().filter(text);
}
@Override
public void beforeTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {
// TODO Auto-generated method stub
}
@Override
public void afterTextChanged(Editable arg0) {
// TODO Auto-generated method stub
}
});

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();

//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}

return super.onOptionsItemSelected(item);
}
}

如果有任何帮助,我将不胜感激。事实证明这是一个令人头疼的问题

最佳答案

将 onTextChanged() 方法中的代码更改为 afterTextChanged() 方法,如下所示:

@Override
public void afterTextChanged(Editable s) {

String str=s.toString();
getSearchItems(str);
}

在 getSearchItems 中正确使用过滤器。我使用其他方式进行过滤,

public void getSearchItems(String searchText){
List<Employee> searchList=new ArrayList<Employee>();
List<Employee> employeeList=getEmployeeList();
if(employeeList2!=null){
for(Employee e:employeeList){
if(e.name.toLowerCase().contains(searchText.toLowerCase())){
searchList.add(e);
}
}
}
adapter.refresh(searchList);
}

然后在适配器中使用另一种方法 refresh() 如下:

public void refresh(List<Employee> employeeList){
this.employeeList=employeeList;
notifyDataSetChanged();
}

实际上我使用了包含他的姓名和地址的 Employee 类,并且我使用员工姓名进行了搜索。因此,您更改上面的代码以适合您的搜索。

关于android - 可搜索 ListView ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30411199/

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