Please help me implementing an OnItemSelectedListener

Basic Tutorials concerning: GUI, Views, Activites, XML, Layouts, Intents, ...

Please help me implementing an OnItemSelectedListener

Postby waynesilver » Sat Feb 06, 2010 11:19 pm

I have an app that read in some records from a CSV, parsed the input into fields and placed those fields into an SQLite DB. When I am using the data, I want to use an auto-completing list, because it solves some sticky navigation problems.

I am trying to trap the event OnItemSelected so I can grab the value of the item selected do a db look-up and populate the rest of the fields. I am only getting the first character of the selection.

1) Where is the selected string stored?
2) How do I access the whole string, not just the first character?
3) Is there a better way to implement the listeners? (More flexible)

I have defined my list as:

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. public class SiteListV4 extends Activity
  2.  
  3.         implements AdapterView.OnItemSelectedListener{
  4.  
  5.        
  6.  
  7.         EditText siteid=null;
  8.  
  9.         TextView directions=null;
  10.  
  11.         TextView keysloc=null;
  12.  
  13.         TextView sitename=null;
  14.  
  15.         AtomicBoolean isActive=new AtomicBoolean(true);
  16.  
  17.         SQLiteDatabase sitedb=null;
  18.  
  19.         Cursor model=null;
  20.  
  21.         String[] sitelistArray=null;
  22.  
  23.     AutoCompleteTextView textView = null;
  24.  
  25.  
  26.  
  27.        
  28.  
  29.     /** Called when the activity is first created. */
  30.  
  31.         @Override
  32.  
  33.         protected void onCreate(Bundle savedInstanceState) {
  34.  
  35.             super.onCreate(savedInstanceState);
  36.  
  37.             setContentView(R.layout.main);
  38.  
  39.  
  40.  
  41.                 sitedb=(new SiteListSQLiteHelper(this)).getReadableDatabase();
  42.  
  43.  
  44.  
  45.                 model=Site.getAll(sitedb);
  46.  
  47.                 startManagingCursor(model);
  48.  
  49.                
  50.  
  51.            
  52.  
  53.             sitelistArray=loadsiteArray(model);
  54.  
  55.  
  56.  
  57.  
  58.  
  59.             textView = (AutoCompleteTextView) findViewById(R.id.autocomplete_site);
  60.  
  61.  
  62.  
  63.             ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.site_list, sitelistArray);
  64.  
  65.             textView.setAdapter(adapter);
  66.  
  67.             textView.setOnClickListener(onClick);
  68.  
  69.             textView.setOnItemSelectedListener(this);
  70.  
  71.         }
Parsed in 0.034 seconds, using GeSHi 1.0.8.4



I define a listener that is activated on the event:

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.         public void onItemSelected(AdapterView<?> parent, View v, int position, long id) {
  2.  
  3.                 String s=null;
  4.  
  5.                 EditText et=null;
  6.  
  7.                
  8.  
  9.                 android.util.Log.v("ONSELECT", "Item Selected (TRAPPED)");
  10.  
  11.                 et=(EditText)findViewById(R.id.autocomplete_site);
  12.  
  13.                 s=et.getText().toString();
  14.  
  15.                 android.util.Log.v("ONSELECT","Value = " + s);
  16.  
  17.                 android.util.Log.v("ONSELECT","Value = " + position);
  18.  
  19.                
  20.  
  21.         };
  22.  
  23.  
  24.  
  25.         public void onNothingSelected(AdapterView<?> parent) {
  26.  
  27.                
  28.  
  29.         }
  30.  
  31.  
Parsed in 0.033 seconds, using GeSHi 1.0.8.4


Right now, string s, returns the first character in the field selected. It happens to be the threshold I set in the layout.

Syntax: [ Download ] [ Hide ]
Using xml Syntax Highlighting
  1. <?xml version="1.0" encoding="utf-8"?>
  2.  
  3. <TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
  4.  
  5.    android:layout_width="fill_parent"
  6.  
  7.    android:layout_height="fill_parent"
  8.  
  9.    android:stretchColumns="1"
  10.  
  11.    >
  12.  
  13.     <TableRow>
  14.  
  15.      <TextView
  16.  
  17.        android:layout_width="wrap_content"
  18.  
  19.        android:layout_height="wrap_content"
  20.  
  21.        android:text="Site: "
  22.  
  23.        />
  24.  
  25.     <AutoCompleteTextView
  26.  
  27.         android:id="@+id/autocomplete_site"
  28.  
  29.        android:layout_width="fill_parent"
  30.  
  31.        android:layout_height="wrap_content"
  32.  
  33.        android:layout_marginLeft="5dp"
  34.  
  35.        android:completionThreshold="1"
  36.  
  37.        />
  38.  
  39.     </TableRow>
  40.  
  41.         <TableRow>
  42.  
  43.                 <TextView android:text="Keys loc: " />
  44.  
  45.                 <EditText android:id="@+id/mainkeys" />
  46.  
  47.         </TableRow>
  48.  
  49. </TableLayout>
  50.  
  51.  
Parsed in 0.004 seconds, using GeSHi 1.0.8.4
waynesilver
Developer
Developer
 
Posts: 28
Joined: Mon Jan 04, 2010 9:01 pm
Location: Seattle

Top

Return to Novice Tutorials

Who is online

Users browsing this forum: No registered users and 8 guests