Note that there are some explanatory texts on larger screens.

plurals
  1. PODisplay primary key in the SQlite database when selecting item in Spinner
    primarykey
    data
    text
    <p>I am trying to display the primary key of the item I selected in Spinner.I want to display the primary key in TextView.How will I do this?I already know how to display a field in a table in database.</p> <p>In my DatabseHandler.java This is how I insert data in my table criteria</p> <pre><code>public long insertLabelCriteria(String label, String label2, String label3){ SQLiteDatabase db = this.getWritableDatabase(); ContentValues values = new ContentValues(); values.put(KEY_CRI_NAME, label); values.put(KEY_CRI_PER, label2); values.put(KEY_CRI_EVPK, label3); // Inserting Row long id = db.insert(TABLE_CRITERIA, null, values); db.close(); // Closing database connection return id; } </code></pre> <p>This is my method in getting labels and returning list of labels</p> <pre><code>public List&lt;Criteria&gt; getAllLabels( String evpk ){ List&lt;Criteria&gt; labels = new ArrayList&lt;Criteria&gt;(); SQLiteDatabase db = this.getReadableDatabase(); String selectQuery = "SELECT * FROM " + TABLE_CRITERIA + " WHERE " + KEY_CRI_EVPK + " = " + evpk ; Cursor cursor = db.rawQuery(selectQuery, null); // looping through all rows and adding to list if (cursor.moveToFirst()) { do { labels.add(new Criteria(cursor.getString(1))); } while (cursor.moveToNext()); } // closing connection cursor.close(); db.close(); // returning lables return labels; </code></pre> <p>}</p> <p>In my MainActivity</p> <p>I have a method loadSpinnerData and I use this evrytime I add a criteria, it will load the Spinner to view the item I added in database</p> <pre><code>private void loadSpinnerData() { // TODO Auto-generated method stub // database handler DatabaseHandler db = new DatabaseHandler(getApplicationContext()); // Spinner Drop down elements List&lt;Criteria&gt; lables = db.getAllLabels(evpk.getText().toString()); // Creating adapter for spinner ArrayAdapter&lt;Criteria&gt; dataAdapter = new ArrayAdapter&lt;Criteria&gt; (this, android.R.layout.simple_spinner_dropdown_item, lables); // Drop down layout style - list view with radio button dataAdapter .setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); // attaching data adapter to spinner criteria_spin.setAdapter(dataAdapter); criteria_spin.setOnItemSelectedListener(this); } </code></pre> <p>Now, on selecting item, how can I display the primary key of the selected item in spinner? This codes below are just to show and select the item click.</p> <pre><code> @Override public void onItemSelected(AdapterView&lt;?&gt; parent, View view, int position, long id) { // On selecting a spinner item String label = parent.getItemAtPosition(position).toString(); // Showing selected spinner item Toast.makeText(parent.getContext(), "You selected: " + label, Toast.LENGTH_LONG).show(); } </code></pre> <p>I also try to add this code ,but the id display is arraylist number, not the primary key</p> <pre><code> long rowId = id; String criteriapk = String.valueOf(label); cripk.setText(criteriapk); </code></pre> <p>I find it hard, in finding solution with this.What will I do?Help me plss</p> <p>I also try this method, but the value is cursor.in the TextView, it does not display number.</p> <pre><code>public Cursor find_id_of_criteria(String label){ SQLiteDatabase db=this.getWritableDatabase(); String selectQuery = "SELECT criteria_id FROM Criteria WHERE criteria_name = "+"'" + label +"'"; Cursor id = db.rawQuery(selectQuery, null); db.close(); return id; </code></pre> <p>}</p> <p>and in loadSpinnerdata I put this</p> <pre><code>//display id of criteria Cursor id2 = db.find_id_of_criteria(label); String cri =(String.valueOf(id2).toString()); cripk.setText(cri); </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload