2010-01-29 13 views
10

बनाने मैं एक AutoCompleteTextViewAutoCompleteTextView प्रदर्शित करता है 'android.database.sqlite.SQLiteCursor @' ... चयन

mComment = (AutoCompleteTextView) findViewById(R.id.comment); 

    Cursor cComments = myAdapter.getDistinctComments(); 
    scaComments = new SimpleCursorAdapter(this,R.layout.auto_complete_item,cComments,new String[] {DBAdapter.KEY_LOG_COMMENT},new int[]{R.id.text1}); 

    mComment.setAdapter(scaComments); 

auto_complete_item.xml

के लिए एडाप्टर (SimpleCursorAdapter) स्थापित करने के लिए निम्न कोड का उपयोग कर रहा करने के बाद
<?xml version="1.0" encoding="utf-8"?> 
<TextView 
xmlns:android="http://schemas.android.com/apk/res/android" 
android:id="@+id/text1" 
android:layout_width="wrap_content" 
android:layout_height="wrap_content"/> 

और थी वास्तविक नियंत्रण

<AutoCompleteTextView 
         android:id="@+id/comment" 
         android:hint="@string/COMMENT" 
         android:layout_width="fill_parent" 
         android:layout_height="wrap_content" 
         android:textSize="18dp"/> 

घ के लिए एक्सएमएल है रोपडाउन सही तरीके से काम करता प्रतीत होता है, और वस्तुओं की एक सूची दिखाता है। जब मैं सूची से चयन करता हूं तो मुझे टेक्स्टव्यू में एक स्क्लाइट ऑब्जेक्ट ('android.database.sqlite.SQLiteCursor @' ...) मिलता है। कोई भी जानता है कि इसका कारण क्या होगा, या इसे कैसे हल करें?

धन्यवाद

ठीक है मैं OnItemClick घटना में हुक करने में सक्षम हूँ, लेकिन AutoCompleteTextView विजेट के TextView.setText() भाग इस बिंदु के बाद अद्यतन किया जाता है। OnItemSelected() ईवेंट कभी नहीं निकाल दिया जाता है, और डाउनडाउन आइटम पहली बार प्रदर्शित होने पर कुछ भी नहीं चुना जाता है() ईवेंट निकाल दिया जाता है।

 mComment.setOnItemClickListener(new OnItemClickListener() { 

     @Override 
     public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, 
       long arg3) { 
      // TODO Auto-generated method stub 

      SimpleCursorAdapter sca = (SimpleCursorAdapter) arg0.getAdapter(); 


      String str = getSpinnerSelectedValue(sca,arg2,"comment"); 

      TextView txt = (TextView) arg1; 
      txt.setText(str); 
      Toast.makeText(ctx, "onItemClick", Toast.LENGTH_SHORT).show(); 

     } 

    }); 
    mComment.setOnItemSelectedListener(new OnItemSelectedListener() { 

     @Override 
     public void onItemSelected(AdapterView<?> arg0, View arg1, 
       int arg2, long arg3) { 


      Toast.makeText(ctx, "onItemSelected", Toast.LENGTH_SHORT).show(); 

     } 

     @Override 
     public void onNothingSelected(AdapterView<?> arg0) { 
      // TODO Auto-generated method stub 
      Toast.makeText(ctx, "onNothingSelected", Toast.LENGTH_SHORT).show(); 
     } 

    }); 

किसी और के पास टेक्स्टव्यू के अद्यतन को ओवरराइड करने के बारे में कोई विचार है?

धन्यवाद

पैट्रिक

उत्तर

0

जब मैं सूची से एक चयन करें मैं ('android.database.sqlite.SQLiteCursor @' ... ) एक SQLite वस्तु मिल TextView में ।

आप यह नहीं कहते कि यह "टेक्स्टव्यू" क्या है या यह Spinner से कैसे संबंधित है।

मैं एक शिक्षित अनुमान लगाने जा रहा हूं और मानता हूं कि आप से चयनित आइटम को TextView में निर्दिष्ट कर रहे हैं।

SimpleCursorAdapter का उपयोग कर Spinner से चयनित आइटम Cursor है, जो उपयोगकर्ता द्वारा चुनी गई पंक्ति पर इंगित करता है। Cursor का टॉस्ट्रिंग() कार्यान्वयन आपको के समान कुछ देगा जो Cursor कहां से आया था।

अधिक संभावना है कि आप कुछ कॉलम मान पुनर्प्राप्त करने के लिए Cursor पर getString() पर कॉल करना चाहते हैं, और इसे TextView पर प्रश्न में असाइन करें।

+0

हाँ, आप सही हैं:

गतिविधि के अंदर मेरे विस्तारित वर्ग के रूप में लग रहा है। मैं बस स्पिनर से विकल्पों में से एक का चयन कर रहा हूं जो AutoCompleteTextView के हिस्से के रूप में बनाया गया है। चयन को संसाधित करने के लिए मैं कहां या कैसे कार्यशीलता को रोकता हूं; कंट्रोल पर getString() का उपयोग करें, इसे नियंत्रण के पाठ के रूप में सेट करने से पहले। मैं भी अपना एक्सएमएल जोड़ूंगा। – bugzy

+0

"चयन को संसाधित करने के लिए मैं कहां या कैसे कार्यशीलता को रोकता हूं" - आप पहले से ही ऐसा कर रहे हैं। कहीं, आप टेक्स्ट व्यू पर 'setText()' को कॉल कर रहे हैं। उस कोड को संपादित करें। – CommonsWare

+0

यह बात है ... मैं नहीं। यह AutoCompleteTextView विजेट का अंतर्निहित व्यवहार है जो AutoCompleteTextView विजेट के TextView भाग को पॉप्युलेट कर रहा है। – bugzy

9

मुझे नहीं लगता कि आपको AutoCompleteTextView के लिए टेक्स्ट को अपडेट करना होगा। इसे स्वचालित रूप से करना चाहिए। यह [CursorAdapter.convertToString (...)] [1] विधि को कॉल करके करता है। यदि आप विधि के विवरण को पढ़ते हैं तो यह इंगित करता है। इसलिए यदि आप अपना स्वयं का कर्सर एडाप्टर लिख रहे थे तो आप सुझावों की सूची में जो भी टेक्स्ट दिखाना चाहते हैं उसे वापस करने के लिए उस विधि को ओवरराइड करेंगे।यह आदमी इसे कैसे करना समझाने का एक अच्छा काम करता है:

लाइन 86 - http://thinkandroid.wordpress.com/2010/02/08/writing-your-own-autocompletetextview/

हालांकि, बाद से आप एक SimpleCursorAdapter उपयोग कर रहे हैं, तो आप इस विधि ओवरराइड नहीं कर सकते। इसके बजाय आप को लागू करने की जरूरत/बनाने एक [SimpleCursorAdapter.CursorToStringConverter] [2] और यह [SimpleCursorAdapter.setCursorToStringConverter (...)] में पारित [3]:

SimpleCursorAdapter adapter = new SimpleCursorAdapter(context, layout, cursor, from, to); 
CursorToStringConverter converter = new CursorToStringConverter() { 

    @Override 
    public CharSequence convertToString(Cursor cursor) { 
     int desiredColumn = 1; 
     return cursor.getString(desiredColumn); 
    } 
}; 

adapter.setCursorToStringConverter(converter); 

या आप एक बनाने के लिए नहीं करना चाहते हैं कर्सर टॉस्ट्रिंग कनवर्टर फिर [SimpleCursorAdapter का उपयोग करें। setStringConversion कॉलम (...)] [4] विधि। लेकिन मुझे लगता है आप अभी भी स्पष्ट रूप से अशक्त करने के लिए CursorToStringConverter सेट करने के लिए:

int desiredColumn = 1; 
adapter.setCursorToStringConverter(null); 
adapter.setStringConversionColumn(desiredColumn); 

क्षमा करें, लेकिन स्पैम अवरोधक मुझे एंड्रॉयड प्रलेखन कि लिंक मैंने ऊपर पोस्ट का वर्णन करने के लिए लिंक पोस्ट नहीं दूँगा। लेकिन एक त्वरित Google खोज आपको सही दस्तावेज़ पृष्ठों पर इंगित करेगी।

3

[देर से जवाब, केवल रिकॉर्ड के लिए। । CursorToStringConverter, और FilterQueryProvider: मेरे सुझाव को दूर करने कि उपवर्गीकरण आवश्यक है संपादित]

एक AutoCompleteTextView साथ SimpleCursorAdapter का उपयोग करने के लिए आपको एडाप्टर पर दो संचालकों सेट करना होगा। स्यूडोकोड इस प्रकार है:

adapter.setCursorToStringConverter(new CursorToStringConverter() { 
     public String convertToString(android.database.Cursor cursor) { 
      // Assume that "someColumn" contains the strings that we want to 
      // use to identify rows in the result set. 
      final int columnIndex = cursor.getColumnIndexOrThrow("someColumn"); 
      final String str = cursor.getString(columnIndex); 
      return str; 
     } 
    }); 

    adapter.setFilterQueryProvider(new FilterQueryProvider() { 
     public Cursor runQuery(CharSequence constraint) { 
      // runSomeQuery will look for all rows in the database 
      // that match the given constraint. 
      Cursor cursor = runSomeQuery(constraint); 
      return cursor; 
     } 
    }); 
0

समस्या मैं सिर्फ SimpleCursorAdapter बढ़ाया और विधि convertToString() कार्यान्वित हल करने के लिए। फिर मैंने एक उदाहरण बनाया और इसे एडाप्टर के रूप में सेट किया।

CursorAdapters I का उपयोग करते समय AutoCompleteTextView में फ़िल्टरिंग की अनुमति देने के लिए मैंने setFilterQueryProvider() का भी उपयोग किया। See this question

private static class AutoCompleteCursorAdapter extends SimpleCursorAdapter { 

    public AutoCompleteCursorAdapter(Context context, int layout, Cursor c, String[] from, int[] to) { 
     super(context, layout, c, from, to); 
    } 

    @Override 
    public CharSequence convertToString(Cursor cursor) { 
     // This is the method that does the trick (return the String you need) 
     return cursor.getString(cursor.getColumnIndex("name")); 
    } 
} 
संबंधित मुद्दे