I have a simple question.
just as we are able to apply adapter to ListActivity in the following way :
getListView().setAdapter(new myadapter());
the same does not work for ListFragment.
Can anybody update why this does not work apart from quoting from the api reference.
public class mylistfrag extends ListFragment
{
#Override
public void onActivityCreated(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onActivityCreated(savedInstanceState);
ListView lv=getListView();
ArrayAdapter<String> aa=new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1,((myfragshow)getActivity()).st);
lv.setAdapter(aa);
}
}
public class myfragshow extends FragmentActivity
{
String st[]={"a","b","C","d","e"};
#Override
protected void onCreate(Bundle arg0) {
// TODO Auto-generated method stub
super.onCreate(arg0);
setContentView(R.layout.myfragshow);
}
}
thanks
problem:
getListView()
when you are trying to call that method within your fragment it will call your activity's listview reference not your fragment's listview
here is the documentation:
Get the activity's list view widget.
instead directly use the setListAdapter to set the adapter of the fragment
sample:
setListAdapter((new myadapter()));
for a ListFragment you have to use ListFragment.setListAdapter() as if you use setadapter important initialization will be skipped.
Related
I have created my own search view as below
public class MySearchView extends SearchView {
public MySearchView(Context context) {
super(context);
// TODO Auto-generated constructor stub
}
// The normal SearchView doesn't clear its search text when
// collapsed, so we will do this for it.
#Override
public void onActionViewCollapsed() {
setQuery("", false);
super.onActionViewCollapsed();
}
}
When i have to create an item of this search View i must pass the context like getActivity()
But since ActionBarActivity does not have getActivity(), what should I pass
Inside the ActionBarActivity extended class, you can access the context by calling the method: getApplicationContext().
Hope this helps!
Nick
I'm trying to set the initial scroll position in a ListFragment, with no success. I'm trying to do the following:
public abstract class AbstractDiaListFragment extends ListFragment implements LoaderManager.LoaderCallbacks<Cursor> {
(...)
#Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
ListView mList = this.getListView();
mList.setSelection(position)
}
I've already tried using smoothScrollToPosition, and tried using onViewCreated() instead of onActivityCreated. Am I missing something?
Thanks!
Try using:
mList.post(new Runnable() {
#Override
public void run() {
mList.setSelection(position);
}
});
I am using a custom adapter that extends CursorAdapter along with a CursorLoader and a Fragment,I made sure to use everything from the v4 library...however the method swapCursor in my adapter does not seem to work below HoneyComb.
How can I implement the equivalent of this code for Gingerbread:
#Override
public void onLoadFinished(Loader<Cursor> loader, Cursor cursor) {
// TODO Auto-generated method stub
adapter.swapCursor(cursor);
}
#Override
public void onLoaderReset(Loader<Cursor> loader) {
// TODO Auto-generated method stub
adapter.swapCursor(null);
}
Thank you in advance for your valuable suggestions.
I figured it out, it was a simple mistake.
I was extending android.widget.CursorAdapter instead of android.support.v4.widget.CursorAdapter.
I have created a custom adapter that inflates rows in a fragment. I would like to know how to put this in a thread.
In my fragment I have:
context = getActivity().getApplicationContext();
ListAdapter adapter = new NotesAdapter(courseId, context);
setListAdapter(adapter);
Every thing works this way but I have tried to put this in all four ways (AsyncTask, Java thread...) that Android offers for multithreading but the adapter won`t start that way. It just shows blank screen.
Can anyone help me how to put this in a separate thread?
For your reference,
public class SampleTask extends AsyncTask<Void, Void, Void> {
#Override
protected Void doInBackground(Void... params) {
// Do your Background process Eg.. some Parsing whatever it's, then paste you adapter initialization code
// Initialize your adapter as global
CustomAdapter sampleAdapter = new CustomAdapter(CurrentActivity.this,
R.id.ImageView01, <Your Arraylist/Array>);
return null;
}
#Override
protected void onPostExecute(Void result) {
super.onPostExecute(result);
// Here you set your adapter
listView.setAdapter(sampleAdapter);
}
#Override
protected void onPreExecute() {
// TODO Auto-generated method stub
super.onPreExecute();
}
}
I have 2 ListViews both containing their own data.
When the user clicks on a button inside one of the items in the first ListView some data changes occur that the second ListView should be notified off. However I can't seem to get this to work. Below the simplified relevant code:
This is the first ListFragment. Inside the AsyncTask the first custom ListAdapter is set with setListAdapter() during onPostExecute().
public class FragmentAListSupport extends SherlockListFragment {
String url = "omitted";
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
AsyncTask<String, Integer, String[]> asynctask =
new DownloadFilesTask(this).execute(url);
}
}
This is the second ListFragment. Inside the AsyncTask the second custom ListAdapter is set with setListAdapter() during onPostExecute().
public class FragmentBListSupport extends SherlockListFragment {
String url = "omitted";
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
AsyncTask<String, Integer, String[]> asynctask =
new DownloadDataTask(this).execute(url);
}
}
Inside the custom adapter used by the first ListFragment (A) I have the onButtonPressed handler.
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
//code omitted
viewHolder.button
.setOnClickListener(new Button.OnClickListener() {
#Override
public void onClick(View buttonview) {
//code omitted that changes some local data
//I need to notify the other ListFragment (B) here that it should update
}
{
}
Now for my question...
How do I correctly use notifyDataSetChanged() from inside the first adapter (A) to notify the second adapter (B) that it should update the ListView? I am confused about how I should retrieve the correct reference to the second adapter from inside the first adapter because they are both initialized inside an AsyncTask.
Any help would be nice!
Try to do some thing like this:
from one fragment create a method that returns the instance of your adapter:
public customAdapter getDataAdapter()
{
return dataAdapter;
}
and in the second fragment get an instance of your first fragment:
and then:
firstfragment.getDataAdapter().clear();
firstfragment.getDataAdapter().addAll(allDataSet);