文字列や画像 URL などのさまざまなアイテムを保持するモデル クラスがあります。
これにフィルターを実装しましたが、残念ながら、検索するたびにすべてのアイテムが消えてしまいます。フィルタの実装方法を教えてください。
これがソースコードです。
package Dummy.list.filter;
import android.app.Activity;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.widget.EditText;
import android.widget.ListView;
public class DummyListFilterActivity extends Activity {
/** Called when the activity is first created. */
myadapter adapter;
EditText filterText;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ModelClass[] str = new ModelClass[]{new ModelClass("cell",R.drawable.ic_launcher,1),
new ModelClass("Bus",R.drawable.ic_launcher,2),
new ModelClass("Car",R.drawable.ic_launcher,3),
new ModelClass("Van",R.drawable.ic_launcher,4),
new ModelClass("Lorry",R.drawable.ic_launcher,5)};
ListView list = (ListView)findViewById(R.id.Service_name_list);
filterText = (EditText)findViewById(R.id.txtUserName);
filterText.addTextChangedListener(filterTextWatcher);
list.setTextFilterEnabled(true);
adapter = new myadapter(this, 0, str);
//ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,str);
list.setAdapter(adapter);
}
private TextWatcher filterTextWatcher = new TextWatcher() {
public void afterTextChanged(Editable s) {
}
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
}
public void onTextChanged(CharSequence s, int start, int before,
int count) {
adapter.getFilter().filter(s.toString());
}
};
@Override
protected void onDestroy() {
super.onDestroy();
filterText.removeTextChangedListener(filterTextWatcher);
}
}
これが私の Adapter クラスの外観です
package Dummy.list.filter;
import java.util.List;
import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;
public class myadapter extends ArrayAdapter<ModelClass>
{
//private final List<String> contactNumbers;
//private final String[] contactDetails;
private final ModelClass[] contactDetails;
private final Activity AdapterContext;
public myadapter(Context context, int textViewResourceId,ModelClass[] objects)
{
super(context, textViewResourceId, objects);
// TODO Auto-generated constructor stub
contactDetails = objects;
AdapterContext = (Activity) context;
}
static class ViewHolder {
protected TextView ContactNumbers;
protected ImageView ContactImage;
}
public View getView(int position, View convertView, ViewGroup parent)
{
View view = null;
if(convertView == null)
{
LayoutInflater inflator = AdapterContext.getLayoutInflater();
view = inflator.inflate(R.layout.item, null);
final ViewHolder holder = new ViewHolder();
holder.ContactImage = (ImageView) view.findViewById(R.id.image);
holder.ContactNumbers = (TextView) view.findViewById(R.id.text);
view.setTag(holder);
}
else
{
view = convertView;
}
ViewHolder holder = (ViewHolder) view.getTag();
holder.ContactNumbers.setText(contactDetails[position].getServiceName());
holder.ContactImage.setImageResource(contactDetails[position].getServiceImage());
view.setTag(holder);
return view;
}
}