BaseAdapter を使用したカスタム listView があります。どの電話帳の連絡先が表示されるか。この listView では、2 つのテキスト ボックスに名前と番号が表示されます。今、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
私が間違いを犯したところを教えてください.........私の問題を理解するのに、上記の説明で十分だと思います.