I want to get data from database and display it in a text view in a dialog box. In my application, I have a button that when it is clicked, a dialogue box will open. there is a text view in this dialogue box. The text view is going show data from SQLite database. Let me say some more. My database has four fields. one is id, the second title, third is the body and the last one is the translation of the body in another language. In the activity that shows the body, I have a button that when the user clicks on it, a dialogue box will appear that displays the translation. I have used recyclerView to show the titles and bodies.
Thanks in advance.
The easiest approach is to use Room and MVVM like this. The second option is to create a cursor and grab the data from that.
Cursor c = db.rawQuery("SELECT * FROM TABLE_NAME", null);
while(c.moveToNext()){
int id = c.getInt(0);
String title = c.getString(1);
String body = c.getString(2);
String bodyTranslated = c.getString(3);
}
This above code shows that the data is fetched from database and the cursor object is used to get the data and show it on the screen.
If you have problem with showing data in the AlertDialog check this link: Custom Dialog + Sending Information to Activity - Android Studio Tutorial
For RecyclerView related issues check this link: RecyclerView + CardView - Part 1 - LAYOUTS AND CUSTOM OBJECTS - Android Studio Tutorial
Related
My app users can create dynamic spinner and add items to it on click of button. Spinner is created using layout inflater.
Example :
User creates 2 spinner dynamically.
gives input for 1st spinner - male , female
gives input for 2nd spinner - apple , ball , cat.
Now I want get the data separate for each spinner and save it in firebase realtime database like.
(root)Dynamic->
spinner 1 ->
-Male
-female
spinner 2 ->
-apple
-ball
-cat
by using which method i can achieve this. i don't need the complete code just guide me which method to be used for it.
App Image : -
App screenshot
App screenshot
Lets say you have UserDefinedView class
class UserDefinedView {
String label;
String values[];
int id;
/*Other properties like viewType = [Spinner,EditText..]*/
}
Now create a list, where you can push user define views
ArrayList<UserDefinedView> userDefinedViews = new ArrayList<>();
UserDefinedView mUserObject = new UserDefinedView(/*label*/,/*Values*/,...);
userDefinedViews.push(mUserObject);
Now you can easily store array into firebase
I would like to get id from dialog box after user select text that populated in dialog box.
How can I populate data with hidden id to dialog box from web service?
Based on the info you gave, I'm guessing you want something like
EditText dialogBox = (EditText) findViewById(R.id.yourDialogBox);
int id = dialogBox.getId();
If you are referring to an actual Dialog object, I do not see any way to directly reference the ID of the dialog other than possibly
yourDialogObject.getOwnerActivity().getTaskId();
Without more info it is hard to say what exactly you are looking for.
I want to read data from the sqlite database in android studio and show them using TextView in a different activity say activity 2. So in first activity i will fill details and click submit button and on clicking the activity 2 will open and will show all the details that i inserted in activity 1 in textview.
this is a example.
String dataOne;
for(int i=0; i < mListData.size();i++){
dataOne = mListData.get(0).getName();
}
text_view.setText(dataOne);
This is the similar example what you are trying to achieve. below is the link
https://www.simplifiedcoding.net/how-to-fetch-data-from-sqlite-database-in-android/
thing is, if i click a button GETDATA - then a new activity starts new.java and i want it to display the contents of my database.
in this new.java i have strings name and id. and i have lined database and this class, such that name and id have the corresponding details that i want to display.
now the ques is, how to display them ?
if i have a
EditText ta;
ta=findViewBy(R.id.edittext1);
ta.setText(name);
should this work ?
it isnt working. also, i dont want user to change the contents and want to make this filed un-editable.
should i use
TextView tf;
ta=findViewBy(R.id.textview1);
tf.setText(name);
?
even this isnt happening.
what is the procedure for this ?
it is done by using append.
TextView ta;
ta=(TextEdit)findViewById(Ri.id.textView1);
String a=getData(1); // goto database, id is 1
ta.append(a);
A stupid question that Im sure is really really simple - ive been trying to fiddle to solve it myself, but its all very new to me so any wisdom would be REALLY appreciated!
With an android application (using eclipse) if I want to compile text for a text view using some data that is stored in a sqlite database (I have a working dbhelper and table I want to use), what do I do?
For example, I want a text box to say something like:
"hello"+#user_name+"its been"+#days+"since your last visit!"
where #user_name and #days are data in a table which I can currently retreive in a list (only 1 value in the list though).
How do i compile a string to be shown in the text view (and assign this string to the view)?
Help!
String textViewValue="hello "+nameList.get(0)+" its been "+daysList.get(0)+" since youy last visit";
TextView tv = (TextView) findViewById(R.id.textView1);
tv.setText(textViewValue);
You said you have stored it in arryalist right?
I am not sure if you have one ArrayList or two for Name and Days. (it would be good if you paste some of your code).
If your ArrayList is arrayList then you can get the name atleast like
"hello"+arrayList.get(0); // will retrive first element
Or best way use cursor and fire a query which will store output of following in curser
select Name,Days from your_table where Name='xyz';
then you can use folloing code to retrive data form curser (I am assuming there is only one row in cursor here)
if(cursor.moveToFirst()){
String s = "hello"+cursor.getString(0)+"Its been"+cursor.getString(1)+"days since your last visit!!";
}
now set this s in your textbox.