I have a database with three rows and columns.I have managed to insert the data into the table.Now i am trying to retrieve the values in a tabular format in my xml view.
How to go about this.
Any help is appreciated.
Thanks.
What you're asking for is a fair amount of work, but saying that, it's relatively straight forward.
The problem is, your request is so vague that people are reluctant to help.
One possible solution (although it might not fit what you need) would be:
1) Create a ListActivity
2) Create a custom SimpleCursorAdapter.
3) Create a custom xml view for each row.
4) override SimpleCursorAdapter's getView() to inflate said xml view
5) Override SimpleCursorAdapter's bindView() method to assign values from the cursor to the row.
The questions in my mind which stopped me from trying to code this for you are:
1) Might you want to change the number of columns dynamically?
2) Might you want more flexibility as to how the column widths are decided upon? etc..
A slightly more complex setup would use TableLayout and TableRow.
Related
I am asked to build a survey application on Android that may contain questions from the users, the question might be changed after some time and their answers as well.
the nature of answer might get in form of radio, checkboxes, drop-down or plain text.
I want to do this through SQL database. it will get the question from Master table and answer Option(s) from detail table and dynamically designs the activity layout accordingly.
Is that possible or is there any other way to do the same.
This can be done in many ways. You can create custom views for each type of layout and inflate the required view based on the type question you fetch from the Database.
You can also just use simple XML layouts instead of the custom views. The benefit of using custom views will be that you can predefine events and other things within the view which makes it easy to reuse.
Here are a couple of tutorials to give you an idea.
http://www.vogella.com/tutorials/AndroidCustomViews/article.html#tutorial_compoundcontrols3
https://www.intertech.com/Blog/android-custom-view-tutorial-part-1-combining-existing-views/
In all honesty you're not going to get any answer on here that provides you with nearly enough detail for how to do this if you're not sure how to start. It's simple to breakdown but a lot more complex than you realise.
However.. essentially you need to:
Create a DB table that contains questions.
Create a DB table that contains potential answers options. If answers
can be used for multiple questions, consider handling this
appropriately.
The question table should contain a column that details the question
type, i.e. radio button, checkbox etc...
The question table should link to another table which provides links
to the potential answers.
The above really needs to be well designed before you code anything. Think it through thoroughly and only use the above as a guide.
Your UI should be dynamic. Use something such as list/recycler view that will allow you to insert rows of data. You can then create XML layouts for checkbox answers, radio answers and insert these as appropriate.
Again, you need to consider, will you display all questions/answers in a list, will they be displayed one by one and moved onto... in a wizard style approach.
As I say, you will not get a fully detailed answer because a lot of this isn't coding, it's thinking and designing.
I hope the above helps you to get a rough and basic understanding of what you need to do and how to approach it. Don't be under any illusion that this is going to be quick to do. Spend quite a bit of time before coding to design this, because, coding it is easy... the design which is the most important part here is what will take the time.
I have done one project that consists only dynamic questions and answers like you.First You have to create Layout programmatically.
RelativeLayout layout = new RelativeLayout();
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT);
Add your rule to that layout.
params.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
Then create your view and then set properties like below.
EditText edittext = new EditText()
editext.setId(1);
editext.setInputType(InputType.TYPE_CLASS_TEXT);
editext.setGravity(Gravity.TOP);
Thats' all you created a view programmatically. Then you can add to you child to the parent view.
layout.addView(edittext);
You can create multiple layouts and multiple views by doing this. just think about it. Don't forget to add your child Layout to your parent Layout. Keep coding..
I am trying to create a candy crush like game. Correct me if I'm wrong, but the way I see the basic logic of candy crush, several rows and columns were created and objects (e.g. candies) are placed on each box, forming a pattern will erase previous objects and will be replaced by a new one. My question is, shall I use a grid or table layout? Shall I create an array for objects (e.g. food, crackers) where it will be placed? are buttons in android shall be used to swap it with different objects?
Thanks for the help. Android newbie here. :)
Definitely check out the official docs for your use-case.
GridLayout: http://developer.android.com/reference/android/widget/GridLayout.html
TableLayout: http://developer.android.com/reference/android/widget/TableLayout.html
I'm not familiar with the candy crush game but from your description I would use a Table Layout as it looks like every item you'll have will stay in its' assigned cell. GridLayouts are very helpful if you have Views that extend past their cell boundaries. Just in case you should decide to use a GridLayout here's a wonderful article on them: http://android-developers.blogspot.com/2011/11/new-layout-widgets-space-and-gridlayout.html
"Shall I create an array for objects (e.g. food, crackers) where it will be placed?" - That sounds like a fine thing to do to me!
"are buttons in android shall be used to swap it with different objects?" - Buttons are simply a type of View, replacing a Button with some other View inside a TableLayout should be pretty straight forward.
I know this was asked forever ago but maybe this could help someone? GL!
I have a post to server which returns a JSONArray. I then populate a TableLayout by dynamically creating TableRows which get inserted into the TableLayout, and also dynamically create TextViews after which I set the text to values from the JSONArray. And add the TextViews to the Rows.
This does work. Although it seem terribly insufficient and I would really like a more elegant solution.
Any suggestions?
You are right, TableLayout is very inefficient, consider you have 100 rows of content, then you have to create 100 rows of widgets.. which is very memory consuming + slow in creation.
Above scenario should be a perfect fit for using ListView, you can simply de-serialize your JSON Array into a custom Adapter to push to the ListView. I usually create/use a JSON-Cursor Adapter so that it can be more reusable (fits to ContentProvider as well).
http://developer.android.com is the most efficient way to learn android...
check out this url for your result :
http://developer.android.com/guide/topics/ui/declaring-layout.html#CommonLayouts
In my app I download from the net some data I need to arrange in a table way.
I already know dimensions: about 26 rows and 6 columns, anyway data can change across subsequent calls so I have to create this table in a dynamic way.
As it is a table I though using a TableLayout (never used before), on the other hand I could simply use a list view, using for the single item a 6 elements linear layout with horizontal orientation.
ListView intrigues me because it is an adapter view (I think I'll store this data in a sqllite db, later) so populating it is quite simple and straightforward, anyway I don't know if it is the best for my problem.
What is your opinion?TableLayout, ListView or other?
When it comes to performance, I think you should use ListView because ListView support Recycling of view and you can check this and this post for more detail regarding Recycling of view.
Hope it helps :)
A ListView is a collection of layouts that are automatically displayed one below the other. The developer adds new items through a piece of code in the Activity. An example is the address book where you have lots of contacts one after the other.
A TableView on the other hand is defined in the layout file and does not only need to contain elements below or above each other but can also arrange elements on the left or on the right. A TableView is exactly what it says: A table with rows and columns.
here's a similar question that might answer what you want to know
Ok I can't find a sample anywhere.
I've done the notepad tutorial on Google's Android site, but I would like to know how to add more fields to the list, in columns. At the moment I can add the columns no problem, but they're not aligned like you would a normal table on the layout:
john smith
heinrich cilliers
will peck
I would like the first names and last names aligned proportionately, as you would in an html table.
It works if I use a constant value for the layout_width parameter (100dip etc), but I would prefer to use a relative percentage. However it's becoming clear that each row is on it's own, and does not know how to alighn itself with the row above.
Any pointers?
UPDATE: I've reached an experienced Android developer which advised me to use WeightSum, which brings me closer, but vertical alignment is stil not happening:
I think therefor you have to extend the ListAdapter.
http://developer.android.com/reference/android/app/ListActivity.html