在Listview中搜索无法正常工作。如果我输入我的数组列表中的字符串,它会显示所有记录,但如果我在编辑文本字段中输入任何其他字符串,则会显示一个空列表。
public class GetCustomList extends ListActivity {
TweetListAdaptor myAdaptor;
ListView lv;
EditText searchTxt;
public ArrayList<Contact> tweets = new ArrayList<Contact>();
DatabaseHandler db = new DatabaseHandler(this);
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.customer_info);
tweets = db.getAllContacts();
lv = (ListView) findViewById(android.R.id.list);
lv.setTextFilterEnabled(true);
searchTxt = (EditText) findViewById(R.id.editTextSearchNameField);
searchTxt.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
// TODO Auto-generated method stub
myAdaptor.getFilter().filter(s.toString());
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
// TODO Auto-generated method stub
}
@Override
public void afterTextChanged(Editable s) {
// TODO Auto-generated method stub
}
});
Button btnSearch = (Button) findViewById(R.id.buttonSearch);
btnSearch.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
onPostExecute(null);
}
});
/**
* CRUD Operations
* */
// Inserting Contacts
Log.d("Insert: ", "Inserting ..");
db.addContact(new Contact("786","zahid", "bilaltown", '0'));
db.addContact(new Contact("123","Shahid", "bilaltown", '0'));
db.addContact(new Contact("123","waqas", "bilaltown", '0'));
// Reading all contacts
Log.d("Reading: ", "Reading all contacts..");
for (Contact cn : tweets) {
String log = "Id: "+cn.get_id()+ ",Code:"+ cn.get_code() +" ,Name: " + cn.get_name() + " ,Address: " + cn.get_address() + " ,Phone: " + cn.get_limit();
// Writing Contacts to log
Log.d("Name: ", log);
}
}
protected void onPostExecute(Void result) {
myAdaptor = new TweetListAdaptor(GetCustomList.this, tweets);
myAdaptor.notifyDataSetChanged();
setListAdapter(myAdaptor);
}
public class TweetListAdaptor extends ArrayAdapter<Contact> implements Filterable {
private ArrayList<Contact> mOriginalValues = new ArrayList<Contact>(); // Original Values
private ArrayList<Contact> mDisplayedValues = new ArrayList<Contact>(); // Values to be displayed
LayoutInflater inflater;
Context context;
public TweetListAdaptor(Context context,ArrayList<Contact> items) {
super(context, R.layout.custom_list_info, items);
this.mOriginalValues = items;
this.mDisplayedValues = items;
this.context = context;
// inflater = LayoutInflater.from(context);
}
@Override
public int getCount() {
return mDisplayedValues.size();
}
@Override
public long getItemId(int position) {
return position;
}
public class ViewHolder {
private TextView CustomerId;
private TextView CustomerShop;
private TextView date;
private TextView phoneNumber;
public ViewHolder(View v) {
this.CustomerId = (TextView) v.findViewById(R.id.textViewCustomerID);
this.CustomerShop = (TextView) v.findViewById(R.id.textViewShopName);
// this.date = (TextView) v.findViewById(R.id.date);
// this.status = (TextView) v.findViewById(R.id.staus);
}
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
View v = convertView;
if (v == null) {
LayoutInflater vi = (LayoutInflater)
getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = vi.inflate(R.layout.custom_list_info, null);
holder = new ViewHolder(v);
v.setTag(holder);
}else {
holder = (ViewHolder) v.getTag();
}
final Contact o = getItem(position);
if (o != null) {
holder.CustomerId.setText(""+o._id);
holder.CustomerShop.setText(o._name);
//holder.date.setText(o.date);
//holder.status.setText(o.status_txt);
/*if(o.status.equals("1")){
RelativeLayout llr = (RelativeLayout) v.findViewById(R.id.testRes);
llr.setBackgroundResource(R.drawable.decline);
}else if(o.status.equals("2")){
RelativeLayout llr = (RelativeLayout) v.findViewById(R.id.testRes);
llr.setBackgroundResource(R.drawable.acceptence);
}
}*/
ImageButton btnSearchCustomer = (ImageButton) v.findViewById(R.id.btnSearchCustomer);
btnSearchCustomer.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Intent intent = new Intent(GetCustomList.this, CustomerDetailedInfo.class);
intent.putExtra("ccode", o._code);
intent.putExtra("cname", o._name);
intent.putExtra("caddress", o._address);
intent.putExtra("climit", o._limit);
startActivity(intent);
}
});
}
return v;
}
///////////////////////////////
@Override
public Filter getFilter() {
Filter filter = new Filter() {
@SuppressWarnings("unchecked")
@Override
protected void publishResults(CharSequence constraint,FilterResults results) {
mDisplayedValues = (ArrayList<Contact>) results.values; // has the filtered values
notifyDataSetChanged(); // notifies the data with new filtered values
}
@Override
protected FilterResults performFiltering(CharSequence constraint) {
FilterResults results = new FilterResults(); // Holds the results of a filtering operation in values
ArrayList<Contact> FilteredArrList = new ArrayList<Contact>();
if (mOriginalValues == null) {
mOriginalValues = new ArrayList<Contact>(mDisplayedValues); // saves the original data in mOriginalValues
}
/********
*
* If constraint(CharSequence that is received) is null returns the mOriginalValues(Original) values
* else does the Filtering and returns FilteredArrList(Filtered)
*
********/
if (constraint == null || constraint.length() == 0) {
// set the Original result to return
results.count = mOriginalValues.size();
results.values = mOriginalValues;
} else {
constraint = constraint.toString().toLowerCase();
for (int i = 0; i < mOriginalValues.size(); i++) {
String data = mOriginalValues.get(i)._name;
if (data.toLowerCase().startsWith(constraint.toString())) {
FilteredArrList.add(new Contact(mOriginalValues.get(i)._name,mOriginalValues.get(i)._code,mOriginalValues.get(i)._address,mOriginalValues.get(i)._limit));
}
}
// set the Filtered result to return
results.count = FilteredArrList.size();
results.values = FilteredArrList;
}
return results;
}
};
return filter;
}
///////////////////
}
联系班级
public class Contact {
//private variables
int _id;
String _code;
String _name;
String _address;
int _limit;
// Empty constructor
public Contact(){
}
// constructor
public Contact(int id, String _code,String name,String address, int _limit){
this._id = id;
this._code = _code;
this._name = name;
this._address = address;
this._limit = _limit;
}
// constructor
public Contact(String _code,String name, String address,int _limit){
this._code = _code;
this._name = name;
this._address = address;
this._limit = _limit;
}
public int get_id() {
return _id;
}
public void set_id(int _id) {
this._id = _id;
}
public String get_code() {
return _code;
}
public void set_code(String _code) {
this._code = _code;
}
public String get_name() {
return _name;
}
public void set_name(String _name) {
this._name = _name;
}
public String get_address() {
return _address;
}
public void set_address(String _address) {
this._address = _address;
}
public int get_limit() {
return _limit;
}
public void set_limit(int _limit) {
this._limit = _limit;
}
}
答案 0 :(得分:0)
代码中有一些小错误。首先,您需要覆盖getItem()
中的TweetListAdaptor
方法:
@Override
public Contact getItem(int position)
{
return mDisplayedValues.get(position);
}
然后,在Filter
的{{1}}方法中,performFiltering()
和code
参数将在以下name
构造函数中切换行:
Contact
我用20个名字进行了测试,我相信这些更正应该可以解决这个问题。