I want to display some of the contents of my database to the user with a scroll ability.
So I have extended the ListActivity and I used the SimpleCursorAdapter. The problem is that i can not see the values displaced.
private void fillData() {
// Get all of the rows from the database and create the item list
ListData = data.findAllDbValues();
startManagingCursor(ListData);
// Create an array to specify the fields we want to display in the list
String[] from = new String[]{Data.ID, Data.DESC};
// and an array of the fields we want to bind those fields to
int[] to = new int[]{R.id.text1, R.id.text2};
// Now create a simple cursor adapter and set it to display
ListAdapter la = new SimpleCursorAdapter(this, android.R.layout.simple_list_item_2, ListData, from, to);//R.layout.showdata
setListAdapter(la);
}
If I use a custom layout(R.layout.showdata),where it has only two TextViews, I can see the values but I have to touch the text which is displayed to do my job instead of just touching anywhere of the selection.
So I want to use a simple_list_item_2 so I can have this ability. Any ideas why I do not see the values or how can I make my custom layout to produce the same abilities with the simple_list?
This worked for me:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android_orientation="horizontal" android:layout_width="fill_parent"
android:layout_height="fill_parent" android:paddingLeft="4dip"
android:paddingRight="5dp">
<TextView android:id="#+id/productRow"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content" android:layout_height="wrap_content" />
</LinearLayout>
The trick is to wrap the TextView row in a LinearLayout. This lets me click anywhere on the list element to select it.
I know this question is old but I arrived here through Google and I think I have the answer since I had similar problem.
You are referencing custom TextView in your int[] to array, not the Android ones. You must use instead:
// and an array of the fields we want to bind those fields to
int[] to = new int[]{android.R.id.text1, android.R.id.text2};
Just check out that the ids in the XML and the ids in your class are the same. When you use custom views the reference in your class is
R.id.text1 and in your layout android:id="#+id:text1" but when you use Android resources the reference in your class is android.R.id.text1 and in your layout android:id="#android:id/text1".
You can use any of them but it must be consistent.
I was using android:id="#+id:text1" in my XML (creating a new TextView with id "text1") but using android.R.id.text1 in my Java Class so the CursorAdaptor was setting the information in a non-existent View and I was getting exactly that result: a listview with all the rows without text.
Related
I have a GridView, each cell of which I would like to populate with a list of names, and I need these lists to be dynamically changeable. I tried implementing this idea as follows:
public void fillBoard(int groupSize){
int numGroups = listPresentMembers.size()/groupSize;
ListView [] groups = new ListView [numGroups];
for(int i=0;i<numGroups;i++){
String [] items = new String[groupSize];
for(int j=0;j<groupSize;j++){
items[j] = listPresentMembers.get(i*groupSize + j);
}
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1,android.R.id.text1,items);
groups[i] = new ListView(this);
groups[i].setAdapter(adapter);
}
ArrayAdapter<ListView> groupsAdapter = new ArrayAdapter<ListView>(this,
android.R.layout.simple_list_item_1,android.R.id.text1,groups);
gridViewBoard.setAdapter(groupsAdapter);
}
The essential idea is that I create an ArrayAdapter of type ListView, each item of which is defined by one of the groups of names I would like to populate a particular cell of the GridView. I then set this ArrayAdapter to my GridView.
The problem is that when I run the app, the elements of the GridView are strings such as
"android.widget.ListView#41bcfa60". Where did I go wrong?
Thanks very much!
Short answer: whenever you see something like "android.widget.ListView#41bcfa60", it means that something in your code was expecting a String and you gave it an object (in this case, a ListView) instead.
So what happened? Take a look at the adapter for your GridView.
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1,android.R.id.text1,items);
Here's the constructor for the ArrayAdapter you're using:
public ArrayAdapter (Context context, int resource, int textViewResourceId, T[] objects)
Parameters
context The current context.
resource The resource ID for a layout file containing a layout to use when instantiating views.
textViewResourceId The id of the TextView within the layout resource to be populated
objects The objects to represent in the ListView.
You're passing android.R.layout.simple_list_item_1 as the resource, which means it's the layout you're using. This layout consists of a single TextView--a single line of text. It is not designed to hold a ListView.
When this layout wants to populate the TextView (the textViewResourceId), it looks at the appropriate element of the array (items) that you passed in and tries to get text out of it--in Java we do this by calling toString() on an object. If toString() is not implemented, by default it returns something like the "android.widget.ListView#41bcfa60" gibberish that you got.
That's the why, but I think your code has bigger problems. If I were you, I'd strongly reconsider a design that involves a GridView where each grid item is a ListView. That is not conventional at all, so there's probably a better way to accomplish whatever you're trying to do. Consider posting another question explaining your goal and asking for code design help.
I've been searching and testing the whole day, but I didn't find any solution for this problem. I've got a ListView and I want to show some Views in that. But everytime I add a view, it only shows me the class or the id of the listItems instead of the Views themselves. Like:
android.widget.Button{40f090e0VFED.. C. ..........l. 0,0-0,0}
What am I doing wrong!? Do I have to set a certain tag!?
This is how my Listview is implemented in the xml-file
<ListView
android:id="#+id/listView1"
android:layout_width="fill_parent"
android:layout_height="60dp"
android:animateLayoutChanges="true"
android:clipChildren="true"
android:clipToPadding="true"
android:dividerHeight="1dp"
android:focusableInTouchMode="true"
android:headerDividersEnabled="true"
android:scrollbarStyle="insideInset"
android:scrollbars="vertical|horizontal"
android:splitMotionEvents="true" >
and this ist the java-code in the activity:
Button[] test = new Button[3];
test[0] = new Button((Activity) _mycontext);
test[1] = new Button((Activity) _mycontext);
test[2] = new Button((Activity) _mycontext);
test[0].setText("Test-Button 1");
test[1].setText("Test-Button 2");
test[2].setText("Test-Button 3");
_dt_columns.setAdapter(new ArrayAdapter<View>((Activity) _mycontext, android.R.layout.simple_list_item_1, test));
_dt_columns.notifyAll();
I also did it all the way with "extends ListActivity" and the Inflater, and it all shows up, but there's always just that text, not the views themselves.
ArrayAdapter populates ListView only with one TextView, each item may have some complex layout but the only difference between them will be a TextView value.
You are passing View elements as list items and adapter call toString method on them to populate this TextView, that is where You get this "strange" values from.
Create Your own xml layout resource for single list item with desired structure instead of using android.R.layout.simple_list_item_1.
And create Your asdapter like this
ArrayAdapter<YourObject> adapter = new ArrayAdapter<YourObject>(this, R.layout.listitem, R.id.textview_inside_listitem, YourObject[])
where YourObject is a class You created to hold data for single item and toString method return value You wish to display in list.
I want to be able to show an alternative to an empty list, currently the following method call sets up the list, however taskArrayList could be empty if the call to the database has returned an empty list
/**
* Setup list adapter.
*/
private void setupListAdapter() {
setListAdapter(new TaskAdapter(this, R.id.tasks_list_view,
taskArrayList));
taskListView = getListView();
taskListView.setOnItemClickListener(new TaskClickListener());
}
I'm thinking that before I call this method, or at the beginning of the method adding the following check
if(taskArrayList.isEmpty()) {
/*load an alternative view*/
}
however because my Activity is a ListActivity so it requires a ListView to be set against it, so I'm unsure of the best approach to handling the case when the list is empty.
I don't want to create a "dummy item" which says there are no items and add it to the ArrayList but I'm not sure of the alternatives.
If you add something with the idea #android:id/empty
it will be shown instead of the list when it is empty automagically!
<TextView android:id="#android:id/empty"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#FF0000"
android:text="No data"/>
In the documentation they say it like this:
Optionally, your custom view can contain another view object of any type to display when the list view is empty. This "empty list" notifier must have an id "android:id/empty". Note that when an empty view is present, the list view will be hidden when there is no data to display.
http://developer.android.com/reference/android/app/ListActivity.html
I'm new to android programming and I would like some help. I have the following code:
Object[] list_cities = parsedData.getCityname().toArray();
Object[] list_countries = parsedData.getCountryname().toArray();
// Display the available locations
list_search.setAdapter(new ArrayAdapter<Object>(this, android.R.layout.simple_list_item_2, android.R.id.text1, list_cities));
list_search.setAdapter(new ArrayAdapter<Object>(this, android.R.layout.simple_list_item_2, android.R.id.text2, list_countries));
I would like to display double rows for each entry in the list (city and country) but I have no luck. With the above code I only see the countries but not the cities. Is there any way to add both the adapters to the list_search so I can see all the data?
Thank you all for your answers!!
Create a custom container object, maybe CountryCityLocation, with a couple strings for country and city name.
Create an array of these containers and fill it with your information.
Create a custom ArrayAdapter, maybe CountryCityAdapter, apply the array to the custom adapter, and feed it to getListView.setAdapter.
Override the ArrayAdapter's getView method to apply your name strings to TextViews in your custom list row view.
EDIT - removed dead tutorial link
You can bind only one adapter to ListView.
If you want to combine adapters you need to implement custom adapter.
For example you can inherit SimpleAdapter, provide two simple adapters in constructor and combine data in in getItem method.
I am just starting out with Android and I am attempting my first test app, but I am a bit stuck.
I have an SQLite database and a user is selecting a picture and a reference to this a long with a short description is getting stored in the database.
This is all working fine, now I want to bind the images from to database to a gallery, but I am a bit confused how I do this.
My layout xml is as follows:
<?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">
<Gallery
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/Weeksgallery"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
<Button android:id="#+id/ok"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/text1"
android:layout_marginLeft="10dip"
android:text="Take a Picture"
android:onClick="TakePicture" />
</LinearLayout>
I want the gallery with a button below
My on create function I have the following:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mDbHelper = new DbAdapter(this);
mDbHelper.open();
Cursor cur = mDbHelper.fetchDaysImages();
//array of fields to display
String[] from = new String[] { mDbHelper.KEY_ROWID,mDbHelper.KEY_PICREF};
int[] to = new int[] { R.id.Weeksgallery };
// Now create a simple cursor adapter and set it to display
SimpleCursorAdapter images = new SimpleCursorAdapter(this,
R.layout.main, cur, from, to);
setListAdapter(images);
mDbHelper.close();
}
I am using the notepad tutorial as an example and my class extends ListActivity.
When I run it I get the error
Your content must have a ListView whose id attribute is 'android.R.id.list'
I'm assuming I am using the wrong type of binding, but I can't seem to find anywhere that tells me how to bind from an sqlite DB to a gallery.
I also want to add a reference to the rowId in the gallery so when the image is clicked it will open the relevant page (so it can show the message)
Can anyone help?
Thanks
Bex
Your Activity inherits ListActivity, right? When you use ListActivity, you must make sure there is a ListView whose id is "android.R.id.list" in the layout of your Activity, that is what the log told you.
If there is no ListView in your Activity, then don't use ListActivity. If you want to set adapter to Gallery, use Gallery`s setAdapter method.