problem in AutoCompleteTextView

Put problem concerning Views, Layouts and other XML-Resources (like AndroidManifest) here.

problem in AutoCompleteTextView

Postby Nemat » Sat Dec 26, 2009 6:16 am

Hi Friends....

I am working on AutoCompleteTextView.Its working fine and doen't give any error.Now the problem I am facing is,suppose we have 3 words starting from "A",when I start typing the letter "A",It shows all the three words.If we have three words like "An","Am","Az",when I type "n" after "A",two other words should be removed from the list.and it should display only "An".But those two letters are not disappeared in my AutoCompleteTextView.What should I do???

Any one have any idea????

Thanks in Advance
Nemat
Nemat
Experienced Developer
Experienced Developer
 
Posts: 62
Joined: Thu Aug 21, 2008 3:16 pm
Location: Jaipur

Top

Postby glr » Sat Dec 26, 2009 8:25 pm

Perhaps some code to see if you missed something. Since it works the way you describe it by default.
glr
Freshman
Freshman
 
Posts: 9
Joined: Sat Nov 28, 2009 9:09 pm

Postby Nemat » Sun Dec 27, 2009 7:29 am

Ya, sure.Here is my code:

Code: Select all
String[] phonesProjection = new String[] {Contacts.People.Phones.NUMBER, Contacts.People.Phones.TYPE,Contacts.People.Phones.DISPLAY_NAME, Contacts.People.Phones._ID,Contacts.People.Phones.LABEL }; Cursor phonesCursor = content.query(Contacts.Phones.CONTENT_URI, phonesProjection, null, null, null); ContactListAdapter adapter = new ContactListAdapter(this, phonesCursor);
AutoCompleteTextView textView = (AutoCompleteTextView)
findViewById(R.id.edit);
textView.setAdapter(adapter);

public static class ContactListAdapter extends CursorAdapter implements Filterable {

    public ContactListAdapter(Context context, Cursor c) {     
        super(context, c);   

        mContent = context.getContentResolver();     
        }       
    @Override   
    public View newView(Context context, Cursor cursor, ViewGroup parent) {     
        final LayoutInflater inflater = LayoutInflater.from(context);       
        final TextView view = (TextView) inflater.inflate(             
                        android.R.layout.simple_dropdown_item_1line, parent, false);   
        Log.i("Cursor Count",""+cursor.getCount());
        view.setText(cursor.getString(2)+" "+cursor.getString(0));         
        return view;     
        }       
    @Override     
    public void bindView(View view, Context context, Cursor cursor) {   
        ((TextView) view).setText(cursor.getString(2)+" "+cursor.getString(0));       
        }       
    @Override     
    public String convertToString(Cursor cursor) {       

        return "";
        }     
    @Override   
    public Cursor runQueryOnBackgroundThread(CharSequence constraint) {     
        if (getFilterQueryProvider() != null) {     
                return getFilterQueryProvider().runQuery(constraint);       
                }           
        StringBuilder buffer = null;     
        String[] args = null;         
        if (constraint != null) {     
                buffer = new StringBuilder();     
                buffer.append("UPPER(");         
                buffer.append(Contacts.People.Phones.DISPLAY_NAME);     
                buffer.append(") GLOB ?");           
                args = new String[] { constraint.toString().toUpperCase() + "*" };   
                }           

        String[] phonesProjection = new String[] {Contacts.People.Phones.NUMBER, Contacts.People.Phones.TYPE,Contacts.People.Phones.DISPLAY_NAME,
                Contacts.People.Phones._ID,Contacts.People.Phones.LABEL         };
        Cursor phonesCursor = mContent.query(Contacts.Phones.CONTENT_URI, phonesProjection, null, null,Contacts.People.Phones.DEFAULT_SORT_ORDER);
        int phonesCursorCount = phonesCursor.getCount();
        System.out.println("phonesCursorCount="+phonesCursorCount);
        return phonesCursor;
        }     
    private ContentResolver mContent;   

Nemat
Experienced Developer
Experienced Developer
 
Posts: 62
Joined: Thu Aug 21, 2008 3:16 pm
Location: Jaipur

Postby glr » Sun Dec 27, 2009 10:32 am

I see. I have not used the ContactListAdapter before but if you take a look at this simple code.

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.         final String[] COLORS = { "black", "blue", "beige", "brown" };
  2.  
  3.        
  4.  
  5.         ArrayAdapter<String> adapter;
  6.  
  7.         AutoCompleteTextView text;
  8.  
  9.    
  10.  
  11.     @Override
  12.  
  13.     public void onCreate(Bundle savedInstanceState) {
  14.  
  15.         super.onCreate(savedInstanceState);
  16.  
  17.         setContentView(R.layout.main);
  18.  
  19.        
  20.  
  21.         adapter = new ArrayAdapter<String>(this, android.R.layout.simple_dropdown_item_1line, COLORS);
  22.  
  23.        
  24.  
  25.         text = (AutoCompleteTextView) findViewById(R.id.AutoCompleteTextView01);
  26.  
  27.         text.setAdapter(adapter);
  28.  
  29.     }
Parsed in 0.033 seconds, using GeSHi 1.0.8.4


Syntax: [ Download ] [ Hide ]
Using xml Syntax Highlighting
  1. <AutoCompleteTextView
  2.  
  3.         android:id="@+id/AutoCompleteTextView01"
  4.  
  5.         android:layout_width="fill_parent"
  6.  
  7.         android:layout_height="wrap_content"
  8.  
  9.         android:completionHint="Pick a color"
  10.  
  11.         android:completionThreshold="1"
  12.  
  13.         />
Parsed in 0.001 seconds, using GeSHi 1.0.8.4


You will find it to be working the way you want. The major difference is the use of an ArrayAdapter<String> instead.

Hope it helps a little.
glr
Freshman
Freshman
 
Posts: 9
Joined: Sat Nov 28, 2009 9:09 pm

Top

Return to View, Layout & Resource Problems

Who is online

Users browsing this forum: No registered users and 5 guests