ListView shows only text instead of Views - android

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.

Related

Adding items to ListView causes first item appear again and again

When the app starts, I load some objects from the Device's SD Card and add them to a ListView.
Objects = new ArrayList<DataObject>();
Items = new ArrayList<MenuItem>();
Adapter = new MenuItemAdapter(this, Items);
listView.setAdapter(Adapter);
LoadObjects();
When I load objects I deserialize the object, check it's unique ID and if it's already in the list of Objects it should not be added. My method LoadObjects loads tries to load all files in a directory with a given extension. Then if successful, tries to load it into the Items list by using this method:
private boolean LoadObject(DataObject obj)
{
for (DataObject do : Objects)
{
if (do.GetID().equals(obj.GetId())
return false;
}
boolean added = Objects.add(do);
if (added)
{
Items.add(new MenuItem(do.GetID(), do.GetName());
Adapter.notifyDataSetChanged();
}
return added;
}
When this method is run "onCreate" it works fine, it loads in all the objects that can be loaded and is stored on the SD Card. When I try to add one after this method, the first item appears in the list where the new item should be added. I input a new name with a Dialog to create a new DataObject, which will later call LoadObject(new DataObject(name));
I've tried:
Resetting the Items adapter
Adding a "Add" method on the custom Adapter so that instead of adding to Items, I add through the Adapter by Adapter.Add(new DataObject(name)) which adds to it's own list; I also have a SetList adapter, to recreate the list without success.
Skip the notifyDataSetChanged
Cleared the Object list, Items list, Create a new Adapter each time - reloading all Objects, all MenuItem's, recreating the adapter and setting a new List - all with the same result.
Skip LoadObjects() and just let all newly added items appear, first one is correct - but whatever I add later shows up as the same as #1 from the ListView.
And I have to use FragmentActivity and not ListActivity because I have some Dialogs I need to show.
Simply put, I use the ListView to show specific Files from the SD Card to be able to load them onto another Activity later - but this hinders me from adding any new items without having to restart the Activity just to show new items, which means I need to be able to start out with an empty list and then
Probably becouse you use Adapter that have multiple references. Try to rename and don't name variables with uppercase.(see also import list to see what Adapter did you have there)
Your attempt #4 has to work. Give next loadObject() a shot:
private boolean LoadObject(DataObject obj)
{
for (DataObject do : Objects)
{
if (do.GetID().equals(obj.GetID())
return false;
}
Objects.add(obj);
Items.add(new MenuItem(obj.GetID(), obj.GetName());
Adapter = new MenuItemAdapter(this, Items);
listView.setAdapter(Adapter);
return true;
}
Seems that the problem occured due to a ListView being inside a RelativeLayout, altough I can't figure out why.
This is how my Layout was first:
<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" >
<ListView
android:id="#+id/main_menulist"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
</ListView>
</RelativeLayout>
When I instead created a new Layout with a ListView only, the problem no longer occured:
<?xml version="1.0" encoding="utf-8"?>
<ListView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/main_menulist"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</ListView>
I've only read about how you should not put a ListView in a ScrollView, but I've used this type of solution (perhaps not by dynamically adding items) before and it's worked.

Displaying an alternative to an empty list in android

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

Assign object property to listview

I have an ArrayList of an object which has properties Object.name and Object.url.
I want to loop through the ArrayList and apply the Object's "name" to an android ListView. I also want to keep the Object's other properties in tact, so that i can call the "url" property in the onClick method.
What i have now is this:
main_list.setAdapter(new ArrayAdapter<RomDataSet>(this, android.R.layout.simple_list_item_1, android.R.id.text1, mRoms));
But clearly that is not what I need...
Any help would be appreciated :)
1.) You have your ArrayList:
main_list
2.) Create a ListView in your XML file (say, main.xml) and grab its id. That is, given:
<?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 xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/liveFeed"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>
</LinearLayout>
Do something like this:
ListView livefeed = (ListView)this.findViewById(R.id.liveFeed);
within your activity (if you're in somewhere else such as an OnClickListener, replace the "this" with the View variable that was passed as a variable into the OnClickListener).
3.) Define your ArrayAdapter. Note that one of its parameters (the third one in your case) will be a TextView id. This is because the ArrayAdapter class, by default, returns a TextView in the ListView. If you override the ArrayAdapter class, you can use custom layouts to have items with custom Views within your ListView, but this is not necessary for what you've outlined in your question, and it seems like you've got it already.
4.) Set the adapter to the ListView (given an ArrayAdapter named 'aa'):
livefeed.setAdapter(aa);
Now the way the ArrayAdapter works is it invokes each Object's toString() method and sets each TextView in the ListView to this String. So make a toString() method in your Object's class that returns its name property:
public String toString(){return name;} //assuming name is a String
Also note that, if you add Objects to the ArrayList, notify the ArrayAdapter that you have so it can accordingly update your ListView with the modifications (given an ArrayAdapter named 'aa'):
aa.notifyDataSetChanged();
Let me know if you need any more help. As always, check the answer check mark if this answered your question.
Also note that, at one point you may wish to cross reference your ArrayAdapter and ArrayList between your activity and Object class. It's very helpful to make these fields static in order to do so.
EDIT:
You wanted to also know how to access a specific Object when you click on an item in the ListView. Here it is (given your ListView is named livefeed):
livefeed.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> a, View v, int position, long id) {
//in here you may access your Object by using livefeed.getItemAtPosition(position)
//for example:
Object current = livefeed.getItemAtPosition(position);
//do whatever with the Object's data
}
});

Problem with AutoCompleteTextView and Spinner using the same Adapter

I have a list of "categories" that are stored as Strings in an ArrayAdapter in my app. This much is pretty simple. The adapter is a field of the Activity and accessible everywhere. It is populated with values during onCreate().
I have a "Entry" Dialog that contains an AutoCompleteTextView which uses this adapter and works very well. This is basically a dialog to add new items to a list.
I also have a second Dialog which acts as a filter for my list and has a single Spinner which when the Dialog is created, uses the same ArrayAdapter. Here's the problem.
If I use the filter Dialog before the entry Dialog, the Spinner is populated with all of the items from the adapter. Works well.
Any and every time I use the entry Dialog, the AutoCompleteTextView works properly.
The problem comes if I use the entry Dialog and select an item in the AutoCompleteTextView when it suggests something. After selecting a suggested item in the popup, even if I cancel the entry Dialog, the next time I bring up the filter Dialog, the Spinner initially shows the item that was last selected from the AutoCompleteTextView (instead of the first item in the adapter), and only displays that single item in the Spinner's list if clicked/touched. The only way to fix it is to end the application and re-open it. I'm not getting any errors or anything in logcat that is helpful.
EDIT - Okay, I've removed the previous code to replace it with a simple test case that I produced. The bottom line is that I would like to know if this is a bug, or if if it is expected results. When a suggestion is selected in an AutoCompleteTextView, I can confirm that the ArrayAdapter that is linked to it has filtering applied so that it's count is affected and the only items shown if the adapter is accessed in a Spinner will be those that were filtered down to. I also added button to display a toast to show that the count is affected. Type "te" in the autocomplete, select either Test entry. They try the spinner or click the button to see the adapter's count is only 2.
So the final question is now... can the adapter's filter be reset (other than by typing in and clearing the AutoCompleteTextView)? I can't find any method to do this. I have worked around the problem in my actual app by setting up a temporary adapter, copying over the main adapters items and setting the views to use the temporary adapter instead. My phone is running 2.2, and I have tested as high as 2.3.3 API level 10 in an emulator.
The xml for main.xml
<?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="fill_parent" >
<AutoCompleteTextView android:id="#+id/actv"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" />
<Spinner android:id="#+id/spinner"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_centerInParent="true" />
<Button android:id="#+id/button"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Check It"
android:layout_alignParentBottom="true"
android:layout_centerInParent="true" />
</RelativeLayout>
The code for MainActivity.java
public class MainActivity extends Activity {
/** Called when the activity is first created. */
AutoCompleteTextView actv;
Spinner spinner;
Button button;
String [] adapterList = {"Test1", "Test2", "Garbage1", "Garbage2"};
ArrayAdapter<String> adapter;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
adapter = new ArrayAdapter<String>(this, android.R.layout.simple_dropdown_item_1line, adapterList);
actv = (AutoCompleteTextView) findViewById(R.id.actv);
spinner = (Spinner) findViewById(R.id.spinner);
button = (Button) findViewById(R.id.button);
actv.setAdapter(adapter);
spinner.setAdapter(adapter);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(MainActivity.this, "" + adapter.getCount() + " Items in Adapter", Toast.LENGTH_SHORT).show();
}
});
}
}
Looking at AutoCompleteTextView source code, it does filter the ArrayAdapter:
http://grepcode.com/file/repository.grepcode.com/java/ext/com.google.android/android/2.2_r1.1/android/widget/AutoCompleteTextView.java#AutoCompleteTextView.performFiltering%28java.lang.CharSequence%2Cint%29
You may want to remove the filter on an onClick callback in the spinner:
Filter filter = adapter.getFilter();
filter = null;
(see the setAdapter method in AutoCompleteTextView, around line 600)
http://grepcode.com/file/repository.grepcode.com/java/ext/com.google.android/android/2.2_r1.1/android/widget/ArrayAdapter.java#ArrayAdapter.getFilter%28%29
Let me know if it works
BTW, is the adapter going to change dynamically? You're "workaround" of two adapters might be a better option than messing with the filter. What if the user starts a word, then clicks the spinner, cancels it and goes back to the word? Now the autocompletion will not make sense, unless you save the filter and restore it somehow.
I do believe that two adapters (based on the same string array) is a better solution.

ListView with CursorAdapter does not display my values

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.

Categories

Resources