Android ListView setEmptyView - android

I am populating listView using webservice; I want to display a message when there is no result. I used following code on activity but it is working fine; But when data is being loaded it still shows empty message in listview.
#Override
public void onContentChanged() {
super.onContentChanged();
View empty = findViewById(R.id.empty);
TextView empty1 = (TextView) findViewById(R.id.empty);
empty1.setText("No Channels Found!");
ListView list = (ListView) findViewById(R.id.listView1);
list.setEmptyView(empty);
}

Try to set the visibility of the empty view to Invisible after list is loaded. empty.setVisibility(View.INVISIBLE);

The empty view will be shown whenever your adapter returns a count of 0. That will include the time when your data is loading. If you don't want that behavior then don't set an empty view until after your data has loaded.

Related

notifiyDataSetChanged() won't update adapter with new fields

I have a list of items: List<SomeObject> items that is mapped to an adapter to show on a ListView. When I set a new field for a specific item in that list and call adapter.notifyDataSetChanged() then list doesn't update. For example, I want to show a date TextView in a specific row in the ListView by setting
item.get(position).showDate(true);
adapter.notifyDataSetChanged();
The view doesn't show until I scroll away from that row and back to it (I'm assuming because of recycling).
Doing list.setAdapter(adapter); works but the entire view flashes and there shouldn't be a reason to re-set the adapter.
How can I get the list to update without scrolling?
You could try to change the List that is referenced in the adapter. Add something like this to your adapter:
public void updateData(List<SomeObject> dataItems) {
this.mDataItems = dataItems;
notifyDataSetChanged();
}
And then:
items.get(position).showDate(true);
adapter.updateData(items);
you can use adapter.getitem(postion) to get SomeObject and refresh it.
SomeObject sobj = (SomeObject)adapter.getItem(position);
sobj .showDate(true);
adapter.notifyDataSetChanged();

How to strikethrough ListView item while populating ListView

so I have this huge problem with applying a strikethrough on a ListView item while populating it from the List of my objects.
private void setListItems() {
DatabaseHandler db = new DatabaseHandler(this);
List<ToDoTask> taskList = db.getAllTasks();
for (ToDoTask tt : taskList) {
this.listAdapter.add(tt.getName());
}
this.listAdapter.notifyDataSetChanged();
}
This part of the code populates my ListView. Now i want to add a strikethrough to item if
tt.getActive() == 0
I don't know how can i get a TextView item from my ListView. I tried in my for loop to:
TextView textView = (TextView) this.listView.getChildAt(this.listView.getCount() -1);
but the app crashes.
The only solution that didn't crash my app was:
TextView textView = this.listAdapter.getView(this.listView.getCount() -1, null, null)
but it didn't actually change anything in the ListView. I think it's not referencing the object itself.
Here's some code that instantiates my variables:
this.listView = (ListView) findViewById(R.id.listViewItem);
this.listItems = new ArrayList<String>();
this.listAdapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, this.listItems);
this.listView.setAdapter(this.listAdapter);
The list view is not in charge of instantiating or modifying the underlying state of the views it contains. That's the job of the adapter. When you scroll a ListView, it calls the adapter method getView() and the adapter will hand it a row (possibly a recycled view from the rows that scrolled off-screen).
Inside the getView() method, the adapter should consult the underlying data set and modify the row as needed - that would include crossing out text, or setting any visual properties of the row content.

Spinner in ActionBar is not updating ListView

I have two elements:
Spinner INSIDE THE ACTIONBAR (HoloEverywhere (support.v7)) of my Activity set with an onItemSelectedListener().
Listview in my Activity which is filled with an ArrayAdapter
When I choose an Item from the Spinner, the ListView should be updated. But somehow there is a problem with my Spinner I guess. The ListView first updates when I clicked in a TextField of my Activity after I choose a SpinnerItem.
My code:
// Get a databaseConnection and fills the history variable with the data of
// the selected spinnerItem "history" is right filled with all Strings for
// the chosen Spinner from the database -> works correct
public void updateHistory() {
sqlLightDatabase database = new sqlLightDatabase(this);
ArrayList<String> history = database.getArrayList(
mySpinner.getSelectedItem().toString());
// set the adapter to the ListView
ArrayAdapter<String> listenAdapter = new ArrayAdapter<String>(this,
R.layout.list_item, history);
myListView.setAdapter(listenAdapter);
// the code works through, but the ListView is not shown the update
// instantly. But now, when I click a TextField in my Activity, myListView
// shows the new data
}
// Is instantly called when an Item is selected in mySpinner (in my ActionBar)
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position,
long id)
{
// the updateHistory function is called but myLitstView doesn't get
// updated with new Data
updateHistory();
//Just for testing. The following is working instantly fine.
drivenDistance_TF.setText("History changed");
}
I tried the last two days to fix this issue, but I have absolute no idea what I could else try to get the ListView instantly updated.
There is no ErrorLog, because there are no errors.
You must call notifyDataSetChanged method to update data in list view
So write this line
listenAdapter.notifyDataSetChanged();
after myListView.setAdapter(listenAdapter);
in updateHistory();

Android ListView items loose listener if too many items added

I have a listview on which I have onItemLongClickListener attached. Everything works fine but when I add too many (e.g. 100) items to the list, some of the views lose the listener. Not sure what the problem is, I also debugged the problem and still can't figure it out.
There is no exception being thrown either, it just stops working.
Then if I remove everything from the list and start to add things, it works fine as long as I don't add to many item.
Is it the memory issue?
I searched around everywhere but no luck.
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_frame);
// Set text view for orderTotal
orderTotal = (TextView) findViewById(R.id.orderTotal);
orderTotal.setText("0.00");
// List view from layout
itemsListView = (ListView) this.findViewById(R.id.item_list);
// Current order to be consistent throughout activities.
currentOrder = ((OrderIt)this.getApplication()).getCurrentOrder();
databaseCategories = (ArrayList<Category>) this.getIntent().getExtras()
.getSerializable("databaseCategories");
// List of items already in order.
itemsListView.setAdapter(new ItemAdapter(this, R.layout.list_item,
currentOrder.getItems()));
itemsListView.setOnItemLongClickListener(this);
this.registerForContextMenu(itemsListView);
// Gridview from layout
gridView = (GridView) findViewById(R.id.item_grid);
ArrayList<Item> items = getItems();
gridView.setAdapter(new ImageAdapter(this, items));
// Set click adapter to grid view items
gridView.setOnItemClickListener(this);
gridView.setOnItemLongClickListener(this);
}
I think:
Your item contains the EditText or Button which vies auto get focus from the item or listView.
you need to setFocus(false) or listView.setItemsCanFocus(false);
let the item views can't take the focus.
found the solution to the problem,
I was asking
adapterView.getChildAt(position)
which is not right way to do it if you looking to get underneath data,
right way is to call
adapterView.getItemAtPosition(position)
which returns underneath object which was used to populate listView.

Displaying empty message for a section in MergedAdapter

I'm using a MergedAdapter to group two custom adapters (ArrayAdapter derived) along with a section header for each one into a single ListView. It is working fine, but now I need to display a TextView saying "No Data" for the section that has no items, e.g. ArrayAdapter is empty.
What's the best approach for this? The code that sets the ListView binding is like this:
ArrayList<ItemOne> firstItems = getFirstGroupItems();
ArrayList<ItemTwo> secondItems = getSecondGroupItems();
ItemOneAdapter firstAdapter = new ItemOneAdapter(this, this.firstItems);
ItemTwoAdapter secondAdapter = new ItemTwoAdapter(this, this.secondItems);
MergeAdapter adapter = new MergeAdapter();
adapter.addView(createGroupSeparator(R.string.first_section_header)); //Just creates a TextView
adapter.addAdapter(firstAdapter);
adapter.addView(createGroupSeparator(R.string.second_section_header)); //Just creates a TextView
adapter.addAdapter(secondAdapter);
listView.setAdapter(adapter);
In case of an empty list, you could make the first item read "No Data", but this usually makes the code unmanageable. I would suggest you add an invisible TextView to your screen with the message; this is only displayed when the list is empty.

Categories

Resources