Problem with my setListAdapter() - android

I don't know what's going on I first posted this in a class that extends Activity. But even after changing it to a ListActivity it still doesn't work. I'll dumb it down to where I think the problem is.
public class PassScreen extends ListActivity {
String[] items = {"lorem","ipsum", "dolor"};// I have it calling an xml file but switched it to this just to see if that was the problem.
setContentView(R.layout.passwordscreen);
setListAdapter(new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1,
items));
I don't get what's wrong. This is what my book uses. I checked to make sure the xml file displays the proper ListView and it does. But here it is anyway:
<?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">
<TextView
android:id="#+id/selection"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
<ListView
android:id="#android:id/list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:drawSelectorOnTop="false"/>
</LinearLayout>

try this..
getListView().setListAdapter(new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, items));
getListView().setTextFilterEnabled(true);

OK so I just found out the problem. ARGH!! This is why I love programing. My Layout needed an orientation.
So:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
Should have looked like this:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" // This is the difference.
android:layout_width="fill_parent"
android:layout_height="fill_parent">

Related

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?

ListView inside Fragment android

Im having trouble with implementing list view inside a fragment.
the xml code is:
<?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" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<ImageView
android:id="#+id/imageView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:src="#drawable/jo_logo" />
</LinearLayout>
<ListView
android:id="#+id/listView1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1" >
</ListView>
</LinearLayout>
simple ImageView and afterwards the list view.
I've already tried to implement:
ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity().getBaseContext(),
android.R.layout.simple_list_item_1, viewList);
setListAdapter(adapter);
where viewList is View[], on the onCreateView function.
seems that it's not working that way inside fragment.
thing is i need that the first view will be ImageView and second one is ListView, and all inside the Fragment.
please help, thank in advance, udi
In your layout xml change android:id="#+id/listView1" to android:id="#android:id/list" otherwise the ListActivity / ListFragment won't find your ListView.

Two ListViews in one activity in android 3.0 and above

In Android 2.1 and 2.3 it works fine. But for 3.0 and above findViewById does not find the second ListView. I have already tried cleaning the project, tried not using ListActivity too, nothing helped. Any ideas?
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
setListAdapter(List1Adaptor);
list1 = (ListView) findViewById(android.R.id.list);
list2 = (ListView) findViewById(R.id.ListView2);
list2.setAdapter(List2Adapter);//Null Pointer
}
the main.xml
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/linearLayout1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<ListView
android:id="#+id/ListView2"
android:layout_height="wrap_content"
android:layout_width="fill_parent" >
</ListView>
</LinearLayout>
<RelativeLayout
android:id="#+id/listLayout_relativeLayout2"
android:layout_height="320dp"
android:layout_width="fill_parent" >
<ListView
android:id="#android:id/list"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
</ListView>
</RelativeLayout>
Your xml file is missing a root view. I'm surprised it works on any version of Android. You should surround everything with a linear layout or something like that.
Your layout has android:id="#+id/listView2" (note: lowercase 'l') and your Java code is requesting R.id.ListView2 (note: uppercase 'L'). Try using the same case in both situations.

Customizing ListView via XML Problem

I have a simple ListActivity that uses a ListAdapter that I'd like to customize the ListView for. Here is my onCreate, where I specify to use a View rather than using the default ListView generated by setListAdapter():
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// set the listview, empty listview
setContentView(R.layout.main);
getListView().setEmptyView(findViewById(android.R.id.empty));
// set the list properties
getListView().setOnCreateContextMenuListener(this);
.
.
.
<code snipped>
.
.
.
setListAdapter(adapter);
}
You can see that I am setting the emptyView as well. Here is the XML file main.xml:
<?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">
<ListView
android:id="#id/android:list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:fadingEdge="vertical"
android:dividerHeight="5dp" />
<TextView
android:id="#android:id/empty"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center"
android:singleLine="false"
android:text="#string/message_empty_list" />
</LinearLayout>
My problem is that the ListView, while applying the dividerHeight correctly, is ignoring the :fadingEdge directive when it is in the XML; however, if I set the fadingEdge programatically, it works, i.e.:
getListView().setVerticalFadingEdgeEnabled(true);
Any ideas why the listview would be ignoring the fadingEdge specifically? Am I doing something incorrectly that could be leading to this behavior?
Thanks!
android:id="#id/android:list"
should be
android:id="#iandroid:id/list"
I'm not even sure why that compiles.
This is how you should be using custom xml for listactivities.
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ListView
android:id="#android:id/list"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
<TextView
android:id="#android:id/empty"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="No data found"
android:gravity="center"
android:textAppearance="?android:attr/textAppearanceMedium" />
</FrameLayout>

Creating ListView with check boxes...Android

I am trying to create a listview that has check box beside each item. (i am following a code from the book android), but i can't get it to work, every time i run it, it crashes. since i am very new to this android staff, i have no clue what to do to get it to work, any help is appreciated.the code is below.
*i have created two layout files in names of list and list_item.
the code for the main activity:
public class ListDemoActivity extends ListActivity {
/** Called when the activity is first created. */
String[] listItems = {"exploring", "android","list", "activities"};
private SimpleCursorAdapter adapter;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.lists);
//setListAdapter(new ArrayAdapter(this,android.R.layout.simple_list_item_1, listItems));
//setContentView(R.layout.lists);
Cursor c = getContentResolver().query(People.CONTENT_URI, null, null, null, null);
startManagingCursor(c);
String[] cols = new String[]{People.NAME};
int[] names = new int[]{R.id.row_tv};
adapter = new SimpleCursorAdapter(this,R.layout.list_item,c,cols,names);
this.setListAdapter(adapter);
}
}
the lists file content:
<?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="wrap_content">
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<ListView
android:id="#android:id/list"
android:layout_width="fill_parent"
android:layout_height="0dip"
android:layout_weight="1"
android:stackFromBottom="true"
android:transcriptMode="normal"/>
</LinearLayout>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Submit Selection"/>
</LinearLayout>
</LinearLayout>
and the list_item file content:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<CheckBox
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/row_chbox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<TextView
android:id="#+id/row_tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
I'm reading the same book (Apress Android 2 Pro, source code at http://www.apress.com/book/downloadfile/4529 - example /Ch4/Listing4-17)
I found when I copied / pasted it into an eclipse project, it didn't work for the simple reason that I hadn't set the package name at the top of the pasted source file.
Try putting:
package asdf.adsf;
...(with your actual package name ;-), at the top of the java file, maybe that will help. The problem was that it wasn't using the project-specific 'R' resource class.

Categories

Resources