Hello, I am hoping someone can point out anything I might be doing wrong that is causing my issue. (seems hard to find "best practice" type of advise for newer android features)
My problem is that a ListView which is inside a fragment, controlled by a ViewPager does not get displayed on the screen if I swipe to the next fragment. (but a TextView inside the same fragment shows up fine on all fragment instances),
To set the scene, I have only one fragment class out of which I instantiate all the fragments used by the ViewPager.
Inside the fragments onActivityCreated I create a cursor loader and a custom cursor adapter and on the loaders onLoadFinished I swap the cursor as its supposed to be. Naturally the customCursorAdapter that I created handles the inflation of the listView row in the newView method and binds the data from the cursor in the bindView method.
Does this sound right? I mean I did it this way with the intent of not having tight coupling, having the fragment be independent. So why won't it draw out the listview for subsequent pages? From what I have seen in the logs, all methods get called for all fragments, including getItem, newView, bindView etc.
Final quirk is that the "previous" fragmet's listView does get drawn. Meaning I start on Fragment 1 - listView is there. Move to fragment 2 - listview not there, move to fragment 3 list view not there, move back to fragment 2 list view IS there now, and back to fragment 1 it is NOT there anymore.
I don't have time to post code right now but will as soon as I get a chance.
Thank you all.
Code:
//main activity which creates the view pager and adapter and starts the process
protected void onCreate(Bundle savedInstanceState) {
Log.d(TAG,"onCreate begin");
super.onCreate(savedInstanceState);
setContentView(R.layout.pager_task_tracking);
viewPager = (ViewPager) findViewById(R.id.pager);
FragmentManager fragmentManager = getSupportFragmentManager();
MyPagerAdapter myPagerAdapter = new MyPagerAdapter(fragmentManager);
viewPager.setOffscreenPageLimit(0); // trying to force drawing of fragments
viewPager.setAdapter(myPagerAdapter);
Log.d(TAG,"onCreate viewPager done");
//MyPagerAdapter getItem (extends FregmentSatePagerAdapter
public Fragment getItem(int i) {
Log.d(TAG,"getItem called with item:"+i);
return TaskListFragment.newInstance(i);
}
//fragment onActivityCreated , this fragment instanciated in getView of viewPager
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
Log.d(TAG, "onActivityCreated with datePointer:"+datePointer);
TextView textViewFragmentTitle = (TextView) getView().findViewById(R.id.fragmentTitle);
textViewFragmentTitle.setText("DATE:"+datePointer);
loaderManager = getLoaderManager();
list = (ListView) getActivity().findViewById(R.id.TaskListView);
customAdapter = new CustomCursorAdapter(getActivity(), null, 1 , datePointer);
list.setAdapter(customAdapter);
loaderID = datePointer; //attempt to get fragment listview to draw - not working
loaderManager.initLoader(loaderID, null, this);
}
///cursor loader onFinish
public void onLoadFinished(android.support.v4.content.Loader<Cursor> loader,
Cursor newCursor) {
Log.d(TAG, "onLoadFinished:"+datePointer);
if (customAdapter != null && newCursor != null ){
Log.d(TAG, "onLoadFinished swapCursor:"+datePointer);
customAdapter.swapCursor(newCursor);
//doesnt help customAdapter.notifyDataSetChanged();
} else {
Log.v(TAG,"OnLoadFinished: mAdapter is null");
}
}
//MyCustomCursorAdapter (used by the listview inside the fragment above)
public View newView(Context context, Cursor cursor, ViewGroup parent) {
LayoutInflater inflater = LayoutInflater.from(context);
View retView = inflater.inflate(R.layout.task_list_item_layout, null);
RelativeLayout.LayoutParams relativeLayoutParameters;
Log.d(TAG,"newView layout inflated:"+datePointer);
return retView;
}
///bindView of same MyCustomCursorAdapter
public void bindView(View view, Context context, Cursor cursor) {
Log.d(TAG,"bindView:"+datePointer);
TextView tvName = (TextView) view.findViewById(R.id.textviewItemName);
tvName.setText(cursor.getString(cursor.getColumnIndex(DBHelper.C_TASK_NAME))+" ["+datePointer+"]");
}
Also check out the LogCat logs with description of the action in the name corresponding to the log statements in the code above.
I noticed more detail about what happens (if you look at the logcat you will see also)
When the screen is displayed the first fragment has the listview present, but the fragment has the id/datePointer (same thing) of "0" BUT the listview items have an id/datePointer of "1"
If you look at the initial logcat you will see "newView layout inflated:1" but no "newView layout inflated:0" how this is being skipped I have no idea. And how the id's can be mix-matched between fragment instances baffles me.
And lastly, after scrolling right twice (to the 0,1, 2nd fragment) it did not have the listview as reported, but scrolling BACK ONE the listview shows up in the 1st fragment ... but even more baffling with lisvtView items that report being part of the "0" fragment .... whaaaat?
Why are you using a listview inside fragment when you have listfragment available?
EatHeat said:
Why are you using a listview inside fragment when you have listfragment available?
Click to expand...
Click to collapse
EatHeat, I am doing this for the sake of flexibility for adding additional controls/views to the fragment besides just a list view (Example I envision adding several buttons to the bottom of the fragment/screen which will be "outside" the listview.
Does it make more sense what I am doing now? That is exactly one of the questions I have, its so hard to find advise on what makes more sense when it comes to these complex combinations of viewPager+fragments+custom listViews+loaders
So my current flow is:
1--Activity (creates ViewPager with myPagerAdapter)
2----ViewPager in itst getItem method instantiates a fragment from class TaskListFragment
3-------The fragment in it's onActivityCreated instantiates a CustomCursorAdapter (with null cursor) and initializes a loader
4-------The loader swaps the cursor in it's onLoadFinished (and this is supposed to populate the listview)
Items 2-4 repeat for every "swipe" or every fragment instantiated.
-The listView control XML is defined in an XML file which is used by the fragment's setContentView
-The lisView row layout XML is defined in a separate XML file inflated by the customCursorAdapter's newView method.
SOLVED: (after literally one week of headache and rewriting the code in every way I know)
the fragments onCreateView I had it getting the listview with getActivity() .. but I should have done it with getView()
the solution was replacing this line
Code:
list = (ListView) getActivity().findViewById(R.id.TaskListView);
with this line
Code:
list = (ListView) getView().findViewById(R.id.TaskListView);
That was it!!! all the headache!!
I don't particularly understand what getView does as opposed to getActivity .. but right now I am pleased it works.
Hi Can u please provide me the code .. I an working on same thing .. bt my getView Function is not called ..
Email : [email protected]
Make sure you custom pager adapter returns all pages as per count.
Use recyclerview in fragments
Sent from my Lenovo A6000 using Tapatalk
The offscreen page limit is by default 1. It is useless to set the limit lesser equals as 1.
Code:
viewPager.setOffscreenPageLimit(0);
Related
Hello,
I have a ListView , in this ListView i have a download button for each item, i have an hidden textview with a ID that i need when i click the button (the textview is filled when the listview is populating), and also i have a hidden progress bar for each item, that it appears when the downloading starts , when i click Download, i instance an asyctask, in this async task i send the progressbar object to be able to update their progress value, and the id i need
the problem is when i scroll my list , and im downloading, the progressbar appears in other item , is there any way to avoid that?
my SimpleAdapter
Code:
public View getView(int position, View convertView, ViewGroup parent) {
View row=super.getView(position, convertView, parent);
//Hidden textview
TextView videoIdText = (TextView) row.findViewById(R.id.videoId);
Button downloadButton = (Button) row.findViewById(R.id.videoDownload);
final ProgressBar downloadProgressBar = (ProgressBar) row.findViewById(R.id.downloadProgressBar);
final String videoId = videoIdText.getText().toString();
downloadButton.setOnClickListener(new OnClickListener(){
[user=439709]@override[/user]
public void onClick(View view) {
DownloadTask task = new DownloadTask();
task.setProgressBar(downloadProgressBar);
task.setVideoId(videoId);
task.execute();
}
});
return row;
}
Please post your full code, layout and manifest so I can try and debug it. Thanks.
frenzyboi said:
Please post your full code, layout and manifest so I can try and debug it. Thanks.
Click to expand...
Click to collapse
Hey there are the files, the feed_item, is the one i used to each item of the ListItem
I want to create a listview. On the last row of the listview, I want to show a tabhost with two tabs and on each tab I would like to load another listview. For this, I've created three XML files.
The main.xml is where I'm loading the initial list. The mainlistdata.xml is how each row of the list is presented. And the last xml is tabslayout.xml. This layout is used for the last row on the main listview. It will load the tabhost. Here is the code with all the xml
My main class is this:
Code:
public class MainActivity extends Activity {
ArrayList<testData> myData = new ArrayList<testData>();
ListView listContent;
TestAdapter dataAdapter;
[user=439709]@override[/user]
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
String[] aString = { "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z" };
for (int i = 0; i < aString.length; i++) {
myData.add(new testData(i, aString[i]));
}
listContent = (ListView) findViewById(R.id.mainlist);
dataAdapter = new TestAdapter(this, myData,1);
listContent.setAdapter(dataAdapter);
}
}
And this is the class that loads the content on the listviews and where I set the tabhost:
Code:
public class TestAdapter extends ArrayAdapter<testData> {
private Context context;
private ArrayList<testData> data;
private ListView listContent,listContent2;
public TabHost tabs;
TestAdapter dataAdapter;
private int choice;
public TestAdapter(Context context, ArrayList<testData> data, int choice) {
super(context, R.layout.mainlistdata, data);
this.context = context;
this.data = data;
this.choice = choice;
}
[user=439709]@override[/user]
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View rowView;
if (choice == 1) {
if (position == data.size()-1) {
rowView = inflater.inflate(R.layout.tabslayout, parent, false);
TextView first = (TextView) rowView.findViewById(R.id.dataTitle);
TextView second = (TextView) rowView.findViewById(R.id.dataValue);
TextView third = (TextView) rowView.findViewById(R.id.dataValueString);
first.setText("Item " + data.get(position).getIntValue());
second.setText(String.valueOf(data.get(position).getIntValue()));
third.setText(data.get(position).getStringValue());
tabs = (TabHost) rowView.findViewById(android.R.id.tabhost);
tabs.setup();
TabSpec tab1 = tabs.newTabSpec("Tab 1");
TabSpec tab2 = tabs.newTabSpec("Tab 2");
tab1.setContent(R.id.tabOne);
tab2.setContent(R.id.tabTwo);
tab1.setIndicator("One");
tab2.setIndicator("two");
tabs.addTab(tab1);
tabs.addTab(tab2);
tabs.setup();
listContent = (ListView) rowView.findViewById(R.id.listOne);
dataAdapter = new TestAdapter(context, data,2);
listContent.setAdapter(dataAdapter);
listContent2 = (ListView) rowView.findViewById(R.id.listTwo);
dataAdapter = new TestAdapter(context, data,3);
listContent2.setAdapter(dataAdapter);
} else {
rowView = inflater.inflate(R.layout.mainlistdata, parent, false);
TextView first = (TextView) rowView.findViewById(R.id.dataTitle);
TextView second = (TextView) rowView.findViewById(R.id.dataValue);
TextView third = (TextView) rowView.findViewById(R.id.dataValueString);
first.setText("Item " + data.get(position).getIntValue());
second.setText(String.valueOf(data.get(position).getIntValue()));
third.setText(data.get(position).getStringValue());
}
} else if (choice == 2) {
rowView = inflater.inflate(R.layout.mainlistdata, parent, false);
TextView first = (TextView) rowView.findViewById(R.id.dataTitle);
TextView second = (TextView) rowView.findViewById(R.id.dataValue);
TextView third = (TextView) rowView.findViewById(R.id.dataValueString);
first.setText("Item tab1 " + data.get(position).getIntValue());
second.setText(String.valueOf(data.get(position).getIntValue()));
third.setText(data.get(position).getStringValue());
} else {
rowView = inflater.inflate(R.layout.mainlistdata, parent, false);
TextView first = (TextView) rowView.findViewById(R.id.dataTitle);
TextView second = (TextView) rowView.findViewById(R.id.dataValue);
TextView third = (TextView) rowView.findViewById(R.id.dataValueString);
first.setText("Item tab2 " + data.get(position).getIntValue());
second.setText(String.valueOf(data.get(position).getIntValue()));
third.setText(data.get(position).getStringValue());
}
return rowView;
}
}
As you can see on the next image, I'm loading the data and then I'm loading the tabs. The problem is that each tab is only loading the first row of the data. Any idea How can I solve this?
favolas said:
I want to create a listview. On the last row of the listview, I want to show a tabhost with two tabs and on each tab I would like to load another listview. For this, I've created three XML files.
As you can see on the next image, I'm loading the data and then I'm loading the tabs. The problem is that each tab is only loading the first row of the data. Any idea How can I solve this?
Click to expand...
Click to collapse
I don't quite understand what you are trying to do but why do you want the tabs in the last item of the list? Could you just put the tabs in the main activity and add a scrollview to bring the tabs up or set the weight of the layout so the tabs take up the bottom of the screen and the top list would scroll if to long?
Your problem may be fixed if you set the tabs and the lists inside to be a certain size (200dp or something). I know sometimes having it set to wrap_content will only show one list item
Acela1230 said:
I don't quite understand what you are trying to do but why do you want the tabs in the last item of the list? Could you just put the tabs in the main activity and add a scrollview to bring the tabs up or set the weight of the layout so the tabs take up the bottom of the screen and the top list would scroll if to long?
Your problem may be fixed if you set the tabs and the lists inside to be a certain size (200dp or something). I know sometimes having it set to wrap_content will only show one list item
Click to expand...
Click to collapse
Hello. Thanks for your answer.
My initial idea was to create a scrollview. In this scrollview, create the main listview, load the tabhost and them, in each tab, load another listview. After seeing this question, I saw that we shouldn't place a listview inside one scrollview. That's why I came up with this solution.
I've tried to do your solution but changing the height did not solve the problem.
After some debugging I belive that this is not working because the first list loads the last row (the one with the tabs) and then returns, not giving time to load the other lists. Can this be the problem?
Regards,
favolas
favolas said:
Hello. Thanks for your answer.
My initial idea was to create a scrollview. In this scrollview, create the main listview, load the tabhost and them, in each tab, load another listview. After seeing this question, I saw that we shouldn't place a listview inside one scrollview. That's why I came up with this solution.
I've tried to do your solution but changing the height did not solve the problem.
After some debugging I belive that this is not working because the first list loads the last row (the one with the tabs) and then returns, not giving time to load the other lists. Can this be the problem?
Regards,
favolas
Click to expand...
Click to collapse
But why? Why not just ActionBar Tabs with ListViews in them? At any rate, you can only have one View in a ScrollView, which is why people always put i.e. a RelativeLayout in a ScrollView, with the other Views in there.
Hi bassie1995.
Sorry for the late feedback.
bassie1995 said:
But why? Why not just ActionBar Tabs with ListViews in them?
Click to expand...
Click to collapse
This is just because my data is this way. Will then try to follow your suggestion and do actionbar tabs and put listview in them.
Regards,
Favolas
I am trying to develop a web crawler to using Android searchable following this guide:
Creating a Search Interface | Android Developers
So far, the search results display a new browser screen to display the google results. The guide says that I can get the results in a ListActivity using a ListAdapter. All examples I have seen describe how to create this Adapter for q query from database or contacts on phone, such as:
Code:
public class MyListAdapter extends ListActivity {
@Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
// We'll define a custom screen layout here (the one shown above), but
// typically, you could just use the standard ListActivity layout.
setContentView(R.layout.custom_list_activity_view);
// Query for all people contacts using the Contacts.People convenience class.
// Put a managed wrapper around the retrieved cursor so we don't have to worry about
// requerying or closing it as the activity changes state.
mCursor = this.getContentResolver().query(People.CONTENT_URI, null, null, null, null);
startManagingCursor(mCursor);
// Now create a new list adapter bound to the cursor.
// SimpleListAdapter is designed for binding to a Cursor.
ListAdapter adapter = new SimpleCursorAdapter(
this, // Context.
android.R.layout.two_line_list_item, // Specify the row template to use (here, two columns bound to the two retrieved cursor
rows).
mCursor, // Pass in the cursor to bind to.
new String[] {People.NAME, People.COMPANY}, // Array of cursor columns to bind to.
new int[] {android.R.id.text1, android.R.id.text2}); // Parallel array of which template objects to bind to those columns.
// Bind to our new adapter.
setListAdapter(adapter);
}
}
I couldn't find anything to declare a cursor to read from the global search results. I appreciate any help I can get to retrieve the global search results programatically to be able to process it and produce a report about the results, not just the mere listings.
PLEASE HELP ME GUYS!!! I HAVE NO CLUE WHAT I SUPPOSE TO DO THIS ASSIGNMENT :
+ Create an app with multiple activities and a ListView with data consisting of instances of a custom model class. Provide an addtional activity allow editing of the data.
+ A ListView with 8 Course Model Class
+ A seperate Activity is display on click of ClickView, which show 3 value (Title, Catalog, Capacity) and editable
-----------------------
So first I create a listview with 8 items on main activity layout, and then this my my MainActivity.class :
ListView listView = (ListView) findViewById(R.id.listView);
String[] items = { "English 156", "Match 232", "Chemistry 110FL", "Physical 051", "Piano 09","Guitar 100","Android Development 207","History 207A" };
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, items);
listView.setAdapter(adapter);
listView.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> arg0, View v, int position, long id) {
Intent myIntent = new Intent(getApplicationContext(), FragmentClass.class);
startActivity(myIntent);
}
});
---------
I have problem that when running app, 2 activity appears at same time...
Please help me with remain codes and what can I do to fix that problems...
my app being shown like this, I want course title, catalog and capacity are shown itemonclick only.
Really???? Noone is wiliing to help me in this case???? SO SAD!!!!
ptt1404 said:
Really???? Noone is wiliing to help me in this case???? SO SAD!!!!
Click to expand...
Click to collapse
Just looks like your throwing a fragment onto the top of a listView ? Not sure what you want, it is not clear
Do you have a second fragment with transparent background?
Hi everyone,
First of all, I'd like to apologize if I'm posting this in a wrong place... This is my first post, I'm still getting familiar with this forum.
I would also like to apologize for the lengthy post.
The title, to a certain extent, reflects what my problem is, but to clarify:
I'm learning Android, so I'm making a simple SMS app for practice.
In it, I have a database which has tables for sent messages, received messages and contacts. I have 3 separate activities (which do NOT extend ListActivity) which show lists for sent messages, received messages and contacts, respectively. The lists are populated through CursorAdapter.
Let's consider the activity for contacts...
In it I have list (ListView) which displays contacts (each list element displays name and phone number). Below the list I have a "Add Contact" button. When I click the button a dialog pops up and shows the form for adding new contact. The buttons in the dialog preform all the database operations.
Similarly, when I click some item in the ListView, another dialog pops up. That dialog has buttons for "Send SMS", "Edit" and "Delete" contact. Again, the buttons do all the work with the database.
The trouble:
My trouble is... When I add a new contact, or delete one (after both operations their dialogs dismiss), the ListView is not refreshed.
In order to see the refreshed list I need to close the activity and start it again.
I googled and googled this for 3 days now, and all the answers I found say that I need to call
Code:
adapter.notifyDataSetChanged
and
Code:
adapter.changeCursor
but that doesn't do the trick.
I'll now post the relevant code with the two methods mentioned above. I placed them where i thought they should be, but this doesn't work.
So, I humbly beg someone to guide me through this ordeal.
Many thanks in advanced!
Here comes the code:
The Adapter:
Code:
public class AdapterContactListView extends CursorAdapter {
private MyDatabaseHelper mdbh;
private LayoutInflater myLayoutInflater;
public AdapterContactListView(Context context, Cursor c, int flags) {
super(context, c, flags);
mdbh = MyDatabaseHelper.getMyDatabaseHelper(context);
myLayoutInflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public void bindView(View view, Context context, Cursor cursor) {
// TODO Auto-generated method stub
TextView fullNameTV = (TextView)view.findViewById(R.id.contactElementNameTV);
TextView phoneNumberTV = (TextView)view.findViewById(R.id.contactElementNumberTV);
String fullName = cursor.getString(cursor.getColumnIndex(mdbh.getContactFirstName()));
fullName = fullName.concat(" ");
fullName = fullName.concat(cursor.getString(cursor.getColumnIndex(mdbh.getContactLastName())));
String phoneNumber = cursor.getString(cursor.getColumnIndex(mdbh.getContactPhoneNumber()));
fullNameTV.setText(fullName);
phoneNumberTV.setText(phoneNumber);
}
@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
// TODO Auto-generated method stub
return myLayoutInflater.inflate(R.layout.contact_element, parent, false);
}
}
And the Activity:
Code:
public class ContactsActivity extends Activity {
private MyUtilities myUtilities;
private MyDatabaseHelper mdbh;
private AdapterContactListView contactsAdapter;
private ListView contactsListView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_contacts);
mdbh = MyDatabaseHelper.getMyDatabaseHelper(this);
myUtilities = new MyUtilities(this);
contactsAdapter = new AdapterContactListView(this,mdbh.getContactsCursor(),CursorAdapter.FLAG_REGISTER_CONTENT_OBSERVER);
contactsListView = (ListView)findViewById(R.id.contactActivityLV);
contactsListView.setAdapter(contactsAdapter);
contactsListView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
// TODO Auto-generated method stub
TextView phoneNumberTV = (TextView)view.findViewById(R.id.contactElementNumberTV);
String phoneNumber = phoneNumberTV.getText().toString();
Contact contact = mdbh.getContactFromPhoneNumber(phoneNumber);
Dialog d = myUtilities.createSelectedContactOptionsDialog(contact);
d.show();
contactsAdapter.changeCursor(mdbh.getContactsCursor());
contactsAdapter.notifyDataSetChanged();
}
});
}
public void addContact(View view) {
Dialog d = myUtilities.createAddContactDialog();
d.show();
contactsAdapter.changeCursor(mdbh.getContactsCursor());
contactsAdapter.notifyDataSetChanged();
}
}
djolec987 said:
Hi everyone,
Click to expand...
Click to collapse
Well notifyDatasetChanged only informs the adapter that the backing interface has new data... but your backing it with a cursor... so in your case it would just cause getView/bindView to be called for all current visible items, thus fire a query at the cursor. I think the cursor will cache the data so it's really a reload on the cursor data and then an adapter notify call you want... If you use loader (depending on target api version) then it should do most of this for you. As it stands if you want to do it manually make sure the cursor is a new cursor of the database that has changed.
(typed in a rush)