Differences
This shows you the differences between two versions of the page.
Both sides previous revision Previous revision Next revision | Previous revision | ||
swdev:android:database_sqlite [2018/04/30 09:01] smayr [Database Helper] |
swdev:android:database_sqlite [2018/05/02 20:30] (current) smayr [Database Utils] |
||
---|---|---|---|
Line 1: | Line 1: | ||
= Database: SQLite = | = Database: SQLite = | ||
==== DB Connector ==== | ==== DB Connector ==== | ||
- | Download | + | When using the built-in '' |
- | * [[https:// | + | |
- | * Extract JAR file to some folder. | + | |
- | * Add JAR file as module: | + | |
- | * JAR package should be now listed under the project Gradle Scripts: '' | + | |
- | * If there is an error including the JAR package, try the following: | + | |
- | * In '' | + | |
- | include ': | + | |
- | include ': | + | |
- | </ | + | |
- | * Sync project (File > Sync Project with Gradle Files). | + | |
- | * Add '' | + | |
- | * Right-click on the '' | + | |
- | * In the top left navigation pane of the " | + | |
- | * Click on the green plus (+), and select ' | + | |
- | * Click OK, and close all the opened windows. A synchronization process should take place once more. | + | |
- | * If there is an error including the JAR package, try the following: | + | |
- | * In '' | + | |
- | dependencies { | + | |
- | //... | + | |
- | compile project(': | + | |
- | } | + | |
- | </ | + | |
- | * If succesfull, under the '' | + | |
- | * Include the classes required in your code. | + | |
Line 246: | Line 222: | ||
//String selection = DbContract.TeamEntry.COL_NAME + " = ?"; | //String selection = DbContract.TeamEntry.COL_NAME + " = ?"; | ||
//String[] selectionArgs = { "My Team" }; | //String[] selectionArgs = { "My Team" }; | ||
+ | String selection = null; // no filters | ||
+ | String[] selectionArgs = null; // no filters | ||
// How you want the results sorted in the resulting Cursor | // How you want the results sorted in the resulting Cursor | ||
Line 254: | Line 232: | ||
DbContract.TeamEntry.TABLE_NAME, | DbContract.TeamEntry.TABLE_NAME, | ||
projection, | projection, | ||
- | | + | selection, |
- | | + | selectionArgs, |
- | //selectionArgs, | + | |
- | null, | + | |
null, // don't group the rows | null, // don't group the rows | ||
null, // don't filter by row groups | null, // don't filter by row groups | ||
Line 294: | Line 270: | ||
//String selection = DbContract.TeamEntry.COL_NAME + " = ?"; | //String selection = DbContract.TeamEntry.COL_NAME + " = ?"; | ||
//String[] selectionArgs = { "My Team" }; | //String[] selectionArgs = { "My Team" }; | ||
+ | String selection = null; // no filters | ||
+ | String[] selectionArgs = null; // no filters | ||
// How you want the results sorted in the resulting Cursor | // How you want the results sorted in the resulting Cursor | ||
Line 303: | Line 281: | ||
projection, | projection, | ||
// | // | ||
- | null, | ||
// | // | ||
- | null, | ||
null, // don't group the rows | null, // don't group the rows | ||
null, // don't filter by row groups | null, // don't filter by row groups | ||
Line 360: | Line 336: | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | === MainActivity === | ||
+ | <code java> | ||
+ | public class MainActivity extends AppCompatActivity { | ||
+ | |||
+ | // Const | ||
+ | private static final String TAG = " | ||
+ | |||
+ | // Properties | ||
+ | ListView lstTeams; | ||
+ | String[] arrTeams; | ||
+ | List< | ||
+ | |||
+ | @Override | ||
+ | protected void onCreate(Bundle savedInstanceState) | ||
+ | { | ||
+ | super.onCreate(savedInstanceState); | ||
+ | setContentView(R.layout.activity_main); | ||
+ | |||
+ | // Connect to resources | ||
+ | Resources res = getResources(); | ||
+ | lstTeams = (ListView) findViewById(R.id.lstTeams); | ||
+ | //arrTeams = res.getStringArray(R.array.teams); | ||
+ | |||
+ | try { | ||
+ | DbUtils dbutil = new DbUtils(); | ||
+ | dbutil.getConnection(this); | ||
+ | teamNames = dbutil.findTeamNamesAll(); | ||
+ | } catch (Exception ex) { | ||
+ | ex.printStackTrace(); | ||
+ | } | ||
+ | // Using custom ItemAdapter with custom layout | ||
+ | TeamItemAdapter itemAdapter = new TeamItemAdapter( | ||
+ | this, // context | ||
+ | // | ||
+ | teamNames | ||
+ | ); | ||
+ | lstTeams.setAdapter(itemAdapter); | ||
+ | |||
+ | lstTeams.setOnItemClickListener(new AdapterView.OnItemClickListener() { | ||
+ | @Override | ||
+ | public void onItemClick(AdapterView<?> | ||
+ | Log.d(TAG, " | ||
+ | Toast.makeText(getApplicationContext(), | ||
+ | |||
+ | Intent showRubricActivity = new Intent(getApplicationContext(), | ||
+ | showRubricActivity.putExtra(" | ||
+ | showRubricActivity.putExtra(" | ||
+ | startActivity(showRubricActivity); | ||
+ | } | ||
+ | }); | ||
+ | } | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | activiy_main.xml Layout: | ||
+ | <code xml> | ||
+ | <?xml version=" | ||
+ | < | ||
+ | xmlns: | ||
+ | xmlns: | ||
+ | android: | ||
+ | android: | ||
+ | tools: | ||
+ | |||
+ | < | ||
+ | android: | ||
+ | android: | ||
+ | android: | ||
+ | android: | ||
+ | android: | ||
+ | android: | ||
+ | app: | ||
+ | app: | ||
+ | app: | ||
+ | </ | ||
+ | </ | ||
+ | |||
+ | === ItemAdapter === | ||
+ | <code java> | ||
+ | package com.acme.myapp; | ||
+ | |||
+ | import android.content.Context; | ||
+ | import android.view.LayoutInflater; | ||
+ | import android.view.View; | ||
+ | import android.view.ViewGroup; | ||
+ | import android.widget.BaseAdapter; | ||
+ | import android.widget.TextView; | ||
+ | |||
+ | import java.util.List; | ||
+ | |||
+ | public class TeamItemAdapter extends BaseAdapter { | ||
+ | LayoutInflater mInflater; | ||
+ | |||
+ | //String[] arrTeams; | ||
+ | List< | ||
+ | |||
+ | //public TeamItemAdapter(Context cx, String[] teams) | ||
+ | public TeamItemAdapter(Context cx, List< | ||
+ | { | ||
+ | arrTeams = teams; | ||
+ | |||
+ | mInflater = (LayoutInflater) cx.getSystemService(Context.LAYOUT_INFLATER_SERVICE); | ||
+ | } | ||
+ | |||
+ | @Override | ||
+ | //public int getCount() | ||
+ | //{ | ||
+ | // return arrTeams.length; | ||
+ | //} | ||
+ | public int getCount() | ||
+ | { | ||
+ | return arrTeams.size(); | ||
+ | } | ||
+ | |||
+ | @Override | ||
+ | //public Object getItem(int idx) | ||
+ | //{ | ||
+ | // return arrTeams[idx]; | ||
+ | //} | ||
+ | public Object getItem(int idx) | ||
+ | { | ||
+ | return arrTeams.get(idx); | ||
+ | } | ||
+ | |||
+ | @Override | ||
+ | public long getItemId(int idx) | ||
+ | { | ||
+ | return idx; | ||
+ | } | ||
+ | |||
+ | @Override | ||
+ | public View getView(int idx, View view, ViewGroup viewGroup) | ||
+ | { | ||
+ | // Use layout inflater. | ||
+ | |||
+ | View vw = mInflater.inflate(R.layout.listview_detail_team, | ||
+ | TextView lblItemNumber = (TextView) vw.findViewById(R.id.lblItemNumber); | ||
+ | TextView lblTeamName = (TextView) vw.findViewById(R.id.lblTeamName); | ||
+ | |||
+ | String strItemNumber = (idx+1) + ""; | ||
+ | //String strTeam = arrTeams[idx]; | ||
+ | String strTeam = arrTeams.get(idx); | ||
+ | |||
+ | lblItemNumber.setText(strItemNumber); | ||
+ | lblTeamName.setText(strTeam); | ||
+ | |||
+ | return vw; | ||
+ | } | ||
} | } | ||
</ | </ | ||
Line 368: | Line 495: | ||
* [[https:// | * [[https:// | ||
* [[https:// | * [[https:// | ||
+ | * [[https:// |