android - filling ListView with array - android

I can't fill a ListView layout with an array of strings. Here is the Activity:
ListView symptomList = (ListView) findViewById(R.id.ListView_Symptom);
String symptomsArray[] = new String[1024];
// Then I fill up symptomsArray
ArrayAdapter<String> adapt = new ArrayAdapter<String>(this, R.layout.menu_item, symptomsArray);
symptomList.setAdapter(adapt);
Here is menu_item:
<TextView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:textSize="#dimen/menu_item_size"
android:text="test string"
android:layout_gravity="center_horizontal"
android:layout_height="wrap_content"
android:shadowRadius="5"
android:gravity="center"
android:shadowDy="3"
android:shadowDx="3" />
And here is symptom.xml -
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ListView
android:layout_height="wrap_content"
android:id="#+id/ListView_Symptom"
android:layout_width="fill_parent"
android:divider="#drawable/divider"
android:listSelector="#drawable/textured"
android:layout_alignParentTop="true"></ListView>
</LinearLayout>
Does anyone have any idea what is wrong? Does my public class need to extend "ListActivity". I tried that and that didn't work.

Try stripping out some of the presentational bits from your TextView. For example, I don't think that you want android:layout_gravity="center" there. Also, try setting your ListView's android:layout_height="fill_parent".

Turns out Eclipse is so slow running on my 5 GB Dram MacPro that it eventually worked and displayed the array in the ListView, it just took a few minutes! Sorry about the confusion.

Related

ListView in RelativeLayout not displaying

I'm writing an application and have come across a bit of an issue.
When working with a layout file, I have a 2 RelativeLayouts inside of a ViewFlipper.
The idea is that this page in particular is a welcome screen. The first RelativeLayout "welcomes" you to the app, and upon pushing a button, the user is led to the second RelativeLayout. In this layout there is a search bar that will allow the user to search for a certain criteria (specific to the app, not important) then display the results in a ListView.
Everything works correctly, but displaying the ListView seems to have some problems. The adapter is set properly, and I tested the method on another ListView in a test layout, and it worked fine. However something seems wrong with the ListView in the RelativeLayout. Here is the layout code
activity_welcome.xml
<ViewFlipper xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/welcomeFlipper"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:animateLayoutChanges="true"
android:background="#color/white"
tools:context="com.jacemcpherson.announcer.WelcomeActivity">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/white"
android:padding="40dp">
...
<!-- This code irrelevant, all's well :) -->
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:animateLayoutChanges="true"
android:background="#color/white"
android:padding="40dp">
<TextView
android:id="#+id/textFirstThingsFirst"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/textYouNeedToSearch"
android:layout_centerHorizontal="true"
android:text="#string/first_things_first"
android:textColor="#color/app_color"
android:textSize="32sp" />
<TextView
android:id="#+id/textYouNeedToSearch"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:layout_margin="10dp"
android:gravity="center"
android:text="#string/you_need_to_search_for_your_school"
android:textColor="#color/black"
android:textSize="18sp" />
<EditText
android:id="#+id/schoolSearchEditText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/textYouNeedToSearch"
android:hint="#string/search_hint" />
<ProgressBar
android:id="#+id/searchListProgressBar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/schoolSearchEditText"
android:layout_centerHorizontal="true"
android:layout_marginTop="20dp"
android:visibility="gone" />
<!-- This is the problem area -->
<ListView
android:id="#+id/searchResultsList"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_below="#+id/schoolSearchEditText" />
</RelativeLayout>
</ViewFlipper>
I'm setting the adapter works like normal, but just in case I missed something, here's the line of code for that...
mListView.setAdapter(new ArrayAdapter<String>(
mContext,
android.R.layout.simple_list_item_1,
result // this is the array of results to be displayed in the list.
));
Thank you for your help, if I've missed something that in some way makes this unanswerable without further information, please let me know.
Change the layout_height from wrap_content to either match_parent or fill_parent.
This is explained very well by this answer.

Spinners not populated with text

I am using a spinner in several places in my program, but I will stick with one case.
I have two xml files -- small_new_system and new_system. They both have the a spinner that is named state_spinner.
The odd thing is that when I use this code on a tablet running 3.2, which uses new_system, they are displayed but when I put the app on my phone that is running 2.1, which uses small_new_system, they do not show up. The items are in the spinner list, but there is no text being displayed. I have tried naming the spinners differently, as well as not using a custom spinner layout.
The other odd thing is that when I use the identical layouts, which do not look very good, on the small device they are not populated with the text either.
Thank you for any help! My code is as follows:
Code to populate the spinner:
states = (Spinner) findViewById(R.id.state_spinner);
ArrayAdapter<CharSequence> adapter = ArrayAdapter
.createFromResource(this, R.array.states,
R.layout.spinner_layout);
adapter.setDropDownViewResource(R.layout.spinner_layout);
states.setAdapter(adapter);
states.setOnItemSelectedListener(new MyItemsOnSelectListener());`
Spinner in small_new_system:
<Spinner
android:id="#+id/state_spinner"
android:layout_width="200dp"
android:layout_height="55dp"
android:layout_below="#+id/city_edit"
android:layout_margin="5dp"
android:layout_alignParentRight="true"
android:inputType="textPersonName"
android:textSize="40dp" >
</Spinner>
Spinner in new_system:
<Spinner
android:id="#+id/state_spinner"
android:layout_width="500dp"
android:layout_height="75dp"
android:layout_below="#+id/city_edit"
android:layout_margin="10dp"
android:layout_alignParentRight="true"
android:textSize="60dp" >
</Spinner>`
Code in custom spinner:
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/spinnerTarget"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="left"
android:textColor="#00000000"
android:textSize="40dp" >
</TextView>`
Try removing:
android:inputType="textPersonName"
from small_new_system. Then it would look like:
<Spinner
android:id="#+id/state_spinner"
android:layout_width="200dp"
android:layout_height="55dp"
android:layout_below="#+id/city_edit"
android:layout_margin="5dp"
android:layout_alignParentRight="true"
android:textSize="40dp" >
</Spinner>
Or you might try adding a background color:
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/spinnerTarget"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="left"
android:textColor="#00000000"
android:background="#FFFFFFFF"
android:textSize="40dp" />
The solution I came up with was to use android.R.layout.simple_dropdown_item for my spinner when an sdk less than 11 is detected, and when the sdk is greater than 11 I use my custom spinner. This seems to work ok. Not my favorite solution, but it does work.
Try this one ..
spnPlan = (Spinner) findViewById(R.id.set_spinner1);
ArrayAdapter<CharSequence> planAdapter = ArrayAdapter.createFromResource(
this, R.array.plan_size, android.R.layout.simple_spinner_item);
planAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spnPlan.setAdapter(planAdapter);
spnPlan.setOnItemSelectedListener(new MyOnItemSelectedListener());

How do I set an empty view for ListViews in an activity with multiple ListViews?

I have 3 lists stacked on top of each other in a single Activity, and I would like to set empty views for each of them. I have tried setting the empty view to both dynamically created views and views defined in the layout, but either way all it seems to do when the list is empty is collapse it to 0 height, regardless of the fact that I have set the minimum height for each list to 60px.
This is the xml definition for the empty view
<TextView android:id="#+id/empty_view"
android:layout_width="fill_parent"
android:layout_height="60px"
android:text="#string/empty_text"
/>
mListView.setEmptyView(findViewById(R.id.empty_view));
and this is how I programmatically created the text view.
mEmptyView = new TextView(this);
mEmptyView.setLayoutParams(new ListView.LayoutParams(60, 60));
mEmptyView.setText(R.string.empty_text);
mListView.setEmptyView(mEmptyView);
My bad, this was pretty silly on my part :P I was under the rather ludicrous impression that if I put a TextView somewhere randomly in the xml layout and then set it as the empty view for a list, it would magically disappear and then reappear in the list when the list was empty. I guess I thought that the list would add it as a row in the list when it's adapter was empty. In the unlikely even that someone else decides to try something similarly ludicrous and wonders what's going on, I will post the full version of my original layout as well as the solution.
The code I used to set the empty view was the standard:
mListView.setEmptyView(findViewById(R.id.empty));
however my original layout looked like this:
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<LinearLayout android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="200sp"
>
<TextView android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#FFFFFF"
android:text="#string/drafts"
/>
<ListView android:id="#+id/drafts"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:minHeight="60sp"
/>
<TextView android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:background="#FFFFFF"
android:text="#string/finished"
/>
<ListView android:id="#+id/finished"
android:layout_width="fill_parent"
android:layout_height="60px"
android:minHeight="60px"
/>
<TextView android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#FFFFFF"
android:text="#string/uploaded"
/>
<ListView android:id="#+id/uploaded"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:minHeight="60sp"
/>
<TextView
android:id="#+id/empty"
android:text="DUDE WHERE'S MY CAR?"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
/>
</LinearLayout>
While my listviews have a minHeight of 60sp, when the list was empty the height seemingly went to 0. As well, I noticed that the TextView was visible below all three of my lists so it did not seem to be "disappearing" and reappearing as a row in my list. How disappointing! In a gross oversight on my part, for testing I set the view as the emptyView for my second list only, ensured that my first and second lists were empty, and populated the third list. My first list had its min-height, my second shrank to 0, my third list was correctly populated, and my empty view appeared below it. I did not try populating the second list, and thus never realized that the empty view would subsequently disappear.
Finally after spending an hour more playing around with dynamically generated empty views and whatnot, I left it for a day and came back tonight and tested populating the second list. Voila, the empty view disappeared and the lightbulb went on. I quickly tested moving the empty view below the second list and above the third list, and of course, it worked perfectly!
Here is the new working layout:
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<LinearLayout android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="200sp"
>
<TextView android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#FFFFFF"
android:text="#string/drafts"
/>
<ListView android:id="#+id/drafts"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
<TextView android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:background="#FFFFFF"
android:text="#string/finished"
/>
<ListView android:id="#+id/finished"
android:layout_width="fill_parent"
android:layout_height="60px"
android:minHeight="60px"
/>
<TextView
android:id="#+id/empty"
android:text="DUDE WHERE'S MY CAR?"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
/>
<TextView android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#FFFFFF"
android:text="#string/uploaded"
/>
<ListView android:id="#+id/uploaded"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>
Long answer, probably just talking to myself, but whatever - asked the question, figured it out, and I never seem to be able to answer anybody else's questions on here so I might as well answer my own :)

Android ListView dynamically adding data

I have a listView that I want to dynamically add data to.
here's the XML I want to add the data to.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView android:id="#+id/rowTextView"
android:layout_width="wrap_content"
android:layout_height="70dp"
android:padding="12dp"
android:textSize="16sp" >
</TextView>
<TextView android:id="#+id/alarm_name_text"
android:layout_height="wrap_content"
android:text=""
android:layout_below="#+id/rowTextView"
android:layout_width="fill_parent">
</TextView>
<CheckBox android:id="#+id/CheckBox01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="10dp"
android:layout_alignParentRight="true" android:layout_marginRight="6sp"
android:focusable="false">
</CheckBox>
</RelativeLayout>
I want to add data to the 2nd TextView with the id alarm_name_text after a user has entered some data in an editText dialogView. I've been told to make a function that adds the data the my ArrayList/adapter but I'm not sure when to call it or how it's being used.. I needs help plz :(. Here's the function.
public void addItems(View v)
{
rowSavedText.setText(getString());
planetList.add(new Planet("This one"));
listAdapter.notifyDataSetChanged();
}
You'll need to work on the part of the code that renders the individual rows. Take a look at the tutorial at http://geekswithblogs.net/bosuch/archive/2011/01/31/android---create-a-custom-multi-line-listview-bound-to-an.aspx: specifically how it uses getView.
Just add the element to your ArrayList and call notifyDataSetChanged() to your array adapter.

Custom list: EditText consumes list's OnItemClickListener

I've been implementing a number of custom-lists, all worked fine. Now I'm trying to add an EditText to the list's rows. The rows look like that:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<TextView android:id="#+id/tv_quest_listrow_categorical_text"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:text="blabla"
android:paddingLeft="10dp"
android:minHeight="48dp"
android:gravity="center_vertical" />
<EditText android:id="#+id/tv_quest_listrow_text"
android:layout_width="60dp"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_marginRight="10dp"
android:gravity="right|center_vertical"
android:singleLine="true"
android:enabled="false"
android:imeOptions="actionDone" />
</RelativeLayout>
When constructing the list I set an ItemClickListener for the list with listView.setOnItemClickListener(onItemClick);. This used to work fine but fails as soon as there is an EditText in the list rows.
Does anyone have a hint on how to do this?
Regards,
Steff
I found this page, which has some good information on it. Apparently others have had this issue as well.
Try iterating through the items of the ListView and set the OnItemClick listner individually for each item
thanks

Categories

Resources