0

I am rather new using SQL in Android apps, and I am having some trouble to update the table I have created with a listView I use with it. I manage to update an element from the listView, but it's not the right one, it's always the first one from the list that changes.

I have made a table with only one column (I shall add more two later on) and here is how i build it:

TaskContract_Faltas.java

class TaskContract_Faltas {
  static final String DB_NAME = "com.example.TodoList.db.tasks";
  static final int DB_VERSION = 1;
  static final String TABLE = "tasks";

  class Columns {
      static final String TASK = "task";
      static final String _ID = BaseColumns._ID;
  }
}

TaskDbHelper_Faltas

class TaskDbHelper_Faltas extends SQLiteOpenHelper {

TaskDbHelper_Faltas(Context context) {
    super(context, TaskContract_Faltas.DB_NAME, null, TaskContract_Faltas.DB_VERSION);
}

@Override
public void onCreate(SQLiteDatabase sqlDB) {
    String sqlQuery =
            String.format("CREATE TABLE %s (" +
                            "_id INTEGER PRIMARY KEY AUTOINCREMENT, " +
                            "%s TEXT)", TaskContract_Faltas.TABLE,
                    TaskContract_Faltas.Columns.TASK);

    Log.d("TaskDBHelper","Query to form table: "+sqlQuery);
    sqlDB.execSQL(sqlQuery);
}

@Override
public void onUpgrade(SQLiteDatabase sqlDB, int i, int i2) {
    sqlDB.execSQL("DROP TABLE IF EXISTS "+TaskContract_Faltas.TABLE);
    onCreate(sqlDB);
}
}

Faltas.java

ListView lista;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.faltas2);
    setTitle("Gerenciador de Faltas");

    lista = (ListView) findViewById(R.id.lista_falta);

    updateUI();

}

//...

public void edita_Materia(View view){

    final Dialog dialog = new Dialog(Faltas2.this);
    dialog.setContentView(R.layout.janela_faltas_add);

    TextView titulo = (TextView) dialog.findViewById(R.id.Titulo_Dialog);
    titulo.setText("Editar Matéria");

    Button cancelarBotao = (Button) dialog.findViewById(R.id.botao_cancelar);
    Button aceitarBotao = (Button) dialog.findViewById(R.id.botao_aceitar);
    final EditText nome_materia = (EditText) dialog.findViewById(R.id.texto_disciplina_falta);

    // The error starts here
    TextView texto = (TextView) findViewById(R.id.Titulo_disciplina);
    nome_materia.setText(texto.getText().toString(), TextView.BufferType.EDITABLE);

    final String teste = texto.getText().toString();
    // From here        

    cancelarBotao.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            dialog.dismiss();
        }
    });

    aceitarBotao.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            String task = nome_materia.getText().toString();

            String sql = String.format("UPDATE %s SET %s = '%s' WHERE %s = '%s'",
                    TaskContract_Faltas.TABLE,
                    TaskContract_Faltas.Columns.TASK,
                    task,
                    TaskContract_Faltas.Columns.TASK,
                    teste);

            Log.d("Faltas2", "A ID eh: " + TaskContract_Faltas.Columns._ID);

            helper = new TaskDbHelper_Faltas(Faltas2.this);
            SQLiteDatabase sqlDB = helper.getWritableDatabase();
            sqlDB.execSQL(sql);
            updateUI();
            dialog.dismiss();
        }
    });

    dialog.show();
}

private void updateUI() {

    helper = new TaskDbHelper_Faltas(Faltas2.this);
    SQLiteDatabase sqlDB = helper.getReadableDatabase();
    Cursor cursor = sqlDB.query(TaskContract_Faltas.TABLE,
            new String[]{TaskContract_Faltas.Columns._ID, TaskContract_Faltas.Columns.TASK},
            null, null, null, null, null);

    ListAdapter listAdapter = new SimpleCursorAdapter(
            this,
            R.layout.item_todo,
            cursor,
            new String[]{TaskContract.Columns.TASK},
            new int[]{R.id.Titulo_disciplina},
            0
    );
    lista.setAdapter(listAdapter);
    visi_info();
}

I make a call to the update function into the item_todo.xml file using android:onClick="edita_Materia"

<!-- ... -->
<ImageButton
    android:id="@+id/botao_editar"
    android:src="@drawable/ic_edit_black_24dp"
    android:layout_width="50dp"
    android:layout_height="50dp"
    android:scaleType="fitCenter"
    android:adjustViewBounds="true"
    android:onClick="edita_Materia"
    android:background="@drawable/botao_borda_falta"
    android:layout_alignParentTop="true"
    android:layout_alignParentEnd="true"
    android:contentDescription="@string/botao_materia" />
<!-- ... -->

I understand that I am not getting the right ID but I can't seem to find a way to get the right element from the listView. If you take a look on the String sql line, I only reach the element from a row comparing a string data, and not through Ids. Can someone help me with that? Thanks!

EDIT: Image from the List itself. Image example for the listView.

Leminur
  • 291
  • 2
  • 7
  • 22
  • 1
    when you add the value in texto save the id also and use the id in "edita_Materia" – Muthukrishnan Rajendran Jun 17 '17 at 02:00
  • I don't understand. Can you show me how I can save such id? The call comes from a XML`android:onClick`, I am not sure exactly how I can retrieve an id coming from it. – Leminur Jun 17 '17 at 04:55
  • How you are setting a value in texto..? – Muthukrishnan Rajendran Jun 17 '17 at 07:36
  • That is the problem I am trying to solve. The value in `String texto` is always the first value in the listView, and not the actual value from the element I have clicked. So for instance, my list have 3 elements, if I try to edit such value on the third element, it will always edit the first element of the listView, and so I am not exactly sure how I am going to access the element I want through the way I showed above. – Leminur Jun 17 '17 at 17:33

1 Answers1

0

Ar you trying to update a list item when clicked on the button acceitarBotao? If so, I would suggest calling a function that fetches data from sqlite for that sprcific ID (the position of the clicked listitem is the id) and updates that specific list item, and than calls the method notifyDatasetChanged, to notify the adapter that sprcific list item has changed, so it would uldate the listview properly.

Nasi Jofce
  • 132
  • 2
  • 10
  • That is what I am trying to find out. I have edit the original post and I have added an image there from the list so you can have an idea. I have no idea how I can retrieve the position from the list using `android:onclick`. Can you suggest me a way to retrieve said position? Thanks! – Leminur Jun 18 '17 at 02:18
  • https://stackoverflow.com/questions/20541821/get-listview-item-position-on-button-click – Nasi Jofce Jun 18 '17 at 06:05
  • I see what you mean, but it does not show how I retrieve the position using `android:onClick` but rather `setOnItemClickListener`, so you want me to use it instead of a direct function call from XML? – Leminur Jun 18 '17 at 23:52
  • Forget that, I realize that the second answer helped me to retrieve the position, I just need to find a way to use such position with BaseColumns._ID. Do you have any idea how to anyway? Thanks for the help! – Leminur Jun 19 '17 at 00:09
  • I would suggest adding another column in the DB which will hold the positions of the elements in the list. – Nasi Jofce Jun 19 '17 at 05:40
  • I have found an easier way, I have made a check if the name is the same as others and then I made `((View) v.getParent())` for the id inside the object, thanks for the help anyway! – Leminur Jun 19 '17 at 19:46