Cannot Resolve Symbol ArrayList on OncreateView (fragment) - android

I want to initialize a new array list in a Fragment (onCreateView), but it says (cannot resolve symbol) and I don't know why.
How to fix that? since it seems that some statements change in the onCreateView method.
Thx

Ok, I have found the solution.
I just needed to add an ArrayList , before initializing.
ArrayList articlesList = new ArrayList<Articles>();
ListView listview = (ListView) view.findViewById(R.id.list);
ArticlesAdapter adapter = new ArticlesAdapter(getActivity().getApplicationContext(), R.layout.articles, articlesList);

Related

Custom adapter size 0 after notifyDataSetChanged()

I have a ListView, an ArrayList and an MaterielAdapter that extends ArrayAdapter. My problem is that calling notifyDataSetChanged() won't update my ListView... I spent 3 hours trying all similar issues on SO but none was efficient for me.
private ListView lvMateriels;
private ArrayList<Materiel> materiels = new ArrayList<>();
...
lvMateriels = new ListView(this);
lvMateriels.setAdapter(new MaterielAdapter(this, R.layout.object_line,materiels));
...
materiels.add(new Materiel());
((MaterielAdapter)lvMateriels.getAdapter()).notifyDataSetChanged();
My collection has all the objects but listview never refreshes, nor the adapter...
You're probably not even adding the ListView to the activity view. Instead of doing new Listview(this), I'd recommend you to define a listview in your layout xml
Something like:
<ListView
android:id="#+id/listview"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
Then in your activity:
lvObjects = (ListView) findViewById(R.id.listview);
Found out solution
My problem was that I was setting my ArrayList another ArrayList
materiels = getMateriels() // getMateriels() returns an ArrayList<Materiel>
It seems that setting a new object to the list doesn't work, what I finally did is the following :
materiels.clear();
materiels.addAll(getMateriels());
And by the way for those who need to have a header into their ListView, you have to do like :
((YourAdapter)((HeaderViewListAdapter)yourListView.getAdapter()).getWrappedAdapter()).notifyDataSetChanged();
Stock your adapter, and your adapter will be your list too.
MaterialAdapter adapter = new MaterialAdapter(this,
R.layout.object_line);
lvObjects = new ListView(this);
lvObjects.setAdapter(adapter);
...
adapter.add(new Object());
adapter.notifyDataSetChanged();
Currenlty your adapter, do not know that he need to use the list that you pass in argument. But you extends ArrayAdapter, that means that your adapter is a list.

Android Listview Item remove but adapter not getting set and item is still showing?

I have this code on Button click
I am removing item from ListView but problem is its still showing on adapter.
restaurants_bean_arr.remove(restaurants_bean_arr.get(item_position).facilities.get(item_position).getUrl());
restaurants_bean_arr.remove(restaurants_bean_arr.get(item_position).facilities.get(item_position).getName());
DisplayDataAdapter adapter = new DisplayDataAdapter(getApplicationContext());
adapter.notifyDataSetChanged();
listView.setAdapter(adapter);
Any Idea what could be the problem that item is not getting removing from object class ArrayList.
Thanks
Switch order of:
adapter.notifyDataSetChanged();
listView.setAdapter(adapter);
To:
listView.setAdapter(adapter);
adapter.notifyDataSetChanged();
It seems like you are creating a new Adapter every time you change your dataset and set this new Adapter to your ListView. You don't have to do that. You can just make changes to your dataset and then call notifyDataSetChanged() on your Adapter.
So your code should look like this:
restaurants_bean_arr.remove(restaurants_bean_arr.get(item_position).facilities.get(item_position).getUrl());
restaurants_bean_arr.remove(restaurants_bean_arr.get(item_position).facilities.get(item_position).getName());
adapter.notifyDataSetChanged();
Here is problem
DisplayDataAdapter adapter = new DisplayDataAdapter(getApplicationContext());
you are creating a new adapter. Create a global value adapter and try like this:
restaurants_bean_arr.remove(restaurants_bean_arr.get(item_position).facilities.get(item_position).getUrl());
restaurants_bean_arr.remove(restaurants_bean_arr.get(item_position).facilities.get(item_position).getName());
adapter.notifyDataSetChanged();

Android null pointer exception on ArrayList for ListView

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);
}

listview not updating with notifydatasetchanged() call

This is my code
listview =(ListView) findViewById(R.id.lv1);
ArrayList<SClass> Monday = new ArrayList<SClass>();
SClass s1=new SClass();
s1.sName="samp";
s1.salary=1000;
Monday.add(s1);
temp=Monday;
adapter = new CustomAdap(this, temp);
listview.setAdapter(adapter);
The above code works fine.But when i change my code to this
listview =(ListView) findViewById(R.id.lv1);
adapter = new CustomAdap(this, temp);
SClass s1=new SClass();
s1.sName="samp";
s1.salary=1000;
Monday.add(s1);
temp=Monday;
listview.setAdapter(adapter);
adapter.notifyDataSetChanged();
Listview does not show anything.what is the problem?
It looks like you're changing the collection that you initialized adapter with. I would change your code in this way:
// initial setup
listview =(ListView) findViewById(R.id.lv1);
ArrayList<SClass> Monday = new ArrayList<SClass>();
adapter = new CustomAdap(this, Monday);
listview.setAdapter(adapter);
// change your model Monday here, since it is what the adapter is observing
SClass s1=new SubjectClass();
s1.sName="samp";
s1.salary=1000;
Monday.add(s1);
// notify the list that the underlying model has changed
adapter.notifyDataSetChanged();
Note that if your CustomAdap was a subclass of ArrayAdapter, you could also have done
// change your array adapter here
SClass s1=new SubjectClass();
s1.sName="samp";
s1.salary=1000;
adapter.add(s1);
// notify the list that the underlying model has changed
adapter.notifyDataSetChanged();
EDIT: I understand more what you want to do now thanks to your comment. You'll probably want to have the adapter replace its contents with that your different ArrayLists then. I would make your CustomAdap be a subclass of ArrayAdapter.
Then you can utilize it this way:
// replace the array adapters contents with the ArrayList corresponding to the day
adapter.clear();
adapter.addAll(MONDAY);
// notify the list that the underlying model has changed
adapter.notifyDataSetChanged();
Why it works in first code ?
--- Because you are setting the values to temp List and passing it the adapter and it shows it into listview.
Why not work in second code ?
--- Because you are setting temp to adapter far before you set value into temp
second,your adapter class might not getting the updated value when you set new value to temp ..that because temp is not public or not at class level or not static..
Put the temp declaration at root level and try.
And please show your full code as much as required and Logcat if you getting any warnings than also.
Check for a link to your referenced view in the proper xml file. Or at least check for the existence of said xml file.
What adapter are you using? It is clearly a case where your adapter is not getting updated after u set the data in your temp variable.

How do you add array to listview

I have an array of apps(PInfo) and I am wondering how do I add that array to a listview?
ArrayList<PInfo> info = appsGetter.listPackages();
int number = 0;
PInfo appInArray;
while(number < info.size()){
appInArray = info.get(number);
}
This is what I have at the moment, the listPackages() is a method that is getting the names of the apps from the device.
At the moment I am trying to get the information out of the array one by one and add it to the listview like that. Is that how I should do it our should I add the array straight to the listview? And how do you do that?
You can use an ArrayAdapter and initialize it like this:
ArrayAdapter<PInfo> adapter = new ArrayAdapter(context,
android.R.layout.simple_list_item_multiple_choice,
info);
Then you can you use ListView.setAdapter(adapter).
I'm not sure if this is what you're asking though. So please clarify further if this is not what you're asking
Try using an Adapter. For example (using just the String value of an object) you could do the following:
ListView listView = (ListView)findViewById( R.id.myListView );
final ArrayList<String> listItems = new ArrayList<String>();
final ArrayAdapter<String> adapter = new ArrayAdapter<String>( this, android.R.layout.simple_list_item_1, listItems );
listView.setAdapter( adapter );
Just a quick example, but I hope it gives you a starting place. Just make sure if you add values to your data source later (in this case the ArrayList) to call the adapter's "notifyDataSetChanged()" method so that it can be properly reflected in whatever has been bound to the adapter (in this case the ListView).
You need to use an ArrayAdapter. Just search for a ListView and ArrayAdapter sample online. It's quite simple once you see it done.

Categories

Resources