I'm attempting to get method to handle updates to the ListView when SetWeatherData is called. Nothing ever shows up in my listview below. Any ideas? _rootView points to the right root and ListView comes back not null. m_weatherdata has a couple string elements in it.
Note the initial set of data does not show up either. Just blank.
I'm thinking it should be easier to setup a generic method to update a ListView when the data changes using straight up code.
private ArrayList<String> m_weatherdata;
private void SetWeatherData ( ArrayList<String> _weather)
{
m_weatherdata = _weather;
UpdateWeatherUI();
return;
}
ArrayAdapter<String> m_adapter = null;
private void UpdateWeatherUI()
{
if ( m_adapter == null ) {
m_adapter = new ArrayAdapter<String>(
this.getContext(),
R.layout.list_item_forecast,
R.id.list_item_forecast_textview,
m_weatherdata);
View _rootview = this.getLayoutInflater(null).inflate(R.layout.fragment_main, null, false);
ListView _listview = (ListView) _rootview.findViewById(R.id.listview_forecast);
_listview.setAdapter(m_adapter);
}
else
{
m_adapter.notifyDataSetChanged();
}
}
You are assigning a new ArrayList to your dataset.
m_weatherdata = _weather;
Instead add items to the dataset. Like this
m_weatherdata.addAll(_weather);
private void SetWeatherData ( ArrayList<String> _weather)
{
m_weatherdata.addAll(_weather);//change here
UpdateWeatherUI();
return;
}
When you set an adapter there is an observer attached to the
underlying data. So notifyDatasetChanged() only works if you only
modify the data in it.
If you want to clear all data from your dataset before adding new items to it, use the clear() method of ArrayList
private void SetWeatherData ( ArrayList<String> _weather)
{
m_weatherdata.clear();//change here
m_weatherdata.addAll(_weather);//change here
UpdateWeatherUI();
return;
}
m_weatherdata = _weather; // updates the local variable with new set of data. but adapter doesn't know about the changes made as you have created the instance of the adapter with array list by the following line of code.
m_adapter = new ArrayAdapter<String>(this.getContext(), R.layout.list_item_forecast,R.id.list_item_forecast_textview,m_weatherdata);
In you want to updated the data either call
m_adapter.addAll(newsetofstringtobeadded);
or
Create new adapter
This will update your list.
Related
When I add a new item in my ListView it is losing the values that existed. It appears only the new value that was added. The goal is to add a new value without losing existing ones.My Code:
List<string> list = new List<string>();
void btnAddItem_Click(object sender, EventArgs e)
{
ArrayAdapter<string> adapter;
string edttxNewItem = FindViewById<EditText>(Resource.Id.newItem).Text;
ListView listItems = FindViewById<ListView>(Resource.Id.ListOfItems);
list.Add(edttxNewItem);
adapter = new ArrayAdapter<string>(this, Android.Resource.Layout.SimpleListItem1, list);
listItems.Adapter = adapter;
listItems.DeferNotifyDataSetChanged();
}
I am unfamiliar about xamarin,but it seems that you are trying to create listview instance each time on btnAddItem_Click.
So initialise listItems in oncreate method as
listItems = FindViewById<ListView>(Resource.Id.ListOfItems);
and define listItems as class variable.
And also set adapter outside of btnAddItem_Click,let say in OnCreate method.
adapter = new ArrayAdapter<string>(this, Android.Resource.Layout.SimpleListItem1, list);
In btnAddItem_Click just add edttxNewItem in listItems and do notifydatasetchanged equivalent in xamarin.
Every time you click in the button you are instantiating a new arraylist (List list = new List();). So, every previous values in the array are deleted
adapter = new ImageAdapter(this, imagelist, imageBool, resourceName, docBool);
adapter.NotifyDataSetChanged();
//gridview.Adapter = null;
gridview.InvalidateViews();
gridview.Adapter = adapter;
I send the list with the new values to the imageadapter and it sorts the values out and returnes the items as images and alls is good. But when i want to refresh the items the old ones just stays the same and the new ones never arrives on the screen. Ive tried a lot of different ways to reload the grid but it doesn't work.
Add following methods to your customized adapter ie. ImageAdapter.
// Call it once , first time when you want to pass the data to adapter
public void setImageList(List<YOUR DATA TYPE> imagelist) {
this.imagelist = imagelist;
}
// Call this method whenever new data is to be added to existing list.
public void updateImageList(List<YOUR DATA TYPE> newImagelist) {
if(this.imagelist != null){
this.imagelist.addAll(newImagelist);
}else{
this.imagelist = imagelist;
}
notifyDataSetChanged();
}
And call it once the new data is downloaded.
Just create a new ImageAdapter with all the Images (including the old ones and the new ones) and assign it to the gridview. This way it works without problems.
After that call
adapter.notifyDataSetChanged();
and it will work fine.
public void UpdateData(ArrayList<HashMap<String,String>> array_list){
GridView glist = (GridView) findViewById(R.id.tipss_grid)
adapter2 =new CurrentAdapter(CurrentChanels.this,array_list);
glist.setAdapter(adapter2);
}
I call this method to populate data in gridview. I m displaying currently running programs. After every 1 min I call this method to refresh the data. The problem is that when user is on the last element of gridview and mean while I refresh it then control move to top of the screen. I do not want the screen to move,it must stay where it is before refresh. Any suggestions?
This is because every one minute you are creating a new Adapter and assigning it to the GridView.
Implement a new method resetData() in CurrentAdapter:
public void resetData(List<HashMap<String,String>> list) {
_list.clear();
_list.addAll(list);
notifyDataSetChanged();
}
Call resetData() whenever you want to refresh the grid:
GridView glist = (GridView) findViewById(R.id.tipss_grid);
if (glist.getAdapter() == null) {
CurrentAdapter adapter2 = new CurrentAdapter(CurrentChanels.this,
array_list);
glist.setAdapter(adapter2);
} else {
CurrentAdapter adapter2 = ((CurrentAdapter)glist.getAdapter());
adapter2.resetData(array_list);
}
use this line after setadapter line
glist.setSelection(adapter2.getCount() - 1) ;
You can call adapter.notifyDataSetChanged() but if you creating new ArrayList you have to set new adapter. You should change data in old ArrayList if you want notifyDataSetChanged() work.
I have a listview that shows the contents of an arraylist. I'm using a simple adaptor to make this possible like so.
public static ArrayList<String> homeScreenContacts = new ArrayList<String>();
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
R.layout.home_screen_contacts_view, NewContact.homeScreenContacts);
The second line is giving me a null pointer exception. I thought about it and I decided it was because the arrayList is empty. So I added the following line between the arraylist declaration and the arrayadaptor declaration...
NewContact.homeScreenContacts.add("A Contact");
This solved the problem and my code worked fine but Now the list view shows "A Contact" and I dont want it to. Is there anyway to get rid of the null pointer exception problem but still have the arraylist empty? Because I want to populate it with user made contacts, not hard-coded, random strings. Thank you.
EDIT: Sorry, The arraylist is located in another class called NewContact, also, I am very beginner Android Programmer I just started.
Simple solution just don't initialize the ListView if there is no element in the ArrayList or the ArrayList is null.
if(NewContact.homeScreenContacts != null && NewContact.homeScreenContacts.size() > 0){
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
R.layout.home_screen_contacts_view, NewContact.homeScreenContacts);
listView.setAdapter(adapter);
}
Also you need to remember that if you you haven't initialize Adapter then dont initialize the ListView and before any operation on list view you should check is it null or not.
As you have said that you want to populate when user add some contact in the application then on add event only you need to populate or update the ListAdapter.
Hope this solution will resolve your problem.
Try this code
public class YourActivity extends Activity
{
private ListView lv;
public void onCreate(Bundle saveInstanceState) {
setContentView(R.layout.your_layout);
lv = (ListView) findViewById(R.id.your_list_view_id);
// Instanciating an array list (you don't need to do this, you already have yours)
ArrayList<String> your_array_list = new ArrayList<String>();
your_array_list.add("foo");
your_array_list.add("bar");
// This is the array adapter, it takes the context of the activity as a first // parameter, the type of list view as a second parameter and your array as a third parameter
ArrayAdapter<String> arrayAdapter =
new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, your_array_list);
lv.setAdapter(arrayAdapter);
}
}
Works fine for me:
if(arrayList.isEmpty())
{
listView.setAdapter(null);
}
else
{
listAdapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,arrayList);
listView.setAdapter(listAdapter);
}
I have ArrayAdapter with this items structure:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout ... >
<TextView
android:id="#+id/itemTextView"
... />
</RelativeLayout>
And add this adapter so:
mAdapter = new ArrayAdapter<String>(this, R.layout.item,
R.id.itemTextView, itemsText);
All is fine but I want to update text in adapter's items. I found a solution
mAdapter.notifyDataSetChanged();
but do not understand how to use it. Help please.
upd
My code:
String[] itemsText = {"123", "345", "567"};
ArrayAdapter<String> mAdapter;
onCreate
mAdapter = new ArrayAdapter<String>(this, R.layout.roomitem,
R.id.itemTextView, itemsText);
setListAdapter(mAdapter);
itemsText = {"789", "910", "1011"};
onClick
mAdapter.notifyDataSetChanged();
//it's dont work
I think something like this
public void updatedData(List itemsArrayList) {
mAdapter.clear();
if (itemsArrayList != null){
for (Object object : itemsArrayList) {
mAdapter.insert(object, mAdapter.getCount());
}
}
mAdapter.notifyDataSetChanged();
}
Your problem is a typical Java error with pointers.
In a first step you are creating an array and passing this array to the adapter.
In the second step you are creating a new array (so new pointer is created) with new information but the adapter is still pointing to the original array.
// init itemsText var and pass to the adapter
String[] itemsText = {"123", "345", "567"};
mAdapter = new ArrayAdapter<String>(..., itemsText);
//ERROR HERE: itemsText variable will point to a new array instance
itemsText = {"789", "910", "1011"};
So, you can do two things, one, update the array contents instead of creating a new one:
//This will work for your example
items[0]="123";
items[1]="345";
items[2]="567";
... or what I would do, use a List, something like:
List<String> items= new ArrayList<String>(3);
boundedDevices.add("123");
boundedDevices.add("456");
boundedDevices.add("789");
And in the update:
boundedDevices.set("789");
boundedDevices.set("910");
boundedDevices.set("1011");
To add more information, in a real application normally you update the contents of the list adapter with information from a service or content provider, so normally to update the items you would do something like:
//clear the actual results
items.clear()
//add the results coming from a service
items.addAll(serviceResults);
With this you will clear the old results and load the new ones (think that the new results should have a different number of items).
And off course after update the data the call to notifyDataSetChanged();
If you have any doubt don't hesitate to comment.
Assuming itemTexts as String array or String ArrayList,where you are adding new items into itemsTextat that time after that you can call
mAdapter.notifyDataSetChanged();
If you did not get answer then please put some code.
I did something like this. And it works correctly.
Add method to the Adapter class:
public void updateList(ArrayList<ITEM> itemList){
this.itemList.clear();
this.adapterList = new ArrayList<ITEM>();
this.adapterList .addAll(itemList);
notifyDataSetChanged();
}
Call the method in the class you use the adapter:
itemList.add(item);
adapter.updateList(itemList);