Salve, sapreste aiutarmi a creare un database sqlite e una listview per la mia app?
non so come si creano...![]()
Salve, sapreste aiutarmi a creare un database sqlite e una listview per la mia app?
non so come si creano...![]()
Usa questa libreria per il database SQL: https://github.com/pardom/ActiveAndr...etting-started
Per la listview devi crearti l'adapter:
1
2public class MyBaseAdapter extends BaseAdapter {
3
4private Context mContext;
5private List<MySqlObject> mObjects;
6
7public MyBaseAdapter(Context con, List<MySqlObject> objs) {
8// constructor
9this.mContext = con;
10this.mObjects = objs;
11}
12
13<a rel="nofollow" href="https://www.androidiani.com/forum/members/override.html" target="_blank">Override</a>
14public int getCount() {
15return mObjects.size();
16}
17
18<a rel="nofollow" href="https://www.androidiani.com/forum/members/override.html" target="_blank">Override</a>
19public Profile getItem(int arg0) {
20return mObjects.get(arg0);
21}
22
23<a rel="nofollow" href="https://www.androidiani.com/forum/members/override.html" target="_blank">Override</a>
24public long getItemId(int arg0) {
25return mObjects.get(arg0).hashCode();
26}
27
28//remove items from the adapter and notify it
29public void remove(int position) {
30mObjects.remove(position);
31this.notifyDataSetChanged();
32}
33
34
35<a rel="nofollow" href="https://www.androidiani.com/forum/members/override.html" target="_blank">Override</a>
36public View getView(final int position, View convertView, ViewGroup parent) {
37ViewHolder holder;
38if(convertView == null) {
39LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
40convertView = inflater.inflater(R.layout.my_row_layout, parent, false);
41holder = new ViewHolder();
42holder.mTitle = (TextView) convertView.findViewById(R.id.title);
43holder.mSummary = (TextView) convertView.findViewById(R.id.summary);
44convertView.setTag(holder);
45}else{
46holder = (ViewHolder) convertView.getTag();
47}
48holder.mTitle.setText(getItem(position).title);
49holder.mSummary.setText(getItem(position).summary);
50
51return convertView;
52}
53
54class ViewHolder {
55TextView mTitle;
56TextView mSummary;
57}
58}
59
questo è un esempio di adapter
la classe ViewHolder viene utilizzata per una questione di performance, dato che gli adapters riciclano le views. In questo modo, se l'adapter ad una determinata posizione, ha già creato la view andrà a ripescarla, in caso contrario "creerà" la nuova view (convertView == null | convertView != null).
Chiaramente devi implementare, il tuo layout, il tuo ViewHolder e la tua "lista di oggetti"
![]()
Ultima modifica di cesco; 08-09-14 alle 16:59
Follow me on:
Androidiani app
<!-- Place this tag where you want the widget to render. -->
<div class="g-person" data-width="180" data-href="//plus.google.com/113012341277613226011" data-theme="dark" data-rel="author"></div>
<!-- Place this tag after the last widget tag. -->
<script type="text/javascript">
window.___gcfg = {lang: 'it'};
(function() {
var po = document.createElement('script'); po.type = 'text/javascript'; po.async = true;
po.src = 'https://apis.google.com/js/plusone.js';
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(po, s);
})();
</script><br>
<a class="twitter-timeline" href="https://twitter.com/xcesco89" data-widget-id="398762031488040960">Tweets di @xcesco89</a>
<script>!function(d,s,id){var js,fjs=d.getElementsByTagName(s)[0],p=/^http:/.test(d.location)?'http':'https';if(!d.getElementB yId(id)){js=d.createElement(s);js.id=id;js.src=p+" ://platform.twitter.com/widgets.js";fjs.parentNode.insertBefore(js,fjs);}} (document,"script","twitter-wjs");</script>
Estensione Androidiani per Google Chrome
Salve, che tipo di libreria è quella?
come viene organizzata e come gestirla ?
per ora ho solo importato la libreria..
Ultima modifica di xixietto; 12-09-14 alle 16:36
A questo link spiega passo passo come implementare il tutto: https://github.com/pardom/ActiveAndr...etting-started
fondamentalmente, devi inizializzare la libreria come indicato , quindi settando il manifest e creando una classe che estenda la classe Application di ActiveAndroid.
La libreria è basata sulle annotazioni (@qualcosa(qualche parametro))
ogni classe che e estende "Model" diverrà fisicamente un elemento nelle tabelle SQL.
Per ogni classe che estende "Model" ActiveAndroid creerà una tabella nel DB contenente tutti quei modelli.
Ovviamente puoi collegare più modelli fra loro e ActiveAndroid li "linkerà" automaticamente (vedi esempi nel wiki della libreria)
Il query è davvero semplice da fare e puoi scegliere se ricevere un elemento singolo o una lista (List<miaclasse extends Model>)
anche l'eliminazione degli elementi è davvero banale.
Un esempio di modello:
Spoiler:
Alcuni esempi per l'interazione con il database:
Spoiler:
Più semplice di così..![]()
Follow me on:
Androidiani app
<!-- Place this tag where you want the widget to render. -->
<div class="g-person" data-width="180" data-href="//plus.google.com/113012341277613226011" data-theme="dark" data-rel="author"></div>
<!-- Place this tag after the last widget tag. -->
<script type="text/javascript">
window.___gcfg = {lang: 'it'};
(function() {
var po = document.createElement('script'); po.type = 'text/javascript'; po.async = true;
po.src = 'https://apis.google.com/js/plusone.js';
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(po, s);
})();
</script><br>
<a class="twitter-timeline" href="https://twitter.com/xcesco89" data-widget-id="398762031488040960">Tweets di @xcesco89</a>
<script>!function(d,s,id){var js,fjs=d.getElementsByTagName(s)[0],p=/^http:/.test(d.location)?'http':'https';if(!d.getElementB yId(id)){js=d.createElement(s);js.id=id;js.src=p+" ://platform.twitter.com/widgets.js";fjs.parentNode.insertBefore(js,fjs);}} (document,"script","twitter-wjs");</script>
Estensione Androidiani per Google Chrome
ho risolto con il database![]()
171public void onCreate(SQLiteDatabase db) {
2// Creazione delle tabelle
3String sql = "";
4sql += "CREATE TABLE movimento (";
5sql += " _id INTEGER PRIMARY KEY,";
6sql += " entrata_uscita TEXT NOT NULL,";
7sql += " importo INTEGER NOT NULL,";
8sql += " descrizione TEXT NOT NULL,";
9sql += " data TEXT NOT NULL";
10sql += ")";
11db.execSQL(sql);
12}
13public Cursor getAll()
14{
15String query = "SELECT(*) FROM movimento";
16return getReadableDatabase().rawQuery(query, null);
17}
ho usato un altro modo per il db....ora non capisco come creare la listview...![]()
Ultima modifica di xixietto; 16-09-14 alle 16:12
creati una classe per gestire tutti i valori:
211
2public class Movimento {
3
4public Movimento (int _id, String _in_out, String _importo, String _desc, String _data) {
5this.id = _id;
6this.entrata_uscita = _in_out;
7this.importo = _importo;
8this.descrizione = _desc;
9this.data = _data;
10}
11
12public Movimento() {
13//empty constructor
14}
15
16public int id;
17public String entrata_uscita;
18public String importo;
19public String descrizione;
20public String data;
21
aggiungi questo metodo per avere il tutto in formato lista:
391
2public ArrayList<Movimento> getResults() {
3
4MyDb db = new MyDb(this); //instanzia il tuo database
5db.open();
6
7ArrayList<Movimento> resultList = new ArrayList<Movimento>();
8
9Cursor c = db.getAll();
10while (c.moveToNext())
11{
12int id = c.getInt(c.getColumnIndex("_id)
13String in_out = c.getString(c.getColumnIndex("entrata_uscita"));
14String importo = c.getString(c.getColumnIndex("importo"));
15String descrizione = c.getString(c.getColumnIndex("descrizione"));
16String data = c.getString(c.getColumnIndex("data"));
17
18try
19{
20Movimento mov = new Movimento();
21mov.id = id;
22mov.entrata_uscita = in_out;
23mov.importo = importo;
24mov.descrizione = descrizione;
25mov.data = data;
26resultList.add(mov);
27}
28catch (Exception e) {
29Log.e(MY_DEBUG_TAG, "Error " + e.toString());
30}
31
32}
33
34c.close();
35
36db.close();
37return resultList;
38}
39
una volta ottenuta la lista devi impostare correttamente l'adapter. Quello postato nel mio post precedente è una buona base di partenza (cambia List<MySqlObject> in ArrayList<Movimento>).
Ricorda di creare il layout per le righe della listview e soprattutto aggiungi eventuali view al viewholder, come indicato nell'esempio
una volta fatto tutto:
51
2mList = (ListView) findViewById(R.id.mia_lista);
3mAdapter = new MioSqlListAdapter(parametri);
4mList.setAdapter(mAdapter);
5
Follow me on:
Androidiani app
<!-- Place this tag where you want the widget to render. -->
<div class="g-person" data-width="180" data-href="//plus.google.com/113012341277613226011" data-theme="dark" data-rel="author"></div>
<!-- Place this tag after the last widget tag. -->
<script type="text/javascript">
window.___gcfg = {lang: 'it'};
(function() {
var po = document.createElement('script'); po.type = 'text/javascript'; po.async = true;
po.src = 'https://apis.google.com/js/plusone.js';
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(po, s);
})();
</script><br>
<a class="twitter-timeline" href="https://twitter.com/xcesco89" data-widget-id="398762031488040960">Tweets di @xcesco89</a>
<script>!function(d,s,id){var js,fjs=d.getElementsByTagName(s)[0],p=/^http:/.test(d.location)?'http':'https';if(!d.getElementB yId(id)){js=d.createElement(s);js.id=id;js.src=p+" ://platform.twitter.com/widgets.js";fjs.parentNode.insertBefore(js,fjs);}} (document,"script","twitter-wjs");</script>
Estensione Androidiani per Google Chrome
scusami se chiedo ogni mio problema qui... ma "MyDb db = new MyDb(this); //instanzia il tuo database
come devo modificare ?
questo è il mio db...301
2public class MioDatabaseHelper extends SQLiteOpenHelper {
3private static final String DB_NAME = "gestione_soldi_db";
4private static final int DB_VERSION = 1;
5public MioDatabaseHelper(Context context) {
6super(context, DB_NAME, null, DB_VERSION);
7}
8<a rel="nofollow" href="https://www.androidiani.com/forum/members/override.html" target="_blank">Override</a>
9public void onCreate(SQLiteDatabase db) {
10// Creazione delle tabelle
11String sql = "";
12sql += "CREATE TABLE movimento (";
13sql += " _id INTEGER PRIMARY KEY,";
14sql += " entrata_uscita TEXT NOT NULL,";
15sql += " importo INTEGER NOT NULL,";
16sql += " descrizione TEXT NOT NULL,";
17sql += " data TEXT NOT NULL";
18sql += ")";
19db.execSQL(sql);
20}
21public Cursor getAll()
22{
23String query = "SELECT(*) FROM movimento";
24return getReadableDatabase().rawQuery(query, null);
25}
26<a rel="nofollow" href="https://www.androidiani.com/forum/members/override.html" target="_blank">Override</a>
27public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
28// Aggiornamento delle tabelle
29}
30}
2) nella classe mvimento inserisco il metodo "public ArrayList<Movimento> getResults() {.........." ?