Is there a more systematic way to populate my layout? - java

I'm working on the layout as shown below.
So far I've had success with manually defining each View in xml (RelativeLayout within a ScrollView) and manually populating each spinner in java. But this is tedious. I wish I could somehow use "I have this array of items. For each item I want a row on the screen with a spinner and an EditText." I must then be able to grab the user input to put into a database. Eventually, I'll want to be able to come back and edit this screen, which means I'll have to repopulate these fields from a database query.
I tried working some with a ListView, but had trouble populating multiple rows and then read that RelativeLayout is supposed to perform better. What would you suggest? If I have to declare every field in xml and java manually, then so be it.
This is what my XML looks like...
<ScrollView
android:id="#+id/scrollView1"
android:layout_width="match_parent"
android:layout_height="180dp" >
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="match_parent" >
<Spinner
android:id="#+id/category_spinner1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
/>
<EditText
android:id="#+id/editWeight1"
android:layout_width="70dp"
android:layout_height="wrap_content"
android:layout_toLeftOf="#+id/percentSign1"
android:layout_alignBaseline="#id/category_spinner1"
android:inputType="numberDecimal" >
</EditText>
<TextView
android:id="#id/percentSign1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignBaseline="#id/category_spinner1"
android:text="#string/percentSign"
android:textSize="20sp" >
</TextView>
<Spinner
android:id="#+id/category_spinner2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#id/category_spinner1"
/>
<EditText
android:id="#+id/editWeight2"
android:layout_width="70dp"
android:layout_height="wrap_content"
android:layout_toLeftOf="#+id/percentSign2"
android:layout_alignBaseline="#id/category_spinner2"
android:inputType="numberDecimal" >
</EditText>
<TextView
android:id="#id/percentSign2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignBaseline="#id/category_spinner2"
android:text="#string/percentSign"
android:textSize="20sp" >
</TextView>
...
And my Java...
Spinner catSpin1 = (Spinner) rootView.findViewById(R.id.category_spinner1);
ArrayAdapter<String> catSpinAdapter1 = new ArrayAdapter<String>(this.getActivity(),android.R.layout.simple_spinner_item, DBAdapter.gradeTypes);
catSpinAdapter1.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
catSpin1.setAdapter(catSpinAdapter1);
catSpin1.setSelection(0);
Spinner catSpin2 = (Spinner) rootView.findViewById(R.id.category_spinner2);
ArrayAdapter<String> catSpinAdapter2 = new ArrayAdapter<String>(this.getActivity(),android.R.layout.simple_spinner_item, DBAdapter.gradeTypes);
catSpinAdapter2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
catSpin2.setAdapter(catSpinAdapter2);
catSpin2.setSelection(1);
Spinner catSpin3 = (Spinner) rootView.findViewById(R.id.category_spinner3);
ArrayAdapter<String> catSpinAdapter3 = new ArrayAdapter<String>(this.getActivity(),android.R.layout.simple_spinner_item, DBAdapter.gradeTypes);
catSpinAdapter3.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
catSpin3.setAdapter(catSpinAdapter3);
catSpin3.setSelection(2);
...

Sure, create a String array, for example:
<string-array name="my_array">
<item>One</item>
<item>Two</item>
</string-array>
Then add it to te spinner on the xml this way:
<Spinner
android:id="#+id/spinner"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:drawSelectorOnTop="true"
android:entries="#array/my_array"/>

My first intention would also have been a ListView, you could create a simple datastructure like a class with two string values that will get updated/displayed by this spinner/edittext combination and you could use this class also to persist it in your database.
Some side note: You should have a look at re-using layouts: http://developer.android.com/training/improving-layouts/reusing-layouts.html
That would have made your approach much easier. (define one spinner/edittext layout and re-use it inside your main xml)

Related

How can I repeat textview in tablerow with for iteration?

I want to make the textview column in tablerow iterated because I want to make table column dynamically based on data in database. I know how to make it with php, but in android, its so confusing for me that is a beginner. here's my code:
<TableLayout
android:layout_width="1000dp"
android:layout_height="wrap_content"
android:id="#+id/kriteriaalternatif"
android:layout_marginTop="10dp">
<TableRow
android:id="#+id/rowlayb"
android:background="#3c5ca0"
android:padding="5dp">
<TextView
android:id="#+id/colalterkrit"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:text="Alternatif/Kriteria"
android:textColor="#FFFFFF"
android:layout_gravity="center_horizontal"/>
<TextView
android:id="#+id/kriteriab"
android:layout_width="150dp"
android:layout_height="wrap_content"
android:textColor="#FFFFFF"
android:layout_gravity="center_horizontal"/>
</TableRow>
</TableLayout>
I want the textview with kriteriab id iterated dynamically. I hope there is a solution, its quite frustrating. thank you in advance.

Widget ListView loading view

I'm trying to display a dynamic list on a widget and so far i've been able to display my 3 headers (which are simply textview inside a linearlayout) but for my items (2 textview in one linearlayout inside another linear layout) the view displays Loading... instead of the content (a task label).
I've already bumped getViewTypeCount to 2 in my RemoteViewsFactory to match the number of views returned by the getViewAt.
I can't figure out what is wrong inside this layout that would cause a problem:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:paddingTop="3dp"
android:clickable="true"
android:focusable="true"
style="#style/SelectableItemBackground"
android:id="#+id/widget_item"
>
<LinearLayout
android:paddingLeft="22dp"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="•"
android:paddingRight="6dp"
android:textSize="17dp"
android:textColor="#color/colorText" />
<TextView
android:id="#+id/widget_item_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="17dp"
android:textColor="#color/colorText" />
</LinearLayout>
Thank you for reading :)
To anyone reading this, i have been able to solve it by suppressing the style attribute

emptyView always showing even if list is not empty

I wish someone could take a look on that and let me know what is wrong.this is my emptyView container in the activity_main
<ListView
android:id="#+id/display_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<RelativeLayout
android:id="#+id/empty_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true">
<TextView
android:id="#+id/empty_shelter_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:fontFamily="sans-serif-medium"
android:paddingTop="16dp"
android:text="#string/empty_view_title_text"
android:textAppearance="?android:textAppearanceMedium" />
<TextView
android:id="#+id/empty_subtitle_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/empty_shelter_title"
android:layout_centerHorizontal="true"
android:fontFamily="sans-serif"
android:paddingTop="8dp"
android:text="#string/empty_view_sub_text"
android:textAppearance="?android:textAppearanceMedium"
android:textColor="#A2AAB0" />
</RelativeLayout>
I have set emptyView in each fragment, and even though when items are showing in the list it is still showing the empty view fields.
//set empty activity
ListView listView = rootView.findViewById(R.id.display_view);
View emptyView = rootView.findViewById(R.id.empty_view);
listView.setEmptyView(emptyView);
//create a new cursor adapter
mCursorAdapter = new PlatformCursorAdapter(getActivity(),null);
listView.setAdapter(mCursorAdapter);
I will highly appreciate it if you can tell me what i did wrong.
Try hiding the empty_view in xml.
<RelativeLayout
android:id="#+id/empty_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="gone"
android:layout_centerInParent="true">
my friends you have a send a null in adapter. send a list
mCursorAdapter = new PlatformCursorAdapter(getActivity(),null);
and other way are used to check a listview is null hide the
display_view view and show the empty_view .

diffculty choosing an item from a list view

Hello I've built a list view that in its XML file have 2 layouts with background and 1 text view on each layout,I'm getting from the strings array text, And changing the text view,this is my code for the row I'm getting for for the list view:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="wrap_content"
android:weightSum="2"
android:orientation="horizontal">
<RelativeLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:background="#drawable/purple"
android:layout_weight="1"
android:id="#+id/rl1">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Large Text"
android:id="#+id/textView"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
android:textColor="#ffffff" />
</RelativeLayout>
<RelativeLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:background="#drawable/purple"
android:layout_weight="1"
android:id="#+id/rl2">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Large Text"
android:id="#+id/textView2"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
android:textColor="#ffffff" />
</RelativeLayout>
</LinearLayout>
Now my problem is when I'm trying to choose only one of the boxes on list view on item selected and doing a switch on the positions I'm getting the 2 boxes on the same position so it don't matter what what box I'm choosing, Iv'e try to give the linear layouts id and then use set on click listener on them with no successes,I'll appreciate and suggestions.
I'm not sure I understand.
Do you use this layout to create ListView items ?
If that is the case, the problem may be that when the ListView is refreshed, it inflates instances of this layout (in its default configuration, the components are in their default position). In order to preserve the changes, you will need to save each item's state and modify its layout every time the list is refreshed.

Display data from database in listview and delete checked item

Can I know how to display the name of item from database with checkbox beside which enable the users to delete the item when the checkbox is ticked.
I have gone through several articles that mentioned about this but most of the articles are not displaying the data from database. They mentioned about the adapter but I am not very sure how does it function.
Can anybody explain to me:
how does the adapter work between listview, textview and checkbox
how to detect and delete the selected item when the checkbox is ticked
Below are the two different .xml that I have done:::
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/wishlistln"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:padding="10dp" >
<CheckBox
android:id="#+id/wishlistcb"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:text="" />
<TextView
android:id="#+id/wishlist"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_marginLeft="10dp"
android:textSize="16sp" />
</LinearLayout>
<ListView
android:id="#android:id/list"
android:layout_width="wrap_content"
android:layout_height="400dp" />
Thank you.
here a nice tutorial on ListView and Adapter, it show how to display data and how to handle events on the ListView

Categories