Single Choice List Item Does not Populate - android

I'm building a single choice list with a button at the bottom, and I can't seem to get the text in the actual list to display. Basically, it's a simple implementation that looks like:
public class SetPrefsActivity extends ListActivity{
#Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.radiolist);
ArrayList<Hall> listItems = new ArrayList<Hall>();
ArrayAdapter<Hall> ar = new ArrayAdapter<Hall>(this, android.R.layout.simple_list_item_single_choice, listItems);
setListAdapter(ar);
ListView lv = getListView();
lv.setTextFilterEnabled(true);
lv.setOnItemClickListener(new OnItemClickListener()
{ ... //stuff});
...
//((ArrayAdapter<Hall>)getListAdapter()).add(stuff)
...
}
And Xml for radiolist is:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ListView
android:id="#android:id/list"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="0dp"
android:drawSelectorOnTop="false"
/>
<Button
android:id="#+id/theButton"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Click to go to next Activity"
/>
</LinearLayout>
What I see is a black area where the list should go, and then my button under it. Before I added: setContentView(R.layout.radiolist); it showed the populated list just fine. How do I get it to show up in this layout?
Sorry I'm just getting into android!

Along with changing your width, you should add : android:cacheColorHint="#00000000"
Here is a link that might help: http://android-developers.blogspot.com/2009/01/why-is-my-list-black-android.html
Otherwise, when you scroll, you will get black boxes and other unwanted results when you scroll with your list.

Related

Listview dynamically allocate width

I want to allow my ListView to be wrapped to its content on its width and making it aligning to the parent centre.
This is what a normal ListView is giving me
This is almost what i want to achieve, only that i want the ListView to wrap by the longest test in the ListView. Currently this is achieved by declaring the dp which is something I would like to avoid if possible
Some codes for first image:
<ListView
android:id="#+id/lst_test"
android:layout_width="0dip"
android:layout_weight="1"
android:layout_height="wrap_content" >
</ListView>
Some codes for second image:
<ListView
android:id="#+id/lst_test"
android:layout_width="200dp"
android:layout_height="wrap_content" >
</ListView>
Do I have to use custom adapter for this or I can stick with the current one? If I would need to use custom adapter, how do i go about doing this?
You don't need to create custom adapter. Here is an example which can help you.
public class MainActivity extends Activity {
HashMap <String, CheckBox> options = new HashMap<String, CheckBox>();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ListView list_view = (ListView) findViewById(R.id.listView1);
ArrayList<String> str = new ArrayList<String>();
for (int i = 0; i < 10; i++) {
str.add("Example " + i);
}
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.list_layout, R.id.textView1, str);
list_view.setAdapter(adapter);
}
}
Above I have created an activity in which i am getting listview and setting data using ArrayAdapter.
I have created two layouts :
First layout: Contains listview.
Second layout: Contains textview which need to display.
Main logic to align data at center of Listview is you need to set gravity of textview as center.
You can manage second layout according to your needs.
Here is my first XML layout:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<ListView
android:id="#+id/listView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true" >
</ListView>
</RelativeLayout>
Here is my second layout:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/RelativeLayout1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:gravity="center"
android:text="TextView" />
</RelativeLayout>
If you are still facing any problem then you can ask. If you need more flexibility then you can create custom adapter by inheriting ArrayAdapter class.
Here is an example of custom adapter: How do I link a checkbox for every contact in populated listview?
Hope it will help you.

Android Layout with ListView overlaps Buttons

I can't seem to find a way to make a layout that contains a ListView that show the entire list of my contacts (its a long list), with 2 buttons one next to the other below the list.
its freaking me out, nothing i do seems to work. i searched some related quiestions and found this one: Android Layout with ListView and Buttons
but none of the alternative solutions over there is working for me.
when i look at the graphical presntation (of eclipse) it looks fine, but when i run it on my device i can only see the list of contacts. the listView overlaps the buttons.
I am using a simpleList that render on another layout that represent a row. i'm starting to suspect something i do programtically ruins it, because i ran over my java code and found no mention of the activity_contact_list that i was talking about. only connection to the row layout.
so i got this activity_contact_list.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/contact_list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ListView
android:id="#+id/list"
android:layout_width="match_parent"
android:layout_alignParentTop="true" >
</ListView>
<LinearLayout
android:id="#+id/buttons"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_weight="1.0"
android:orientation="horizontal" >
<Button
android:id="#+id/action_cancel"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Cancle" />
<Button
android:id="#+id/action_add"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Add" />
</LinearLayout>
</LinearLayout>
this is after many tries such as: using RelativeLayout instead of the LinearLayout, puting the ListView's code below the LinearLayout of the buttons, and many more..
and here is the code of the activity: ContactList.java:
public class ContactList extends ListActivity{
//some global variables for late use.
public Model model;
SimpleAdapter adapter;
List<Map<String, String>> data; /* data is a list of Map */
String[] from = {"name","phone"}; /* names of the columns */ //TODO - add date!
int[] to = {R.id.name, R.id.number};
List<Contact> contacts; /* list of contacts */
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// setContentView(R.layout.activity_contact_list);
// Set up the action bar.
final ActionBar actionBar = getActionBar();
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_STANDARD);
actionBar.setDisplayHomeAsUpEnabled(true);
model = Model.getInstance(this);
data = new ArrayList<Map<String, String>>();
contacts = fillContactsList();
fill_data();
adapter = new SimpleAdapter(this, data, R.layout.contact_list_row, from, to);
setListAdapter(adapter);
}
i commented the setContentView line cause it doesnt compile otherway...
As is said. the list works perfactly. but it overlaps the buttons at the bottom of the activity. any idea? i'm sure its something silly i'm missing..
thanks in advance!
Did you try to use Relative Layout ?
Change Linear Layout to Relative Layout that's all.
As Linear Layout Doesn't support align parent..
Edited
set List Height to mach parent.
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/contact_list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ListView
android:id="#+id/list"
android:layout_below="#+id/buttons"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</ListView>
<LinearLayout
android:id="#+id/buttons"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:weightSum="2" >
<Button
android:id="#+id/action_cancel"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Cancle" />
<Button
android:id="#+id/action_add"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Add" />
</LinearLayout>
How long is your ListView. Maybe it is longer than your screen and is the LinearLayout with buttons shown outside your screen.
What happens when you rearrange with the buttons above the ListView.
EDIT:
I think you have to set a Max-size in your ListView. Maybe you can try something like this in your java-code, but I don't know if it is scrollable then...
ListView listView = (ListView) findViewById(R.id.listView):
LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) listView .getLayoutParams();
params.height = 200; // or something bigger / smaller
listView .setLayoutParams(params);

Issue with XML display font (Android)

I'm trying to display my parsed JSON in a ListView, but there is a little problem with the text in the ListView always appearing as a light grey, even though I have specified the font color as #000000
Code snippet that calls the list:
protected void onPostExecute(JSONObject json) {
ListView myListView = (ListView)findViewById(android.R.id.list);
myListView.setAdapter(new ArrayAdapter<String>(getApplicationContext(), android.R.layout.simple_list_item_1, items));
}
XML file:
<?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="fill_parent"
android:orientation="vertical" >
<ListView
android:id="#android:id/list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textColor="#000000" >
</ListView>
</LinearLayout>
Any ideas what the problem could be?

Why does empty view in layout file shows up on screen even though listview has items?

I have a Listview to show some item and i have also used an empty view in case my adapter has no item for listview to show.Problem is when I go this this activity first it shows the empty view on the screen for a second and then loads the item and show them in listview.
My Activity onCreate looks like this:
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.favorite_exams);
list = (ListView) findViewById(R.id.favoriteExamsList);
View empty = findViewById(R.id.favoriteExamsListEmptyView);
list.setEmptyView(empty);
new readingFavFileTask().execute(UtilityFuctions.FAV_EXAMS_FILE_NAME);
}
My layout file is:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ListView
android:id="#+id/favoriteExamsList"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
</ListView>
<LinearLayout
android:id="#+id/favoriteExamsListEmptyView"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:visibility="gone" >
<TextView
android:id="#+id/favoriteExamsEmptyViewMessage"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:text="You do not have any favorite Exams.Please add Exams using below button."
android:textSize="20dp" />
<Button
android:id="#+id/favoriteExamsAdd"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:onClick="onClickAddExams"
android:text="Add Exams"
>
</Button>
</LinearLayout>
</LinearLayout>
How I Do make sure that empty view never shows up even for a second if we have data in our adapter.Should I attach the empty view to list later after checking the adapter or is thr any other way to do this?
I found the answer. I just need to apply Empty View list.setEmptyView(empty) in my AsyncTask onPostExecute() method after setting my adapter to list and it works perfectly.

Set emptyView of android listActivity

i have created a My custom listActivty by extending ListActivity like this.
public class MainList extends ListActivity {
ListView listView;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
listView = getListView();
setContentView(R.layout.list_main);
}
}
And declaring its xml like this
<?xml version="1.0" encoding="utf-8"?>
<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:id="#id/android:list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
</ListView>
<TextView android:id="#id/android:empty"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#FFFFFF"
android:text="My Text"
/>
</LinearLayout>
i am using CursorAdapter as listActivity adapter.Its showing perfectly with MainList is empty.
My problem is that i want to show more than a textview when MainList is empty. Basically i need custom view(imageview,button etc) to be shown when list is empty.How should i do this.
Thanks
For the listview there is one default method to set empty view.
setEmptyView(view)
Try This.... In the place of view pass your custom view.
If you are not getting then tell me. I will post an example for this.
You may want to count the items in your CursorAdapter. If 0 is returned, hide/show parts from your XML? That's how I done similar, but they may be better ways.
Try using the android:id/empty for your custom view. I think you can apply the id to any view, not just a TextView

Categories

Resources