我正在尝试创建一个过滤搜索(使用EditText)来搜索我的自定义ListView项目。我试图这样做,但似乎有一些错误,因为当我尝试搜索(输入EditText)时没有任何反应。
你能帮我吗?
提前致谢! :)
MainActivity.java
public class MainActivity extends ActionBarActivity{
private ListView mListView;
private AnimalAdapter mAdapter;
ProgressBar mProgressBar;
EditText mEditText;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Add your initialization code here
Parse.initialize(this, "code", "code");
ParseObject.registerSubclass(Animal.class);
ParseAnalytics.trackAppOpened(getIntent());
View header = getLayoutInflater().inflate(R.layout.header, null);
header.setPadding(2, 8, 4, 2);
mListView = (ListView) findViewById(R.id.animal_list);
mListView.setVisibility(View.INVISIBLE);
mListView.addHeaderView(header);
mProgressBar = (ProgressBar) findViewById (R.id.loading_animals);
mProgressBar.setVisibility(View.VISIBLE);
RemoteDataTask task = new RemoteDataTask();
task.execute();
mEditText = (EditText) findViewById(R.id.search_animal);
mEditText.addTextChangedListener(new TextWatcher(){
@Override
public void afterTextChanged(Editable arg0) {
// TODO Auto-generated method stub
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
// TODO Auto-generated method stub
}
@Override
public void onTextChanged(CharSequence s, int start, int before,
int count) {
System.out.println("Text ["+s+"] - Start ["+start+"] - Before ["+before+"] - Count ["+count+"]");
if (count < before) {
// We're deleting char so we need to reset the adapter data
mAdapter.resetData();
}
mAdapter.getFilter().filter(s.toString());
}});
}
public void updateData(){
ParseQuery<Animal> query = ParseQuery.getQuery(Animal.class);
query.setCachePolicy(CachePolicy.CACHE_THEN_NETWORK);
query.orderByAscending("animal");
query.findInBackground(new FindCallback<Animal>() {
@Override
public void done(List<Animal> animals, ParseException error) {
if(animals != null){
mAdapter.clear();
mProgressBar.setVisibility(View.INVISIBLE);
for (int i = 0; i < animals.size(); i++) {
mAdapter.add(animals.get(i));
}
}
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.action_refresh:
Intent refreshIntent = new Intent(MainActivity.this, MainActivity.class);
refreshIntent.setFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
refreshIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(refreshIntent);
overridePendingTransition(0,0);
return true;
}
return super.onOptionsItemSelected(item);
}
private class RemoteDataTask extends AsyncTask<Void, Void, Void> {
@Override
protected void onPreExecute() {
super.onPreExecute();
// Create a progressdialog
}
@Override
protected Void doInBackground(Void... params) {
updateData();
return null;
}
@Override
protected void onPostExecute(Void result) {
mListView = (ListView) findViewById(R.id.animal_list);
// Pass the results into ListViewAdapter.java
mAdapter = new AnimalAdapter(MainActivity.this, new ArrayList<Animal>());
mListView.setAdapter(mAdapter);
mListView.setVisibility(View.VISIBLE);
}
}
}
AnimalAdapter.java
public class AnimalAdapter extends ArrayAdapter<Animal> implements Filterable{
private Context mContext;
private List<Animal> mAnimals;
ImageLoader imageLoader;
ArrayAdapter<Animal> array;
DisplayImageOptions options;
Activity activity;
private Filter animalFilter;
@SuppressWarnings("deprecation")
public AnimalAdapter(Context context, List<Animal> objects) {
super(context, R.layout.animal_row_item, objects);
ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(context).build();
imageLoader = ImageLoader.getInstance();
imageLoader.init(config);
options = new DisplayImageOptions.Builder()
.cacheInMemory()
.cacheOnDisc()
.build();
this.mContext = context;
this.mAnimals = objects;
}
public View getView(int position, View convertView, ViewGroup parent){
if(convertView == null){
LayoutInflater mLayoutInflater = LayoutInflater.from(mContext);
convertView = mLayoutInflater.inflate(R.layout.animal_row_item, null);
}
final Animal animal = mAnimals.get(position);
TextView animalView = (TextView) convertView.findViewById(R.id.animal_text);
TextView areaView = (TextView) convertView.findViewById(R.id.area_text);
final ImageView animalPic = (ImageView)convertView.findViewById(R.id.animal_pic);
final ProgressBar indicator = (ProgressBar)convertView.findViewById(R.id.progress);
indicator.setVisibility(View.VISIBLE);
animalPic.setVisibility(View.INVISIBLE);
//Setup a listener we can use to switch from the loading indicator to the Image once it's ready
ImageLoadingListener listener = new ImageLoadingListener(){
@Override
public void onLoadingStarted(String arg0, View arg1) {
// TODO Auto-generated method stub
}
@Override
public void onLoadingCancelled(String arg0, View arg1) {
// TODO Auto-generated method stub
}
@Override
public void onLoadingComplete(String arg0, View arg1, Bitmap arg2) {
indicator.setVisibility(View.INVISIBLE);
animalPic.setVisibility(View.VISIBLE);
}
@Override
public void onLoadingFailed(String arg0, View view, FailReason arg2) {
}
};
imageLoader.displayImage(getItem(position).getImgUrl(), animalPic,options, listener);
animalView.setText(animal.getAnimal());
areaView.setText(animal.getArea());
convertView.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(getContext(), MoreActivity.class);
intent.putExtra("about", animal.getAbout());
intent.putExtra("animal", animal.getAnimal());
intent.putExtra("imgUrl", animal.getImgUrl());
getContext().startActivity(intent);
}
});
return convertView;
}
public void resetData() {
mAnimals = mAnimals;
}
@Override
public Filter getFilter() {
if (animalFilter == null)
animalFilter = new AnimalFilter();
return animalFilter;
}
私有类AnimalFilter扩展了Filter {
@Override
protected FilterResults performFiltering(CharSequence constraint) {
FilterResults results = new FilterResults();
// We implement here the filter logic
if (constraint == null || constraint.length() == 0) {
// No filter implemented we return all the list
results.values = mAnimals;
results.count = mAnimals.size();
}
else {
// We perform filtering operation
List<Animal> nPlanetList = new ArrayList<Animal>();
for (Animal a : mAnimals) {
if (a.getAnimal().toUpperCase().startsWith(constraint.toString().toUpperCase()))
nPlanetList.add(a);
}
results.values = mAnimals;
results.count = mAnimals.size();
}
return results;
}
@SuppressWarnings("unchecked")
@Override
protected void publishResults(CharSequence constraint,
FilterResults results) {
// Now we have to inform the adapter about the new list filtered
if (results.count == 0)
notifyDataSetInvalidated();
else {
mAnimals = (List<Animal>) results.values;
notifyDataSetChanged();
}
}
} }
Animal.java
@ParseClassName("Animal")
public class Animal扩展ParseObject {
public Animal(){
}
public String getAnimal(){
return getString("animal");
}
public void setAnimal(String animal){
put("animal", animal);
}
public String getArea(){
return getString("area");
}
public void setArea(String area){
put("area", area);
}
public String getImgUrl(){
return getString("imgUrl");
}
public void setImgUrl(String imgUrl){
put("imgUrl", imgUrl);
}
public String getAbout(){
return getString("about");
}
public void setAbout(String about){
put("about", about);
}
}
答案 0 :(得分:0)
解决了它。只需添加mListView.setTextFilterEnabled(true);