Update in a table row

Asked

Viewed 56 times

0

I’m starting to use the Sqlite on a project and would like a help on how to do the update in a specific line, in this case, only one "ID" will be updated at a time.

Follows the codes:

View and where is the update button:

 @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        //recupera o estado da posição atual
        final UserCommunity inviteUsers = usersList.get(position);

        //Cria uma instancia do layout .. na view
        View view = inflater.inflate(R.layout.item_invite_listview,null);

        TextView txt_Nome = (TextView)view.findViewById(R.id.txt_nome_invite);
        TextView txt_Email = (TextView)view.findViewById(R.id.txt_email_invite);
        TextView txt_Distancia = (TextView)view.findViewById(R.id.txt_distancia_invite);
        ImageButton btn_Share = (ImageButton)view.findViewById(R.id.img_invite);

        txt_Nome.setText(inviteUsers.name);
        txt_Email.setText(inviteUsers.email);
        txt_Distancia.setText(Integer.toString(inviteUsers.distance));

        btn_Share.setTag(position);
        btn_Share.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(AppController.getAppContext(),"Enviado para: "+inviteUsers.name,Toast.LENGTH_SHORT).show();
                usersList.remove(position);
                notifyDataSetChanged();
                service.upddateDataBase(inviteUsers.id,true);
            }
        });
}

Service with the update method (how to do?):

public class InviteUsersService {

    public void upddateDataBase(int id, boolean b) {

    }
}

Database code: (Field to be changed "invited BOOLEAN")

@Override

public void onCreate(SQLiteDatabase db) {

String CREATE_USERS_TABLE = "CREATE TABLE if not exists users ( " +
        "id INTEGER PRIMARY KEY, " +
        "name TEXT, "+
        "email TEXT, "+
        "company TEXT, "+
        "photo_url TEXT, "+
        "departure_time TEXT, "+
        "arrival_time TEXT, "+
        "address_lat DOUBLE,"+
        "address_lng DOUBLE,"+
        "invited BOOLEAN)";

// create books table
db.execSQL(CREATE_USERS_TABLE);

}
  • Read this tutorial. If you have any questions on it edit your question.

  • https://www.sqlite.org/lang_update.html

  • Is this the answer? public void upddateDataBase(int id, Boolean b) { Sqlitedatabase db = this.getReadableDatabase(); Contentvalues = new Contentvalues(); String Where = "Where id =?"; String[] whereArgs = new String[]{String.valueOf(id)}; values.put(KEY_INVITED,b); db.update(TABLE_USERS,values,Where,whereArgs); }

1 answer

0


 public void upddateDataBase(int id, boolean b) { 
SQLiteDatabase db = this.getReadableDatabase();
 ContentValues values = new ContentValues(); 
String where = "where id =?";
 String[] whereArgs = new String[]{String.valueOf(id)}; values.put(KEY_INVITED,b); db.update(TABLE_USERS,values,where,whereArgs); 
}

Answer to the question.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.