Update a specific row in a Room Database of Android - android

I am new to Android and need some help.
I have a listView with some edittext fields and buttons and store the information of the edittext in a room database.
Looks like this: https://imgur.com/a/NTpMUmY
Now, when I change the input of field A to, let's say AAAA, and click on the "button", the room database has to update the row.
My question is when exactly do I call the update command, and how does room know which row has changed? I don't want to update to whole table.
Let me know if I should explain it in another way.

Ok, I found a solution which works for me.
TextWatcher textWatcher = new TextWatcher() {
#Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
#Override
public void afterTextChanged(Editable s) {
Dao_db.update(s.toString());
}
};
viewHolder.bearbeiten_et.addTextChangedListener(textWatcher);
I use this Textwatcher (didn't know that something like this exist) and update my db when the text changed. :)

Related

android addTextChangedListener not fully deleting the text in Firebase when backspace

I was facing the problem with the addTextChangedListener that was not fully delete the text in Firebase. I state an example, I get my name from Firebase and setText() on the myname. So on myname editText has show my name there. When I wanted to edit, I click on editText, so I can remove the word by backspace, but when I backspace too fast, the text in editText was fully removed (client side) and there are some word did not delete on Firebase(server side).
Which mean the removing text value are not consistent and accurate with the client side and server side. The ordinary text of my name is Tommy, so I backspace until the Tommy word gone, so that was cleared but in Firebase it still showing the first character 'T' of my name.
But when I was clearing all word on the editText, I input new word such as "Hello", the Firebase will store Hello.
Code:
//delcare myname
private MultiAutoCompleteTextView myname;
myname.addTextChangedListener(new TextWatcher() {
#Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
#Override
public void afterTextChanged(Editable s) {
if(!myname.getText().toString().trim().isEmpty()){
DatabaseReference nameRef = FirebaseDatabase.getInstance().getReference()
.child(Config.URL_USER);
nameRef.child(uid).child("myName").setValue(userET.getText().toString());
parentActivity.updateHeaderUserName(myname.getText().toString());
}
}
});
This loops work until there is a single charcter in the edittext, so add an else loop and inside the else loops set the value as null

where condition in SQLite and EditText related Event

Respected Users,
I am new with android and SQLite technology.
I am getting error in following statement.
Kindly correct me.
Cursor mCur=
db.query(DATABASE_TABLE,
new String[]{ROWID,MEDICINE,KEY_DESC,PRICE},
MEDICINE+" = "+medName,null,null,null,null);
My Other question is that is there any event associated with EditText so that I can use its functionality as I was using it with text_change in C# .NET.
[When I types the text event should get fired].
For this I have tried with following events:
setOnEditActionListener and
setOnTouchListener
1) In your SQL query I assume your medName was the problem because it is a String, use:
Cursor mCur=
db.query(DATABASE_TABLE,
new String[]{ROWID,MEDICINE,KEY_DESC,PRICE},
MEDICINE+" = ?",new String[] {medName},null,null,null);
This approach also protects you from SQL injection attacks.
2) To listen for changes in an EditText, use a TextWatcher.
EditText edit = (EditText) findViewById(R.id.edit);
edit.addTextChangedListener(new TextWatcher() {
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
public void afterTextChanged(Editable s) {
}
});
For your Sqlite question please post the Logcat logs.
Add a TextWatcher to your EditText to implement 3 callback methods.

Filter ListView in real time

I want to filter a ListView in real time. I have an EditText in the ActionBar, and each time the user writes a character I want to update the cursor of the ListView filtering the information.
I have done an AsyncTask to perform the query but I have two questions:
1º) If the user types three characters I'm creating three AsyncTasks (one to search the first character, one to search the two first characters, and finally one to search the three characters). Is there a simple way to say to the AsyncTask to replace the previous task with the new one?
2º) How can I put a small delay to start the AsyncTask? So if the user types three characters without stopping, I will not create the AsyncTask until the end.
Thanks!
Hmmm...Not totally sure why your using AsyncTask for search in real-time. Here's how I would(and have successfully) do it.
Add a TextChangeListener to your Edittext:
editText.addTextChangedListener(new TextWatcher() {
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
searchResults = myDbHelper.searchAll(s.toString());
searchAdapter.notifyDataSetChanged();
}
#Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
}
#Override
public void afterTextChanged(Editable s) {
// TODO Auto-generated method stub
}
});
I am searching through a sqlite database everytime a user presses a key. My searchResults is used to populate the ListView, and so after I set the get the search results, tell the list adapter that the data set changed.
Hope this helps.

quick search within app in android?

I have a application already working fine. I need to implement a quick search feature to it. Quick search i mean as the users types in every single character i want to result to be fetched. My Activity is a list activity for which the data is coming from the database query. Say the list view has 50 items, and when the user searches with word as "test" I want to query the database and filter the items and display it in the same list view. Something like the contact search in android. Please let me know how to implement this. A quick sample would be help full. Thank you.
you can do by this
I implemented this feature in one of my previous app. The entire code is too long to be pasted here. So, I have posted some portions of it. You may read a bit about the approach used by me and get it done for your app.
EditText filterText;
words = new MySimpleCursorAdapter(Main.this, R.layout.emptylist, temp, from, to);
filterText= (EditText) findViewById(R.id.search_box);
filterText.addTextChangedListener(filterTextWatcher);
words.setFilterQueryProvider(new FilterQueryProvider() {
#Override
public Cursor runQuery(CharSequence constraint) {
Cursor cur=mDbHelper.fetchSugNotes(filterText.getText().toString());
return cur;
}
});
private TextWatcher filterTextWatcher = new TextWatcher() {
public void afterTextChanged(android.text.Editable s) {
};
public void beforeTextChanged(CharSequence s, int start, int count, int after) {};
public void onTextChanged(CharSequence s, int start, int before, int count) {
words.getFilter().filter(s.toString());
};
};

show data in listview from webservice according to keypress in android

I am developing an android app for a Korean client. I have to search a data according to key press, like, if I press A then all the data who started from A show in listview where data come from a web service in listview. Is it possible?
If yes, then how to do it. If possible give me some code or link which I can use?
Thanks.
Yes its possible.You can add TextChangedListener on your edittext and in the onTextChanged event get the date from the webserver as json or xml and parse that data and show the data in a listview.
edittext.addTextChangedListener(textWacther);
final TextWatcher textWacther = new TextWatcher() {
public void afterTextChanged(Editable s) {
}
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
}
public void onTextChanged(CharSequence s, int start, int before,
int count) {
getdata();
}
};
Getting the data from webserver this question might be helpful for you
How to connect to a Remote Database with Webservices?
At the area where getData() is called in #Syam's code, fetch the new data according to the text entered in the edit text, put it in the array that has the source of ListView, don't forget to call setDataSetChanged() on the adapter in the end.

Categories

Resources