i have one arraylist inside onCreateView() of fragment which contains the data to be updated inside listview, i am using interface callback to update the data from another activity as follow:
listUpdatable.update(nameStr,otpStr,timeStr);
my update method inside the fragment is as follow:
#Override
public void update(String name, String time, String otp) {
SentMessageModel message = new SentMessageModel();
message.setOtp(otp);
message.setName(name);
message.setTime(time);
messages.add(0, message);
adapter.notifyDataSetChanged();
}
The app carashes on calling update method is showing the NPE on arraylist.
What should i do?
View view = inflater.inflate(R.layout.fragment_sent_message_history, container, false);
ListView listView = view.findViewById(R.id.sent_message_listView);
emptyText = view.findViewById(R.id.empty_text);
messages = new ArrayList<>();
databaseCodeForAll(); // fetches the content of messages arraylist from sqlite
adapter = new SentMessageAdapter(getContext(), messages);
listView.setAdapter(adapter);
The code is inside onCreateView().
Related
In my main activity I have a RecyclerView which is loaded with data from an array list:
protected void onReady(Bundle savedInstanceState) {
...
// create summary item array & populate it based on task item array
_alData = new ArrayList();
PopulateDataList();
_rv = (RecyclerView) findViewById(R.id.rvDataList);
_li = getLayoutInflater();
_rv.setLayoutManager(new LinearLayoutManager(this));
_adapter = new TaskItemAdapter();
_rv.setAdapter(_adapter);
...
}
When the back button is tapped in a second activity, it returns to my main activity, but the RecyclerView is not updated with the new data. So I want to add the following:
public void onResume() {
super.onResume(); // Always call the superclass method first
// clear out & re-populate summary list, in case something has changed
_alData.clear();
PopulateDataList();
// *** WHAT GOES HERE...???
}
I don't know how to tell the RecyclerView to re-bind the data from the repopulated list... Any help would be appreciated!
Add this after populateDataList() :
_adapter.notifyDataSetChanged();
I am trying to populate my Listview using the data from another fragment.
I am able to get the data from the other fragment, but when I try to make my listview object, it is returning null.
As a result, the app is crashing.
I am getting data from the user from one fragment and then calling a method from another fragment to pass the data. I am making my listview object and array adapter in the poplist() method of the second method. However, the app is crashing due to null pointer exception.
Please help.
public class WishListFragment extends Fragment {
ArrayList<String> wishListTitles = new ArrayList<String>();
View rootView;
ListView wishListView;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
Context context = getActivity();
rootView = inflater.inflate(R.layout.fragment_wish_list, container, false);
return rootView;
}
// Another fragment gets the data from user as strings and calls this method
public void popList(String str1, String str2, String str3, Context context)
{
// LibModel is a pojo, I am using its constructor to set the values.
LibModel lm = new LibModel(str1,str2,str3);
Log.w("Title:",lm.getTitle());
Log.w("Author:",lm.getAuthor());
Log.w("Language:",lm.getLang());
wishListTitles.add(lm.getTitle());
// I get this on the log, so the ArrayList is made correctly
Log.w("ArrayList:",wishListTitles.get(0));
// The listView gives Null Pointer exception and crashes the app
wishListView = (ListView) rootView.findViewById(R.id.wl_lv);
ArrayAdapter<String> wishListAdapter = new ArrayAdapter<String>(context,
android.R.layout.simple_list_item_1,wishListTitles);
wishListView.setAdapter(wishListAdapter);
}
}
I have tried the following, but it does not work
Used getView method instead of rootView while making the Listview.
Tried to make the listview inside the onCreateView() method but then the listview object is null, I get null pointer.
I am unable to find a way to put the set the adapter for the listview as it is returning Null.
I would recommend having interface callback in the fragment with the data. Also keep a reference of the list view in the activity containing the fragment.
Upon callback, the interface function will be executed and you can update list view from there.
I have three fragments that contains three ListView created using a BaseAdapter. Like the image below:
Anyway my listView is fetching data from an SqliteDatabase. What I need to know is: when I delete an item from my ListView My(Favorite,Rejected) Fragments ListViews are not notified and are not refreshing.
What I have tried so far is :
Call listView.invalidateViews() after notifyDataSetChanged() in the onResume() Method of my fragments .
I tried these solution two Android ListView not refreshing after notifyDataSetChanged
My code is :
In My BaseAdapter I'am using these method to refresh my adapter :
public void UpdateView(List<Voiture> items)
{
this.voitureList = items;
notifyDataSetChanged();
}
In My fragments I'am using these method to notify the adapter :
#Override
public void onResume() {
super.onResume();
adapterLogin.UpdateView(databaseHelper.getAllVoiture(username,currentLength));
listView.setAdapter(new AdapterLogin(getActivity(),voitureList,username,currentLength,1));
}
In the OncreateView() Method I'am using :
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
inflate = inflater ;
x = inflater.inflate(R.layout.fragemnt_favoris,null);
empty = (TextView) x.findViewById(R.id.texteempty);
listView = (ListView) x.findViewById(R.id.list);
activity = (Main2Activity) getActivity() ;
username = activity.getUsername();
databaseHelper = new DatabaseHelper(getActivity());
databaseHelper.InsertActivity(2,username);
voitureList = databaseHelper.getAllVoitureFavourite(1,username);
adapterLogin = new AdapterLogin(getActivity(),voitureList,username,currentLength,2);
if (voitureList.size()>0)
{
listView.setAdapter(adapterLogin);
((BaseAdapter)listView.getAdapter()).notifyDataSetChanged();
}
else
{
empty.setVisibility(View.VISIBLE);
}
// Inflate the layout for this fragment
return x;
}
Any help would be greatly appreciated.
Thanks to Paul answer the idea was to add the notifyDataSetInvalidated in my UpdateView() Method in my adapter and it works just fine and my fragments are refreshing correctly now :
void notifyDataSetInvalidated ()
the method Notifies the attached observers that the underlying data is no longer valid or available. Once invoked this adapter is no longer valid and should not report further data set changes.
refer link :
notifyDataSetInvalidated
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.
I have got a custom list view adapter and an image button in the adapter class. When i click on the image button, the listener should reload the list view. I need to reload the list view within getview() of adapter class. So I need to know other options than using notifyDataSetChanged() in my listActivity class.
Thanks
You want to refresh a cell inside the listview or do you want to refresh the whole listview, if a single row is loaded inside getView() ?
Check this out:
Android ListView Refresh Single Row
Create a static handler inside the activity which calls a method which reloads the listview and send a message to this handler from the adapter whenever required.
handler = new Handler() {
public void handleMessage(Message paramAnonymousMessage) {
switch (paramAnonymousMessage.what) {
case 1:
populateList();
break;
}
}
};
public void populateBill() {
MyBasketAdapter adapter = new MyBasketAdapter(this, basketList);
listView = (ListView) findViewById(android.R.id.list);
listView.setAdapter(adapter);
}
Inside the adapter class. for example,
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Message msg = Message.obtain();
msg.what = 1;
MyActivity.handler.sendMessage(msg);
}
});
That is very Simple just write a method in your adapter class and call it get view when you deleting or adding anything in your list which you are binding to your adapter.and use notifyDataSetChanged after change in list
public void updateResults(ArrayList<CustomList> results) {
// assign the new result list to your existing list it will work
notifyDataSetChanged();
}