Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Use below code,</p> <pre><code>public class Classes extends Activity{ ImageView imageViewNewClass, imageViewDelete; static ListView mListView; String[] stg1; static List&lt;String[]&gt; names2 = null; private static List&lt;String&gt; myList = new ArrayList&lt;String&gt;(); DataManipulatorClass dataManipulator; ArrayAdapter&lt;CharSequence&gt; adapterSpinner; ArrayAdapter&lt;String&gt; adapter; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.classes); imageViewNewClass = (ImageView) findViewById(R.id.newclass); mListView = (ListView) findViewById(R.id.displaydata); imageViewNewClass.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { Intent intent = new Intent(Classes.this, Class_Create.class); startActivity(intent); } }); mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() { public void onItemClick(AdapterView&lt;?&gt; parent, View item, int position, long id) { SparseBooleanArray sp = mListView.getCheckedItemPositions(); StringBuffer str = new StringBuffer(); for (int i = 0; i &lt; sp.size(); i++) { if (sp.valueAt(i) == true) { String s = ((TextView) mListView.getChildAt(i)) .getText().toString(); str = str.append(" " + s); } } Toast.makeText(Classes.this, "Selected items are " + str.toString(), Toast.LENGTH_LONG).show(); } }); dataManipulator = new DataManipulatorClass(this); } @Override protected void onResume() { super.onResume(); names2 = dataManipulator.selectAll(); stg1 = new String[names2.size()]; int x = 0; String stg; for (String[] name : names2) { stg = "Class Name : " + name[1]; stg1[x] = stg; x++; } adapter = new ArrayAdapter&lt;String&gt;(this, R.layout.custom_list_item, stg1); mListView.setChoiceMode(ListView.CHOICE_MODE_SINGLE); mListView.setBackgroundResource(R.drawable.assignmentheader); mListView.setCacheColorHint(Color.TRANSPARENT); mListView.setDivider(new ColorDrawable(0x99000000)); mListView.setDividerHeight(1); mListView.setAdapter(adapter); adapter.notifyDataSetChanged(); } @Override protected void onDestroy() { super.onDestroy(); if (dataManipulator != null) dataManipulator.close(); } } </code></pre> <p><strong>DataManipulatorClass</strong></p> <pre><code>public class DataManipulatorClass { public static final String KEY_ROWID = "id"; private static final String DATABASE_NAME = "mydatabaseclass.db"; private static final int DATABASE_VERSION = 1; static final String TABLE_NAME = "newtableclass"; private static Context context; static SQLiteDatabase db; OpenHelper openHelper = null; private SQLiteStatement insertStmt; private static final String INSERT = "insert into " + TABLE_NAME + "(classname) values (?)"; public DataManipulatorClass(Context context) { DataManipulatorClass.context = context; OpenHelper openHelper = new OpenHelper(DataManipulatorClass.context); DataManipulatorClass.db = openHelper.getWritableDatabase(); this.insertStmt = DataManipulatorClass.db.compileStatement(INSERT); } public long insert(String classname) { this.insertStmt.bindString(1, classname); return this.insertStmt.executeInsert(); } public void close() { if (openHelper != null) { openHelper.close(); } } public void deleteAll() { db.delete(TABLE_NAME, null, null); } public List&lt;String[]&gt; selectAll() { List&lt;String[]&gt; list = new ArrayList&lt;String[]&gt;(); Cursor cursor = db.query(TABLE_NAME, new String[] { "id", "classname" }, null, null, null, null, "classname asc"); int x = 0; if (cursor.moveToFirst()) { do { String[] b1 = new String[] { cursor.getString(0), cursor.getString(1) }; list.add(b1); x = x + 1; } while (cursor.moveToNext()); } if (cursor != null &amp;&amp; !cursor.isClosed()) { cursor.close(); } cursor.close(); return list; } public boolean delete(long rowId) { /** this method deletes by id, the first column in your database */ return db.delete(TABLE_NAME, KEY_ROWID + "=" + rowId, null) &gt; 0; } public int update(String updateClass) { ContentValues values = new ContentValues(); values.put("classname", Code.className); return db.update(TABLE_NAME, values, KEY_ROWID + "=" + updateClass, null); } private static class OpenHelper extends SQLiteOpenHelper { OpenHelper(Context context) { super(context, DATABASE_NAME, null, DATABASE_VERSION); } @Override public void onCreate(SQLiteDatabase db) { db.execSQL("CREATE TABLE " + TABLE_NAME + " (id INTEGER PRIMARY KEY, classname TEXT)"); } @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME); onCreate(db); } } } </code></pre> <p>and call this below code inside your submit button click event.</p> <pre><code>this.dataManipulatorClass = new DataManipulatorClass(this); this.dataManipulatorClass.insert(Code.className); this.dataManipulatorClass.close(); </code></pre>
    singulars
    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.
    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