我正在制作一个自定义列表视图,我在列表视图的顶部采用编辑文本来搜索列表视图中的项目但是当我在编辑文本中输入任何值时,它会在列表视图中搜索但是当我清除编辑测试时不会显示原始列表视图
我的主要活动代码是
package com.example.searchonlist;
import java.util.ArrayList;
import android.app.Activity;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.EditText;
import android.widget.ListView;
public class MainActivity extends Activity implements TextWatcher {
private FriendListAdapter friendListAdapter;
private EditText inputSearch;
private ListView friendList;
private ArrayList<String> frndArrList;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
inputSearch = (EditText) findViewById(R.id.inputSearch);
inputSearch.addTextChangedListener(this);
frndArrList = new ArrayList<String>();
frndArrList.add("Ashutosh");
frndArrList.add("ShreeVridhee");
frndArrList.add("Tanishi");
frndArrList.add("Divyanu");
frndArrList.add("Adarsh");
frndArrList.add("Akshara");
frndArrList.add("Pooja");
frndArrList.add("NoOne");
friendList = (ListView) findViewById(R.id.listView1);
friendListAdapter = new FriendListAdapter(this, frndArrList);
friendList.setAdapter(friendListAdapter);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
Log.e("EditText", s.toString());
friendListAdapter.getFilter().filter(s.toString());
}
@Override
public void afterTextChanged(Editable s) {
}
@Override
public void onBackPressed() {
super.onBackPressed();
}
}
我的列表适配器是
package com.example.searchonlist;
import java.util.ArrayList;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Filter;
import android.widget.Filterable;
import android.widget.TextView;
public class FriendListAdapter extends BaseAdapter implements Filterable {
MainActivity context;
ArrayList<String> frndArrList;
ArrayList<String> filterArrList;
private FriendFilter friendFilter;
public FriendListAdapter(MainActivity context, ArrayList<String> frndArrList) {
this.context = context;
this.frndArrList = frndArrList;
this.filterArrList = frndArrList;
}
@Override
public int getCount() {
return frndArrList.size();
}
@Override
public Object getItem(int position) {
return position;
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View view, ViewGroup parent) {
LayoutInflater layoutInflater = LayoutInflater.from(context);
view = layoutInflater.inflate(R.layout.inflate_view, parent, false);
TextView txtView = (TextView) view.findViewById(R.id.inflate_txtView);
txtView.setText(frndArrList.get(position));
return view;
}
@Override
public Filter getFilter() {
if (friendFilter == null)
friendFilter = new FriendFilter();
return friendFilter;
}
private class FriendFilter extends Filter {
@Override
protected FilterResults performFiltering(CharSequence constraint) {
FilterResults results = new FilterResults();
if (constraint == null || constraint.length() == 0) {
// No filter implemented we return all the list
results.values = frndArrList;
results.count = frndArrList.size();
}
else {
// We perform filtering operation
ArrayList<String> nPlanetList = new ArrayList<String>();
for (String p : frndArrList) {
Log.v("String", p);
if (p.toLowerCase().contains(constraint.toString().toLowerCase()))
nPlanetList.add(p);
Log.e("nPlanetList", nPlanetList.toString());
}
results.values = nPlanetList;
results.count = nPlanetList.size();
}
return results;
}
@Override
protected void publishResults(CharSequence constraint,
FilterResults results)
{
frndArrList = (ArrayList<String>) results.values;
notifyDataSetChanged();
}
}
}
答案 0 :(得分:0)
在构造函数中,不要为这两个列表分配引用。
public FriendListAdapter(MainActivity context, ArrayList<String> frndArrList) {
this.context = context;
this.frndArrList = frndArrList;
this.filterArrList.addAll(frndArrList);
}
答案 1 :(得分:0)
您必须在适配器中为搜索操作创建两个不同的arraylist。 一个是原创的,另一个是临时的。 确保不要创建具有相同引用的列表。 您必须使用此临时列表来过滤税,并使用原始arraylist进行比较操作。
答案 2 :(得分:0)
由于您的publishResults
方法,这种情况正在发生。您将frndArrList指定为搜索结果,这意味着在输入搜索后,实际上删除了对原始数组列表中数据的所有引用。要解决此问题,您需要更改以下内容:
public int getCount() {
return filterArrList.size()
}
//...
public View getView(int position, View view, ViewGroup parent) {
LayoutInflater layoutInflater = LayoutInflater.from(context);
view = layoutInflater.inflate(R.layout.inflate_view, parent, false);
TextView txtView = (TextView) view.findViewById(R.id.inflate_txtView);
//txtView.setText(frndArrList.get(position));
txtView.setText(filterArrList.get(position));
return view;
}
//...
protected void publishResults(CharSequence constraint,
FilterResults results)
{
filterArrList= (ArrayList<String>) results.values;
notifyDataSetChanged();
}
}
答案 3 :(得分:0)
您可以使用textchanged侦听器在文本更改的editext调用上过滤新列表。
edit_searchbox.addTextChangedListener(new TextWatcher() {
public void onTextChanged(CharSequence s, int start,
int before, int count) {
// get the text in the EditText
String searchString = edit_searchbox.getText().toString();
int textLength = searchString.length();
// clear the initial data set
searchResults.clear();
String playerName="";
for (int i = 0; i < aa.size(); i++) {
if(isContent){
playerName = aa.get(i).getComment().toString();}
else{
playerName = aa.get(i).getTitle_txt().toString();}
if (textLength <= playerName.length()) {
// compare the String in EditText with Names in the
//aa--- ArrayList
if (searchString.equalsIgnoreCase(playerName
.substring(0, textLength)))
searchResults.add(aa.get(i));
}
}
adapter = new NotepadAdapter(MemoActivity.this,
R.layout.listitem, searchResults);
list.setAdapter(adapter);
adapter.notifyDataSetChanged();
}
public void beforeTextChanged(CharSequence s, int start,
int count, int after) {
}
public void afterTextChanged(Editable s) {
}
});