0
My application has a screen that lists all the elements registered in BD. The method I use to list the results returns a List (in the Diagnosticodatasource class), until then all right, the problem begins when I load the list to display the results, it displays only the first item (of the checkbox selected) and the algorithm does not allow inserting another item after the first insertion.
I have already looked for several ways to solve this problem, I even found a very similar issue on the forum, but also did not solve my problem Only print the first element of Arraylist
Code snippets
Diagnostico (screen printing in Listview):
public void carregarLista() {
DiagnosticoDataSource dsDiagnostico = new DiagnosticoDataSource(this);
dsDiagnostico.open();
List<DiagnosticoSintomas> diagnosticos = dsDiagnostico.getAlldiagnostico();
dsDiagnostico.close();
ArrayAdapter<DiagnosticoSintomas> adapter = new ArrayAdapter<DiagnosticoSintomas>(this,
android.R.layout.simple_list_item_1, diagnosticos);
listDiagnostico.setAdapter(adapter);
}
Diagnosticodatasource:
public class DiagnosticoDataSource {
private SQLiteDatabase sqlDatabase;
private Database database;
private String[] colunas = { Database.COLUMN_ID, Database.COLUMN_SINTOMAS };
public DiagnosticoDataSource(Context context) {
database = new Database(context);
}
public void open() throws SQLException {
sqlDatabase = database.getWritableDatabase();
}
public void close() {
database.close();
}
public DiagnosticoSintomas create(DiagnosticoSintomas diagnosticoSintomas) {
ContentValues values = new ContentValues();
values.put(Database.COLUMN_ID, diagnosticoSintomas.get_id());
values.put(Database.COLUMN_SINTOMAS, diagnosticoSintomas.getSintomas().toString());
long id = 0;
if (diagnosticoSintomas.get_id() != 0) {
id = sqlDatabase.update(Database.TABLE_SINTOMAS, values, Database.COLUMN_ID + " = ?",
new String[] { String.valueOf(diagnosticoSintomas.get_id()) });
} else {
id = sqlDatabase.insert(Database.TABLE_SINTOMAS, null, values);
}
return getDiagnostico((int) id);
}
public DiagnosticoSintomas getDiagnostico(int _id) {
Cursor cursor = sqlDatabase.query(Database.TABLE_SINTOMAS,
colunas, Database.COLUMN_ID + " = " + _id, null, null, null, null);
cursor.moveToFirst();
DiagnosticoSintomas newDiagnostico = cursorToDiagnostico(cursor);
cursor.close();
return newDiagnostico;
}
public void delete(DiagnosticoSintomas diagnostico) {
long id = diagnostico.get_id();
sqlDatabase.delete(Database.TABLE_SINTOMAS, Database.COLUMN_ID + " = " + id, null);
}
public List<DiagnosticoSintomas> getAlldiagnostico() {
List<DiagnosticoSintomas> diagnosticos = new ArrayList<DiagnosticoSintomas>();
Cursor cursor = sqlDatabase.query(Database.TABLE_SINTOMAS, colunas, null, null, null, null,
Database.COLUMN_SINTOMAS);
cursor.moveToFirst();
while (!cursor.isAfterLast()) {
DiagnosticoSintomas diagnostico = cursorToDiagnostico(cursor);
diagnosticos.add(diagnostico);
cursor.moveToNext();
}
cursor.close();
return diagnosticos;
}
private DiagnosticoSintomas cursorToDiagnostico(Cursor cursor) {
ArrayList<String> a = new ArrayList<String>();
a.add(cursor.getString(1));
DiagnosticoSintomas diagnostico = new DiagnosticoSintomas();
diagnostico.set_id(Integer.parseInt(cursor.getLong(0) + ""));
diagnostico.setSintomas(a);
return diagnostico;
}
}
Xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="${relativePackage}.${activityClass}" >
<ListView
android:id="@+id/diagnosticos"
android:layout_width="wrap_content"
android:layout_height="wrap_content"></ListView>
Database:
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(
"CREATE TABLE sintomas (_id INTEGER PRIMARY KEY, sintomas TEXT);");
}
The answer to the link question says that the problem is that Textview is only filled in when the convertView/v
is null. The equivalent variable in my code is diagnosticoSintomas.get_id()
you are doing the same thing, only you are also checking when the variable is different from 0. Thanks in advance.