Requesting logic for listview fragment - android

I'm making an activity with a listView fragment in it. I am having a button click on the MainActivity load items for an rss feed that populate the listview fragment.
My question is, how should I go about setting it up? I already have the code for it.
My plan now is to have the button click on MainActivity point to my method in my RSSFragment which extends ListFragment. I'm confused about some things like which activity to pass here:
#Override
protected void onPostExecute(Exception s) {
super.onPostExecute(s);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1, titles);
lvRss.setAdapter(adapter);
progressDialog.dismiss();
}
}
Should I use getActivity()? or Pass MainActivity or RSSFragment.this?
Also, how do I set list adapter to a listview fragment? Imagine lvRss isn't there.
I know it's a long question.
Thanks.

Should I use getActivity()? or Pass MainActivity or RSSFragment.this?
If you are in Activity the use this or Activity.this . IF you are in Fragment the use getActivity().
Also, how do I set list adapter to a listview fragment? Imagine lvRss isn't there.
Use setListAdapter method setting Adapter to ListFragment

Related

How do I update ListView in another Fragment?

I have an Activity which holds a ViewPager with 2 Fragments. One fragment is for adding items to ListView and another fragment is holding the ListView.
I've been trying for almost 3 days now without any positive results. How do I update the other fragment's ListView from the first fragment?
I'm trying to call the method that updates ListView from the Activity that holds ViewPager but it doesn't work.
Calling the method from ViewPager activity :
#Override
public void onPageSelected(int position) {
library.populateListView(getApplicationContext());
aBar.setSelectedNavigationItem(position);
}
This is the populateListView method:
public void populateListView(Context context){
CustomListViewAdapter customAdapter = new CustomListViewAdapter(getDatabaseArrayList(context), getActivity());
if (lView != null)
{
lView.setAdapter(customAdapter);
}
customAdapter.notifyDataSetChanged();
}
However this doesn't work because the lView variable (ListView) is null because the fragment isn't shown at the moment when this is being called.
I am assuming that function populateListView() is a function of the Fragment containing the ListView. You are calling populateListView() on every call to onPageSelected. Should you not check what is the position that is being selected. Anyway the populateListView() method should be a public method of the Fragment containing ListView. And You Can Instantiate The Fragment from the Viewpager adapter in the Activity and than call this method. In That way the listView should not be null.
#Override
public void onPageSelected(int position) {
ListViewFragment frag=(ListViewFragment)adapter.instantiateItem(viewPager, 1);
//here adapter is the ViewPager Adapter and you must supply the viewpager that contains
//the fragments and also the position of the fragment to instantiate.
//For example 0 or 1 etc.
frag.populateListView(getApplicationContext());
aBar.setSelectedNavigationItem(position);
}
Understand Fragments
Please see this link. I have gone in great detail explaining the concept of fragments.
Pay particular attention to the definition of rootview:
public void onActivityCreared(Bundle savedInstanceState){
super.onActivityCreated(savedInstanceState);
// Do stuff on creation. This is usually where you add the bulk of your code. Like clickListners
// You can define this object as any element in any of your xml's
View rootview = inflater.inflate(R.layout.xml_the_fragment_uses container,false);
rootview.findViewById(R.id.your_id).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//Do something
}
});
}
In the above case I defined a button for an on click listener, but you can just as easily define a ListView along with its appropriate methods.
Alternate Solution
A second method could be using getView or getActivity (check the communicating with activity section).
For example:
ListView listView = (ListView) getView().findViewById(R.id.your_listView_id);
OR (more likely solution for your problem)
View listView = getActivity().findViewById(R.id.list);
Please read this post for additional information.
Good Luck.
To do this safely, you have to keep your listview data in a higher level Parent-Activity not the fragments. Make your MainActivityclass singleton class by making the constructor private and create a getInstance method that return the only initialized instance of your `MainActivity.
This will allow you to keep your data instance safe from being re-initialized or lost. Then, in onResume of your fragment re-set the data (get it from the MainActivity) to your listview adapter and call notifyDataSetChanged() method from the adapter instance.
This will do the trick.

Android adapter notifyDataSetChanged() doesn't clear previous list

I saw that there were many problems regarding notifyDataSetChanged(). I've looked through many of them and none of the solutions worked for me.
I am using ArrayList to set my list, and after my ArrayList is updated, i ran notifyDataSetChanged(). The new list is added onto the previous one.
lets say first list is a,b and new list is a,b,c. what i get in the end is a,b,a,b,c.
and each time updating this happens again with the new list.
I've tried other such as invalidate(), adapter clear(), refreshDrawableState() etc and nothing worked.
Thank you in advance.
Here is the simplified code, a note that changing MainActivity extends Activity to ListActivity crashes the program even after i change the code in .xml file.
public class MainActivity extends Activity
{
ArrayList<String> names = new ArrayList<String>();
ArrayAdapter arrayAdapter = null;
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ListView lv1 = (ListView) findViewById(R.id.listview);
arrayAdapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, names);
lv1.setAdapter(arrayAdapter);
}
//code here to edit the ArrayList names.
public void onResume()
{
super.onResume(); // Always call the superclass method first
//the activity need to be updated everytime it resumes.
arrayAdapter.notifyDataSetChanged();
}
}
Seems like you are not properly updating the array list. Please try logging the array list contents after updating.

Activity refresh command from custom listview

Environment: Android 2.2+
I have an Activity. The Activity creates a custom listview with its adapter and populates it. When the user clicks on an item (an ImageView thumbnail) in the list, a context menu is shown. The repercussion of selection is that the view of Activity needs to be refreshed.
My question is: How can I send a command from the onClick of the custom adapter to the Activity asking to refresh itself?
Activity
...
ListView lv;
lv = (ListView) findViewById(R.id.lvlist);
lv.setAdapter(null);
MyCustomAdapter lvAdapter = new MyCustomAdapter(...);
lv.setAdapter(lvAdapter);
lv.setOnItemClickListener(myClickListener);
...
MyCustomAdapter (There is a thumbnail ImageView inside each list item called ivLogo)
...
ivLogo.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// Carry out some tasks
// At this point inform Activity that it needs to refresh
}
});
It must be noted that I'm not looking for anything in myClickListener. I want a signal from the thumbnail's onClickListener.
I scoured the internet, but could not find anything helpful for this specific case.
Any help/pointers will be much appreciated.
TIA
-sph
add this line to your onClick()
lvAdapter.notifyDataSetChanged();
you write his code whenever you need adpater refreshed...its worked
lvAdapter .notifyDataSetChanged();

How to refresh ListView in Android

I know you can call notifyDataSetChanged(); How would you do that in this context -- is it even possible?
ListView listView = getListView();
listView.setTextFilterEnabled(true);
setListAdapter(new ArrayAdapter<String>(Items.this, R.layout.list, items));
Edit: I have changed to one of the suggestions below. Here is what I am doing:
protected void onPostExecute(Void v) {
adapter = new ArrayAdapter<String>(Items.this, R.layout.list, items);
setListAdapter(adapter);
adapter.notifyDataSetChanged();
}
Here I am refreshing inside an AsyncTask method. This is being called inside a Dialog with a Submit button on top of a ListLiew. The Dialog inserts new data into a database and I want the list refreshed where to reflect this. I have tried this code above AND inside the onClick for the Button in the Dialog. Right before dialog dismissed.
You need to initialize the ArrayAdapter by itself:
ArrayAdapter<String> adapter = new ArrayAdapter<String>(Items.this, R.layout.list, items);
setListAdapter(adapter);
...then call notifyDataSetChanged() on that:
adapter.notifyDataSetChanged();
Note that you only need to call this method if the data actually changes; given this, you probably need to have it stored as a member variable in the class so it can be accessed later.
You should just be able to call notifyDataSetChanged() on your ArrayAdapter, since ArrayAdapter extends BaseAdapter
http://developer.android.com/reference/android/widget/ArrayAdapter.html
In your example you'd do something like this, taking that you meant to call listView.setListAdapter()
(ArrayAdapter)listView.getAdapter()).notifyDataSetChanged();
In your example, you shouldn't be setting the adapter in your AsyncTask. Generally it is a good idea to do that in onCreate() because otherwise you are setting it each time AsyncTask is called. If you make your adapter a member variable, then you should be able to call <adapter>.notifyDataSetChanged() from within the AsyncTask.

How To Set the Text of an item in ListView

I have a list of ListItems's in my listview. The ListItem has a name instance variable. I am trying to get the text of each of the items just to be the name.
How can I do this?
public class List extends ListActivity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setListAdapter(new ArrayAdapter<ListItem>(this, R.layout.list_item, Previousclass.LinkedList-from-previous-class));
Right now it is just displaying the memory location of each item i.e. ListItem#47234c10
You have to extend ArrayAdapter to implement getView(). The ArrayAdapter implementation is returning the toString() value of your ListItem object.
Or alternatively stringify your data before adding it to the adapter.
What is Previousclass.LinkedList-from-previous-class ? Is it a list of your items?
If yes,
Your list of items should be a list of names of items. Your resulting screen should have listview in it and the adapter you created should be used this way
result.setListAdapter(Youradapter)
I am just giving you sample based on what code you have posted. If you need details, please elaborate your code.
result.setListAdapter(new ArrayAdapter<ListItem>(this, R.layout.list_item, Previousclass.LinkedList-from-previous-class))

Categories

Resources