我有一个使用 BaseAdapter 的自定义 listView。在其中显示电话簿联系人。在此 listView 中,两个文本框相应地显示名称和数字。现在我正在尝试在 listView 的搜索框顶部添加功能但我遇到了一些问题我不知道当我在 Eclipse 中看到我的 logcat 窗口时会发生什么然后我可以清楚地看到过滤后的列表但设备 listView 不是再改变。请按照我的代码如下:
package com.leaning.him;
public class ForHimActivity extends Activity {
private ListView contactList = null;
private LayoutInflater mInflater;
private String phoneNo;
private String contact;
private ContentResolver cr;
private Cursor cursor;
public ArrayList<String> aarr = new ArrayList<String>();
public ArrayList<String> aarr2 = new ArrayList<String>();
private static String ClickedNum;
private static boolean check;
private DBAdapter db;
CustomkListAdapter adapter;
EditText searchBar;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
searchBar = (EditText)findViewById(R.id.serchEditBox);
aarr.clear();
aarr2.clear();
adapter = new CustomkListAdapter(ForHimActivity.this);
contactList = (ListView)findViewById(R.id.facebookList);
contactList.setAdapter(adapter);
TextWatcher searchTextWatcher = new TextWatcher() {
public void onTextChanged(CharSequence s, int arg1, int arg2, int arg3) {
Log.v("","This is what you entred : "+s.toString());
adapter.getFilter().filter(s);
adapter.notifyDataSetChanged();
contactList.setAdapter(adapter);
}
public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
int arg3) {
// TODO Auto-generated method stub
}
public void afterTextChanged(Editable arg0) {
// TODO Auto-generated method stub
}
};
searchBar.addTextChangedListener(searchTextWatcher);
}
public class CustomkListAdapter extends BaseAdapter {
ArrayList<String> arrayList;
ArrayList<String> originallist;
public CustomkListAdapter(Context context){
// Log.v("","This is context of BaseAdapter Class : "+context);
mInflater = LayoutInflater.from(context);
arrayList = new ArrayList<String>();
originallist = new ArrayList<String>();
originallist = aarr;
cr = context.getContentResolver();
cursor = cr.query(
ContactsContract.Contacts.CONTENT_URI,
null,
ContactsContract.Contacts.IN_VISIBLE_GROUP + "=1",
null,
null);
db = new DBAdapter(context);
}
public int getCount()
{
return cursor.getCount();
// return 5;
}
public Object getItem(int position)
{
return aarr.get(position);
}
public long getItemId(int position)
{
return position;
}
public void getContacts(){
while(cursor.moveToNext()){
int nameFiledColumnIndex = cursor.getColumnIndex(
ContactsContract.Contacts.DISPLAY_NAME);
String id = cursor.getString(
cursor.getColumnIndex(ContactsContract.Contacts._ID));
Cursor pCur = cr.query(
ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
null,
ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = ?",
new String[]{id}, null);
while (pCur.moveToNext()) {
phoneNo = pCur.getString(pCur.getColumnIndex(
ContactsContract.CommonDataKinds.Phone.NUMBER));
}//end inner while
contact = cursor.getString(nameFiledColumnIndex);
aarr.add(contact);
aarr2.add(phoneNo);
//Log.v("","Checkd this : "+aarr);
}
}
public View getView(final int position, View convertView, ViewGroup parentView)
{
int i = 0;
ViewHolder holder = null;
if(convertView == null)
{
// Log.v("","position : "+position);
}//end if
else
{
holder = (ViewHolder) convertView.getTag();
}//end else
holder = new ViewHolder();
convertView = mInflater.inflate(R.layout.twitter_row, null);
holder.txtName = (TextView) convertView.findViewById(R.id.txtName);
holder.txtnumber = (TextView) convertView.findViewById(R.id.txtnumber);
holder.chkBox = (CheckBox) convertView.findViewById(R.id.chkAdd);
if(cursor != null){
getContacts();
db.open();
Cursor c = db.getAllData();
if(c.moveToFirst()){
do {
if(aarr2.get(position).replace("-", "").equals(c.getString(2))){
holder.chkBox.setChecked(true);
}
} while (c.moveToNext());
}
db.close();
c.close();
}else
{
//Log.v("","Not Found any contacts....");
}
holder.txtName.setText(aarr.get(position));
holder.txtnumber.setText(aarr2.get(position));
// Log.v("","Now heck : "+aarr.get(i));
i++;
if(position == 1){
for(int j=0;j<100;j++){
//
// Log.v("","Contacts :"+aarr.get(i));
}
}
holder.chkBox.setOnCheckedChangeListener(new OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton buton, boolean itemChked) {
check = itemChked;
ClickedNum = aarr2.get(position).replace("-", "");
if(check == true){
//Log.v("","This will save : "+ClickedNum.replace("-", ""));
db.open();
db.insert(ClickedNum, check);
db.close();
}else if (check == false){
db.open();
db.deleteFromNum(ClickedNum);
db.close();
}
}
});
cursor.close();
// Log.v("","Latest getView() : "+aarr.get(position));
return convertView;
}
public class ViewHolder
{
public TextView txtName, txtnumber ;
CheckBox chkBox;
Button btn;
}
public Filter getFilter() {
Filter filter = new Filter(){
@Override
protected void publishResults(CharSequence constraint, FilterResults results) {
Log.v("","from pubishResult()");
arrayList = (ArrayList<String>) results.values;
Log.v("","This is array lsit from publishResults() : "+arrayList);
notifyDataSetChanged();
}
@Override
protected FilterResults performFiltering(CharSequence constraint) {
Log.v("","from performFiltering()");
FilterResults results = new FilterResults();
List<String> FilteredArrList = new ArrayList<String>();
if (originallist == null ) {
originallist = new ArrayList<String>(arrayList);
}
if (constraint == null || constraint.length() == 0) {
results.count = originallist.size();
results.values = originallist;
}
else{
constraint = constraint.toString();
for (int i = 0; i < originallist.size(); i++) {
String data = originallist.get(i);
if (data.toLowerCase().startsWith(constraint.toString())) {
FilteredArrList.add(data);
}
}
results.count = FilteredArrList.size();
results.values = FilteredArrList;
}
Log.v("","This is result : "+results);
return results;
}
};
Log.v("","This is filter : "+filter.toString());
return filter;
}
}
}
当我从edittex按“A”时,以下是我的日志catwindow::
10-18 12:57:41.273: I/InputDispatcher(1483): Delivering touch to current input target: action: 1, channel '407b51d8 com.android.settings/com.android.settings.WirelessSettings (server)'
10-18 12:57:41.406: I/ActivityManager(1483): Starting: Intent { act=android.intent.action.MAIN cmp=com.android.settings/.TetherSettings } from pid 6709
10-18 12:57:41.414: W/ActivityManager(1483): Trying to launch com.android.settings/.TetherSettings
10-18 12:57:41.429: I/ApplicationPackageManager(6709): cscCountry is not German : INU
10-18 12:57:41.445: I/webclipboard(6709): clipservice: android.sec.clipboard.ClipboardExManager@40571208
10-18 12:57:41.546: D/WML_SISO(6709): InitPasteboardJni
10-18 12:57:41.671: D/dalvikvm(6709): GC_CONCURRENT freed 109K, 45% free 3116K/5639K, external 647K/1031K, paused 3ms+4ms
10-18 12:57:41.695: I/ActivityManager(1483): Displayed com.android.settings/.TetherSettings: +282ms
10-18 12:57:41.921: E/StatusBarPolicy(1535): ecio: 31
10-18 12:57:41.921: E/StatusBarPolicy(1535): iconLevel: 4
10-18 12:57:42.187: I/InputReader(1483): dispatchTouch::touch event's action is 0
10-18 12:57:42.187: I/InputDispatcher(1483): Delivering touch to current input target: action: 0, channel '407cbfc0 com.android.settings/com.android.settings.TetherSettings (server)'
10-18 12:57:42.203: I/InputReader(1483): dispatchTouch::touch event's action is 1
10-18 12:57:42.203: I/InputDispatcher(1483): Delivering touch to current input target: action: 1, channel '407cbfc0 com.android.settings/com.android.settings.TetherSettings (server)'
10-18 12:57:42.335: D/Tethering(1483): Tethering usb0
10-18 12:57:42.335: D/Tethering(1483): [ActiveSync] internet sharing is NOT blocked
请指导我哪里出错............我希望我上面描述的内容足以理解我的问题。