emptyView always showing even if list is not empty - java

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 .

Related

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.

Elements below the expandable list - not floating footer

Is there any option to put elements below the expandable list, let me explain: I already implemented elements below the list, but they are behaving like footer - floating at the bottom of the screen.
But if i place elements below the list in xml, elements become hidden when any group is expanded.
at the moment my layout is behaving like this
http://shrani.si/f/2s/ba/ga2VKfi/lay2.jpg
but i want to be like this at the end of the list
http://shrani.si/f/42/4m/2GAcNu6T/lay3.jpg
My layout at the moment
<LinearLayout
android:id="#+id/linearLayoutListExer"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dip"
android:background="#ffffff"
android:gravity="center_vertical"
android:orientation="vertical">
<ExpandableListView
android:id="#+id/ExerciseELV"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/linearLayoutStatus"
android:layout_weight="1"
android:groupIndicator="#null" />
<LinearLayout
android:id="#+id/relativeFooter"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#id/linearLayoutListExer"
android:background="#ffffff"
android:orientation="vertical">
<TextView
android:id="#+id/textViewBellowExercise"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="10dip"
android:text="The following exercises will help you lose body weight"
android:textAppearance="?android:attr/textAppearanceSmall"
/>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#id/textViewBellowExercise">
<Button
android:id="#+id/buttonFaceit"
android:layout_width="0dp"
android:layout_height="20dp"
android:layout_margin="5dp"
android:layout_weight="1"
android:background="#color/green"
android:text="FaceIT!"
android:textColor="#color/white" />
<Button
android:id="#+id/buttonChallenge"
android:layout_width="0dp"
android:layout_height="20dp"
android:layout_margin="5dp"
android:layout_weight="1"
android:background="#color/red"
android:text="Challenge"
android:textColor="#color/white" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
Your following property should change.
Instead of
android:layout_below="#id/linearLayoutListExer"
you should write
android:layout_below="#id/ExerciseELV"
for the LinearLayout with android:id="#+id/relativeFooter"
This will make the bottom layout appear below the listview
I'm new to android so i'm sorry for any "stupid" questions i may be asking.
I ended up using addfooterview method which works great.
You need to separate footer layout in its own xml and then inflate it and add it to expandable listview.
View footer=inflater.inflate(R.layout.activity_main_footer,null);
elv.addFooterView(footer);

Is there a more systematic way to populate my layout?

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)

RelativeLayout Params addRule without ID

I want to add the following rule to a TextView within my RelativeLayout
TextView anyView = new TextView(anyContext);
RelativeLayout.LayoutParams layout;
..
layout.addRule(RelativeLayout.LEFT_OF, anyView.getID());
This only works if I add the ID of the element. The problem is that I do not have and do not want to have any IDs specified, because my view can be used multiple times within the same ViewGroup later.
So either I have to assign that rule by reference or I need to create new IDs dynamically so that I don't create multiple Views with the same ID.
Any suggestions what to do in this situation?
I tried to copy from this xml:
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<GridLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_alignParentRight="true"
android:id="#+id/gridLayout">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="A lot of text for some width"/>
</GridLayout>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="123"
android:layout_centerVertical="true"
android:gravity="center_horizontal"
android:layout_toLeftOf="#+id/gridLayout"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
</RelativeLayout>
try this
RelativeLayout.LayoutParams pAC = new RelativeLayout.LayoutParams(
your width,
your height);
pAC.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
anyView .setLayoutParams(pAC);

Add xml dynamically to layout

I have a (complex) RelativeLayout with a few TextViews, ImageViews and a Button. The amount of how often this layout is created depends on data from the database. I get this data from a list and want to show it dynamically, what have I to do?
xml to add dynamically:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/zusatz_content" >
<TextView
android:id="#+id/text1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginTop="20dp"
android:layout_marginRight="15dp"
android:layout_marginLeft="15dp"
android:text="Lorem Ipsum 1" />
<Button
android:id="#+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/text2"
android:layout_centerHorizontal="true"
android:layout_marginTop="20dp"
android:text="#string/button_desc" />
<ImageView
android:id="#+id/image"
android:layout_width="300dp"
android:layout_height="wrap_content"
android:layout_below="#+id/text1"
android:layout_centerHorizontal="true"
android:contentDescription="#string/ima_zusatz_desc"
android:src="#drawable/nix" />
<TextView
android:id="#+id/text2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/text1"
android:layout_below="#+id/image"
android:text="Lorem Ipsum 2" />
Notice: The texts and image should be changed dynamically by the content from the database.
thanks, tomtom
Besides your XML, you will need an Adaptor and a ListView of some kind. In your Adapter you will have to implement
public View getView(int position, View convertView, ViewGroup parent)
In this method, return your new view (Probably created with LayoutInflator and your xml) and update that data displayed on each button/text view.
Make sure to research how convertView works. If your lists are long, the performance boost from using convertView properly is huge. If you get this correct, Android's listview and baseadpater will do the hard work for you.

Categories