I tried following the recommendations here (How to refresh Android listview?) to update my ListView, but to no avail.
The relevant code is below. Please note that I call runOnUiThread in my MainActivity to refresh the ListView of this (fragment).
protected ArrayAdapter<String> adapter;
protected View view;
protected ArrayList<String> theList;
protected ListView listView;
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
view = inflater.inflate(R.layout.fragment_1, container, false);
listView = (ListView) view.findViewById(R.id.listview);
theList = new ArrayList<String>();
adapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1, theList);
listView.setAdapter(adapter);
if(view != null) { return view;}
((ViewGroup) listView.getParent()).removeView(listView);
container.addView(listView);
return view;
}
protected void updateListViewWithPath(String resultFromServerRead) {
//TODO: Somehow split the information..
theList.add("yo");
theList.add("hey");
adapter.clear();
adapter.addAll(theList);
listView.invalidateViews();
adapter.notifyDataSetChanged();
listView.refreshDrawableState();
}
You already have the reference to the arraylist list so you dont need to add it again to the listView just simply add the string to the arraylist and notifyDataSetChanged;
example:
protected void updateListViewWithPath(String resultFromServerRead) {
//TODO: Somehow split the information..
theList.add("yo");
theList.add("hey");
adapter.notifyDataSetChanged();
}
Related
enter image description hereBelow is the coding i have and it seems i have problem on the
AgentAdapter adapter = new AgentAdapter(getActivity(), R.layout.list_agent, member_names, profile_pics);
How do i use image and text together for the list adapter i have in my fragment? Please help.
public class AgentFragment extends Fragment{
String[] member_names;
TypedArray profile_pics;
ListView mylistview;
String[] agentname={"Robert","Shanni","Rachel","Mady","Nikhil"};
Integer [] imgid = {R.drawable.robert,R.drawable.shanni,R.drawable.rachael,R.drawable.maddy_pic,R.drawable.nikhil_pic};
public AgentFragment(){
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.list_layout1, null);
setupList(view);
return view;
}
private void setupList(View view){
AgentAdapter adapter = new AgentAdapter(getActivity(), R.layout.list_agent, member_names, profile_pics);
mylistview = (ListView) view.findViewById(R.id.listView);
mylistview.setAdapter(adapter);
}
}
Since its possible that the fragment has not been placed in any activity at runtime you can afford for this by changing the getActivity() to be the context from the view parameter:
private void setupList(View view){
AgentAdapter adapter = new AgentAdapter(view.getContext(), R.layout.list_agent, member_names, profile_pics);
mylistview = (ListView) view.findViewById(R.id.listView);
mylistview.setAdapter(adapter);
}
I'm trying to display data from SQLite and display it in the listview in fragment.This is my code and when I run it, it closes automatically. This code is inside my main activity
public static class ListDoctorFragment extends Fragment {
ListView list;
DataDB data = new DataDB();
ArrayAdapter<String> listAdapter;
public ListDoctorFragment() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
final View view = inflater.inflate(R.layout.listdoctor, container, false);
ArrayList<String> names = new ArrayList<String>();
try {
names = data.getDoctorlistDB(getActivity());
} catch (SQLException e) {
e.printStackTrace();
}
listAdapter = new ArrayAdapter<String>(getActivity(), R.layout.support_simple_spinner_dropdown_item, names);
// set the adapter
list.setAdapter(listAdapter);
return view;
}
}
Your 'ListView list' is not initialized. It must be something like:
list = (ListView) view.findViewById(R.id.listview);
My listview gets filled on the start but after i cant find a way to make changes
pls see the code bellow
public class myFragment extends android.support.v4.app.Fragment
{
ListView list;
ArrayList<String> restorants=new ArrayList<String>();
Manager m=new Manager();
ArrayAdapter<String> adapter;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_, container, false);
list=(ListView) rootView.findViewById(R.id.myList);
for(Restorant r : m.getResByCity("nyc"))
{
restorants.add(r.getName());
}
adapter=new ArrayAdapter<String>(getActivity(), R.layout.item_list, R.id.restorantItem,restorants);
list.setAdapter(adapter);
return rootView;
}
public void makeChange(String s)
{
restorants.remove(0);
((ArrayAdapter)list.getAdapter()).notifyDataSetChanged();
}
}
While calling the method makeChange() i have checked that the restorants items are removing so that there is no problem with calling the method
makeChange() is called from my mainActivity that contains viewpager where this fragment is contained in the viewpager.
you need to remove the element from the dataset you provided to the adapter,
adapter.remove(s)
Here you can find the documentation
Here's the code that's producing unexpected results. If anyone can please guide me to what I'm doing wrong here
public static class LaunchpadSectionFragment extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_section_launchpad, container, false);
ListView moviesList= (ListView) rootView.findViewById(R.id.movies_list);
ArrayAdapter<String> moviesAdapter;
List<String> Testing = new ArrayList<String>();
Testing.add("Hey");
Testing.add("Hey");
Testing.add("Hey");
Testing.add("Hey");
moviesAdapter= new MyAdapter(getActivity(),android.R.layout.simple_list_item_1,Testing);
moviesList.setAdapter(moviesAdapter);
return rootView;
}
Ok after playing around a Lot going through documentation I finally found a workaround to this.
This is what I did
#Override
public void onActivityCreated(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onActivityCreated(savedInstanceState);
super.onActivityCreated(savedInstanceState);
Log.d("CreatedActivity", "LaunchpadFragment");
ListView moviesList= (ListView) getActivity().findViewById(R.id.movies_list);
// Gets the ListView from the View list of the parent activity
// Gets a CursorAdapter
ArrayAdapter<String> moviesAdapter;
List<String> Testing = new ArrayList<String>();
Testing.add("Hey");
Testing.add("Hey Do");
Testing.add("Hey It");
Testing.add("Hey Please");
moviesAdapter= new ArrayAdapter<String>(getActivity(),android.R.layout.simple_list_item_1, Testing);
moviesList.setAdapter(moviesAdapter);
}
Note the over-ridden method here, it is
onActivityCreated
Android documentation states
If your view of the fragment is a listview that is being filled by a data adapter, you should override the onActivityCreated method and do the filling in there.
I don't have a documentation link to this blockquote though:|
I have a simple MyListFragment fragment which shows one simple array of data on the screen.
public class MyListFragment extends ListFragment {
private ArrayAdapter<String> adapter;
public ArrayAdapter<String> getAdapter() {
return adapter;
}
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
String[] values = new String[] {"data", "data", "data"};
adapter = new ArrayAdapter<String>(getActivity(),
android.R.layout.simple_list_item_1, values);
setListAdapter(adapter);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return super.onCreateView(inflater, container, savedInstanceState);
}
}
I wonder how can I change this data dynamically from my Activity? Please tell me if there is a simple way how to do this! I've tried but have failed...
UPD: I added a function changeContent to MyListFragment:
public void changeContent(String[] value) {
adapter.addAll(value);
adapter.notifyDataSetChanged();
}
}
As you see, adapter is a global variable and it's initialized inside onActivityCreated, but my program crashes because inside changeContent in equals null! I can't get why!
You could write a function that changes your values variable and then once you have filled it with your new data just call
adapter.notifyDataSetChanged();
to fill your list with the new data.