SQLite and MySQL - android

I'm working on an android application for my final year project. My app is about storing information and then display it out on the phone.
I figure it out by using sqlite, I'm able to store data in it, as well as retrieve it by using cursor c. But how am I able to make the retrieve data visible to the user?
example: I'm creating an app about Burger King, and I click the information about Burger King, it shows the information (when was is created etc) to the user.

This is kind of an open-ended question. If you want to present your search results to the user as a list or a grid, then use a ListView or a Gridview, and attach a CursorAdapter to your View. The CursorAdapter will read the results out of the database and provide them to the View. You'll probably want to subclass your CursorAdapter so that it generates the appropriate views for your ListView/GridView to display.
It's kind of an advanced topic, but I'm sure you can find tutorials and code samples.
If you just want to display a single data item on the screen (presumably a data item with a lot of detail), then you'll probably want to define a Dialog or Activity to present it to the user, and then read the data from the cursor and load it into the various View objects in your Dialog/Activity.
I hope this helps. It would be useful if we knew more detail about what you're trying to do.

I agree with Edward Falk on the need for more clearly defined details. In answering your question I would suggest creating some TextViews or a ListView in your xml file like so:
<?xml version="1.0" encoding="utf-8"?>
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/fragmentLayout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:paddingLeft="8dp"
android:paddingRight="8dp" >
<TextView
android:id="#+id/viewl"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/viewvaluesLabel"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="#+id/view2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/viewvaluesLabel"
android:textAppearance="?android:attr/textAppearanceLarge" />
.
.
.
</LinearLayout>
</ScrollView>
You can then get these TextViews by their respective ids using a "R.find" statement like:
TextView view1 = (TextView)findViewById(R.id.view1);
TextView view2 = (TextView)findViewById(R.id.view2);
TextView view3 = (TextView)findViewById(R.id.view3);
and then set the data you retrieve from your database table to display in these views using code like this:
Cursor c = db.getDetails(id);
if (c.moveToFirst()) {
view1.setText(c.getString(1));
view2.setText(c.getString(2));
view3.setText(c.getString(3));
} else {
db.close();
}
Hope that helps.

Related

Android auto reduce data table

I am new to Android, and I have a table I made using:
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/question_list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="5dp"
android:orientation="vertical"
android:layout_below="#+id/textView7"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_above="#+id/linearLayout">
</TableLayout>
added rows to it and it perfectly shows the table. Now I want to implement a search box that reduce the result as I type, I am not sure how to do that. I tried searching, maybe I am missing the term to search. Can someone please add a link to it and/or tell me some technique, because re-populating the table everything someone types is like a bad thing to do.
PS: Data is sent from API as JSON, and I own the API.
Displaying large information in TableLayout is bad practice. You can use ListView. Because, every time when text changed in your search box, you need to clear all views in TableLayout, then you need to create rows that appropriate your search filter and then you need to add these rows to TableLayout again. Creating many views and drawing them on screen is hard process for Android and it can crash your app.

Simple non interactive ListView Android

Please cope with me as im new to android and i have done a lot of searching on my own, about this, and im fed up not finding a proper way.
This is my custom listview template. rowbuttonlayout.xml.
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TextView
android:id="#+id/label"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#+id/label"
android:textSize="19.6dp" >
</TextView>
<CheckBox
android:id="#+id/check"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_marginLeft="3.6dp"
android:layout_marginRight="9.6dp" >
</CheckBox>
<TextView
android:id="#+id/result"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/check"
android:layout_alignBottom="#+id/check"
android:layout_toLeftOf="#+id/check"
android:text="#string/nothing" />
</RelativeLayout>
What i want is to create something like this in this pic without the header and footer.
http://www.vogella.com/articles/AndroidListView/images/xinteractive10.png.pagespeed.ic.V0cVgp99SN.png
As on my layout ill be having another textview just before the checkbox as well. Also ill be adding a dynamic button at the footer.
Now i dont want the users to interact with my listview. Only the button should be clickable and when they click it i want to run some calculations and show them in the relevant "result" textview and to check the checkbox next to it to show that i have calculated it. Meaning i have to access each and every "result" textview and "check" checkbox in the code.
Please provide me some guidance for coding this app. Im fed up looking through very complex codes trying learn on my own. Many many thanks for your help.
Shavinka
Check out below links...
ListView + CheckBox =?
How to get checked items from android listview?
http://sunil-android.blogspot.in/2013/04/android-listview-checkbox-example.html
http://fundoocode.net/android-listview-checkbox-example-onitemclicklistener-and-onclicklistener/
http://www.mysamplecode.com/2012/07/android-listview-checkbox-example.html

Best solution to merge/include an xml layout file multiple times in an activity layout

I'm building a flashcards app as a college project, and wanted a horizontally scrolling display of the cards. I've built an xml file for the flashcard design itself:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/relativeLayout1"
android:layout_width="fill_parent"
android:layout_height="fill_parent" android:background="#drawable/whitenote" android:padding="3dp">
<ImageButton
android:id="#+id/imageButtonPins"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" android:background="#color/transparent" android:src="#drawable/pinselector"/>
<TextView
android:id="#+id/textViewWord"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/imageButton1"
android:layout_centerHorizontal="true"
android:text="Word" android:textColor="#color/black" android:paddingTop="30dp" android:textSize="20dp"/>
<TextView
android:id="#+id/textViewMeaning"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/textViewWord"
android:layout_centerHorizontal="true"
android:text="Meaning" android:textColor="#color/black"/>
</RelativeLayout>
I've also created the Class file.
In the activity layout, I have a few more elements and as such a linearlayout at the root level is necessary. I've been able to display a single flashcard for test purposes using and using layout inflater.
Question
In both the ways, in and layout inflater I've been unable to get the ImageButton working. My question is how do I get the button to work.
Update: Managed to get the ImageButton working using . Realised that I have to handle the onclick event in the activity, and not the Custom Adapter class. This should allow me to obtain the words too, as long as I can keep track of the "current" flashcard on display
Also, whats the best way to handle the scrolling for a flashcard app? My current plans so far is to use a HorizontalScrollView and customise it a bit, because I need (a) a swipe should make the flashcard move only to the next one (b) I need to focus on the "current" flashcard since I need some data from its children views (ie, the word).
Are you considering Fragments?
You can get some help with the ViewPager here.This is supported in Android 3.0 or above or Android 1.6 with the compatibility package.
http://geekyouup.blogspot.com/2011/07/viewpager-example-from-paug.html
If you do not wish to use the fragments, you can simply use the Gallery. This way, you can achieve the horizontal scrolling. (like in the Amazon app) without complex ViewPager.
For the second part of your question, take a look at the ViewPager.
A HorizontalScrollView or a Gallery are probably the most direct way of implementing this. I don't use Gallery-- but it is good to at least know it exists.
If you want a much more robust implementation, I agree with dcanh121 and think you should check out a Fragment based ViewPager. This will allow more options than just a View , but might be overkill depending on the goal. A fragment is basically the bizarre offspring of an Activity and a View, but don't quote me on that.
Also,
Inflating layouts is costly, so try to only inflate the XML into a View once, and reuse that View object. Try not to re-inflate the XML every time a new flashcard is drawn.

Custom listview with two buttons in each row

I have spent literally two days trying to sort this issue. If anyone could help I would be massively appreciative.
What I'm trying to achieve:
Have a ListView, whereby the player can add new entries (players), through a text field (for the player name), and then a submit button. In each field of the ListView, I display the player name, and then two ImageButtons. One with a male symbol, and one with a female symbol. The male symbol is toggled by default, and the user can set the player as being male or female by toggling either the male button or the female button. Finally, once the user moves onto the next screen (a new activity), the application will save the player names and the attached sex to some form of storage and proceed to the next activity.
What I have achieved:
I have a simple array adapter, which upon the player adding a new player name to the list, I run the notifyDataSetChanged() on it. The adapter also is set to use a custom row layout file. Inside the layout file, it looks like this:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:layout_height="wrap_content">
<RelativeLayout android:layout_height="wrap_content"
android:layout_width="fill_parent" android:id="#+id/relativeLayout1" android:layout_marginTop="5dp">
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content" android:id="#+id/playerName"
android:layout_centerVertical="true" android:text="Derp" android:textStyle="bold" android:layout_marginLeft="5dp" android:textSize="22dp" android:textColor="#color/male_blue"></TextView>
<ImageButton android:layout_height="60dp"
android:layout_width="60dp" android:onClick="maleClickHandler"
android:src="#drawable/male_icon_settings" android:id="#+id/buttonA" android:layout_alignParentRight="true" android:layout_marginRight="65dp"></ImageButton>
<ImageButton android:onClick="femaleClickHandler"
android:layout_height="60dp" android:layout_width="60dp" android:id="#+id/buttonB"
android:layout_alignParentRight="true" android:layout_marginRight="5dp" android:src="#drawable/female_icon_settings"></ImageButton>
</RelativeLayout>
</LinearLayout>
The two buttons on each row reference to methods in the class file. Here is my code for this:
public void maleClickHandler(View v) {
RelativeLayout vwParentRow = (RelativeLayout) v.getParent();
ImageButton maleButton = (ImageButton) vwParentRow.getChildAt(1);
maleButton.setImageDrawable(getResources().getDrawable(
R.drawable.male_icon_selected));
vwParentRow.refreshDrawableState();
}
public void femaleClickHandler(View v) {
RelativeLayout vwParentRow = (RelativeLayout) v.getParent();
ImageButton femaleButton = (ImageButton) vwParentRow.getChildAt(2);
femaleButton.setImageDrawable(getResources().getDrawable(
R.drawable.female_icon_selected));
vwParentRow.refreshDrawableState();
}
I haven't yet implemented any inter-connectivity between these two buttons, to allow only one to be active at a time, or to even untoggle one, since I think I might be taking the wrong approach entirely.
The problem:
Upon adding new entries to the list, AFTER toggling one and/or the other male/female buttons, things get really buggy, and the male/female toggled icon might move as it should, along with the attached player string, or more likely, those toggled will stay on that first row (array position 0 of the list), or even move into the second list position, AND copy themselves as being toggled onto the row above.
How you can help...?
I have attached an image below of my screen, from the emulator, to help illustrate my points
Screenshot!
I think that I might need to use some form of custom adapter; I have done so much reading around on the subject, but I can't find anything relevant to what I am trying to achieve, so if you could point me in the right direction, or even try and put together the most basic solution to this type of problem, I would be very grateful.
Finally, when I get this working, which form of storage would be best for storing player names, and their sex? I would like the user to be able to keep the player list after they quit the application and restarted it.
Thanks for any help! :)
You will need to use a Custom Adapter, which in itself should be able to track the male/female flag for each of it's entries.
Your method will not work since the state of the buttons are managed by the getView method of the adapter. Even if you change them by digging through the children, the next time when the getView method is called, it's going to mess up things.
A lot of this depends on how many players you expect to have in your game. If it's a number that would likely fit on one screen (or very close to it), the ListView is actually unnecessary. ListViews and adapters aren't really a convenience method as much as they are a tool to improve performance. They only keep in memory what is on the screen and recycle old, already-displayed Views for new rows when you scroll--this is why some of your button states are being copied to different rows.
There are a couple of ways you could fix this:
You could write a custom adapter yourself as Kumar Bibek suggests. In this adapter, you would want to override the getView() method to make sure each button has the correct state each time the method is called.
You could also simply use a ScrollView populated with a few of your rows manually if you don't have enough data to warrant using a ListView. This way you wouldn't need to worry about your rows being recycled and button states being out of wack.
In addition, you might want to look into using a RadioGroup for the gender selector (I can't think of a much better use for radio buttons since they are made to be mutually exclusive).
Also, the outer LinearLayout in your row XML file looks unnecessary.
As far as storage, you could either use an SQLite database or SharedPreferences. SharedPreferences requires no setup, but I feel like an SQLite database is more suited to your needs.

How to list view whole row selectable in android?

I created a sample application from Android sample tutorial and completed all 3 exercises for Notepad. I am able to add and edit notes created. However when I try to edit the notes I am force to select the text title for editing. If I click on to the right of the text, basically any where else In the same row I am not able to edit it.
I would like to make the whole row to be select-able/editable. Please let me know how to do this. I tried to change the notes_row.xml width="fill_parent". But this did not work out. Please help me out with this. Thank you.
change your notes_row.xml file
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content" android:layout_height="wrap_content">
<TextView android:id="#+id/text1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="TextView">
</TextView>
</RelativeLayout>

Categories

Resources