I got my MainActivity(FragmentActivity) with ViewPager with FragmentPagerAdapter which creates OrdersFragment in which i have ListView. Now i want to start new Activity OnItemClickListener but i can't figure out how to do this.
This is OrdersFragment class:
public class OrdersFragment extends Fragment {
ListView lv;
private ArrayList<Order> d = new ArrayList<Order>();
private ordersAdapter adapter;
public OrdersFragment (){
}
public OrdersFragment setArguments(ArrayList<Order> orders){
if(adapter == null) return this;
adapter.setData(orders);
Log.d("fragment", "zmiana");
adapter.notifyDataSetChanged();
return this;
}
#Override
public View onCreateView(LayoutInflater inflater,
ViewGroup container, Bundle savedInstanceState) {
//d.add(new Order());
Log.d("fragment", "orders fragment created" + d.toString());
lv = (ListView) inflater.inflate(R.layout.orders_list, container, false);
lv.setOnItemClickListener(itemClicked);
adapter = new ordersAdapter(inflater, d);
lv.setAdapter(adapter);
return lv;
}
public ArrayList<Order> getOrders(){
return d;
}
OnItemClickListener itemClicked = new OnItemClickListener(){
#Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
Log.d("fragment", "clicked!!" + parent.getItemAtPosition(position).toString());
startActivity(new Intent(OrdersFragment.this, OrderActivity.class));
}
};
}
It says that Intent constructor is undefined and also when i move over startActivity it says Note: This element neither has attached source nor attached Javadoc and hence no Javadoc could be found. And it states that this fuction is defined in android.support.v4.app.Fragment.startActivity but I'm acually import this:
import android.app.Activity;
import android.content.Intent;
What I'm doing wrong?
Try this pass context as getActivity() to intent
OnItemClickListener itemClicked = new OnItemClickListener(){
#Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
Log.d("fragment", "clicked!!" + parent.getItemAtPosition(position).toString());
startActivity(new Intent(getActivity(), OrderActivity.class));
}
};
In Fragment we have to pass context as getActivity(), replace your code with this below code it will work.
OnItemClickListener itemClicked = new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Log.d("fragment", "clicked!!" + parent.getItemAtPosition(position).toString());
startActivity(new Intent(getActivity(), OrderActivity.class));
}
};
OnItemClickListener itemClicked = new OnItemClickListener(){
#Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
if(getActivity() != null) {
Log.d("fragment", "clicked!!" + parent.getItemAtPosition(position).toString());
startActivity(new Intent(getActivity(), OrderActivity.class));
}
}
};
getActivity() will give you the Activity context that you can use to start the new Activity. In some rare cases getActivity() can return null so checking getActivity() is not null might not harm in your case.
Related
I am trying to make a popup menu appear when I click one of the items on the ListView, but I cant get the position of the item.
listax.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) {
ItemList item= adapter.getItem(position);
Toast.makeText(getContext(), item.getTxtNomeItem().toString(), Toast.LENGTH_SHORT).show();
//showPopup(adapterView);
}
});
That code gives me "Attempt to invoke virtual method 'java.lang.Object com.example.securityaplication.ItemArrayAdapter.getItem(int)' on a null object reference" error.
I have made the same thing in another project and it worked fine:
listax.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
modelo item = adapter.getItem(position);
Toast.makeText(getApplicationContext(),
item.getNome().toString (), Toast.LENGTH_SHORT).show();
}
});
The only difference is that I used "getContext" instead of "getApplicationContext()" because it didnt accept "getApplicationContext()" and that the first one is in a fragment and the latter one is in a activity.
My Fragment where it gives me the error:
public class HomeFragment extends Fragment implements PopupMenu.OnMenuItemClickListener {
ItemArrayAdapter adapter;
private ListView listax;
public HomeFragment() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_home, container, false);
listax = (ListView)view.findViewById(R.id.listViewx);
ArrayList<ItemList> ItemList = new ArrayList<>();
ItemArrayAdapter itemArrayAdapter = new ItemArrayAdapter(getActivity(), R.layout.adapter_view_layout,ItemList);
listax.setAdapter(itemArrayAdapter);
listax.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) {
ItemList item= adapter.getItem(position);
Toast.makeText(getContext(), item.getTxtNomeItem().toString(), Toast.LENGTH_SHORT).show();
//showPopup(adapterView);
}
});
return view;
}
public void showPopup(View v){
PopupMenu popup = new PopupMenu(getActivity(), v);
popup.setOnMenuItemClickListener(this);
popup.inflate(R.menu.menupopup);
popup.show();
}
#Override
public boolean onMenuItemClick(MenuItem menuItem) {
switch(menuItem.getItemId()){
case R.id.itemAlterar:
Toast.makeText(getActivity(), "Item 1 clicked", Toast.LENGTH_LONG).show();
return true;
case R.id.itemExcluir:
Toast.makeText(getActivity(), "Item 2 clicked", Toast.LENGTH_LONG).show();
return true;
case R.id.itemEmprestar:
Toast.makeText(getActivity(), "Item 3 clicked", Toast.LENGTH_LONG).show();
return true;
case R.id.itemRecuperar:
Toast.makeText(getActivity(), "Item 3 clicked", Toast.LENGTH_LONG).show();
return true;
default:
return false;
}}
}
Don't forget to use the class member variable "adapter" when you setup the ListAdapter.
//ItemArrayAdapter itemArrayAdapter = new ItemArrayAdapter(getActivity(), R.layout.adapter_view_layout,ItemList);
//listax.setAdapter(itemArrayAdapter);
adapter = new ItemArrayAdapter(getActivity(), R.layout.adapter_view_layout,ItemList);
listax.setAdapter(adapter);
I am using onListItemClick with simple_list_item_1
public class DrinkCategoryActivity extends ListActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ListView listDrinks = getListView();
ArrayAdapter<Drinks> listAdapter = new ArrayAdapter<Drinks>(this,
android.R.layout.simple_list_item_1,
Drinks.drinks);
listDrinks.setAdapter(listAdapter);
}
#Override
public void onListItemClick(ListView listView, View itemview, int position, int id){
Intent intent = new Intent(this, DrinkActivity.class);
intent.putExtra(DrinkActivity.EXTRA_DRINKNO, id);
startActivity(intent);
}
}
It says that onListItemClick is never used
When I open my app it doesn't respond to click.
I don't want to use a new layout activity.
I am using list activity which doesn't always require layout
You should use setOnItemClickListener
setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Object listItem = list.getItemAtPosition(position);
}
});
you can try below
public class DrinkCategoryActivity extends Activity implements AdapterView.OnItemClickListener
{
#Override
public void onCreate(Bundle savedInstanceState)
{
setContentView(R.layout.activity_drink_category);
ListView listDrinks = (ListView)findViewById(R.id.your_list_view_id);
ArrayAdapter<Drinks> listAdapter = new ArrayAdapter<Drinks>(this,
android.R.layout.simple_list_item_1,
Drinks.drinks);
listDrinks.setAdapter(listAdapter);
listDrinks.setOnItemClickListener(this);
}
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id)
{
//Callback logic here for clicked items
}
...
}
Try this
listDrinks.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
}
});
Just add
public class DrinkCategoryActivity extends ListActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ListView listDrinks = getListView();
ArrayAdapter<Drinks> listAdapter = new ArrayAdapter<Drinks>(this,
android.R.layout.simple_list_item_1,
Drinks.drinks);
listDrinks.setAdapter(listAdapter);
// Add this block
listDrinks.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Intent intent = new Intent(this, DrinkActivity.class);
intent.putExtra(DrinkActivity.EXTRA_DRINKNO, id);
startActivity(intent);
}
});
}
Remove
#Override
public void onListItemClick(ListView listView, View itemview, int position, int id){
Intent intent = new Intent(this, DrinkActivity.class);
intent.putExtra(DrinkActivity.EXTRA_DRINKNO, id);
startActivity(intent);
}
Friends, I've spent enough time to find an answer to my question(here and on android's site) But no luck.
Here is the logic:
From DialogFragment upon finishing putting user's data he clicks OK button:
import de.greenrobot.event.EventBus;
...
public class AddDialogFragment extends DialogFragment implements
DialogInterface.OnClickListener {
...
public void onClick(DialogInterface dialog, int which) {
StringBuilder date = new StringBuilder(today.getText().subSequence(0,
today.getText().length()));
date = (date.toString().equals(getString(R.string.today))) ? new StringBuilder("20150104") : date;
int weight = 89;
String[] bsi = rb.getSelectedItems();
String[] gsi = rg.getSelectedItems();
DatabaseManipulation dm = new DatabaseManipulation(getActivity());
dm.run(date, weight, bsi, gsi);
dm.destroy();
DialogDataModel toSend = new DialogDataModel(weight, date.toString(), bsi, gsi);
/*
Context activity = getActivity();
if (activity instanceof WDActivity)
((WDActivity) activity).updateListItemFragment();
*/
EventBus.getDefault().post(new UpdateItemEvent(toSend));
Log.d(LOG_TAG, "send event");
Toast.makeText(getActivity(), R.string.saved_data, Toast.LENGTH_LONG).show();
}
...
Event is successfully sent to Adapter class which is registered for receiving this event:
...
import de.greenrobot.event.EventBus;
public class ItemsAdapter extends BaseAdapter implements ListAdapter {
private static final String LOG_TAG = "Logs";
private ArrayList<DialogDataModel> list = new ArrayList<DialogDataModel>();
private Context activity;
public ItemsAdapter (ArrayList<DialogDataModel> list, Context context) {
this.list = list;
this.activity = context;
registerEventBus();
}
#Override
public int getCount() {
return list.size();
}
#Override
public Object getItem(int pos) {
return list.get(pos);
}
#Override
public long getItemId(int pos) {
return pos;
//just return 0 if your list items do not have an Id variable.
}
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
View view = convertView;
if (view == null) {
LayoutInflater inflater = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inflater.inflate(R.layout.row_item, null);
}
DialogDataModel curData = list.get(position);
TextView itemDate = (TextView)view.findViewById(R.id.item_date);
itemDate.setText(curData.getDate());
TextView itemWeight = (TextView)view.findViewById(R.id.item_weight);
itemWeight.setText( Integer.toString(curData.getWeight()) );
TextView itemEvents = (TextView)view.findViewById(R.id.item_events);
itemEvents.setText(curData.getEventsShort());
//Handle buttons and add onClickListeners
ImageButton editBtn = (ImageButton)view.findViewById(R.id.item_edit_btn);
editBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (activity instanceof WDActivity)
((WDActivity) activity).AddNewData(v, list.get(position));
notifyDataSetChanged();
}
});
ImageButton deleteBtn = (ImageButton) view.findViewById(R.id.item_delete_btn);
deleteBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String date = list.get(position).getDate();
DatabaseManipulation dm = new DatabaseManipulation(activity);
dm.deleteItem(date);
dm.destroy();
list.remove(position);
notifyDataSetChanged();
}
});
return view;
}
public void registerEventBus(){
if (!EventBus.getDefault().isRegistered(this)) EventBus.getDefault().register(this);
Log.d(LOG_TAG, "Registred from adapter");
}
public void unregisterEventBus(){
EventBus.getDefault().unregister(this);
}
public void onEventMainThread(UpdateItemEvent event) {
Log.d(LOG_TAG, "Event fired!");
list = DialogDataModel.replaceFieldsInArray(list,event.getModel());
notifyDataSetChanged();
}
}
After clicking OK in dialog window the user sees the list of items and the item he's updated is up to date. Without this logic he sees the old version of this item item in the list and to refresh the list he needs to relaunch the app.
Yes, it's working with EventBus.
But as you can see here in the class there is no place for unregistering it from listening for UpdateItemEvent. Since there are no destructors in java(I don't believe that garbage collector should do this for me) I should handle this. Moreover the class registeres as listener in constructor (it happens several times, that's why you see this ugly if statement(if (!EventBus.getDefault().isRegistered(this))) You may ask me why not getting this adapter through Activity (there is the only one in the app) I tried building a chain AddDialogFragment -> WDActivity -> PlaceholderFragment -> ItemsAdapter
//AddDialogFragment
Context activity = getActivity();
if (activity instanceof WDActivity)
((WDActivity) activity).updateListItemFragment();
//WDActivity --beginTransaction().replace does not work here
...
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_wd);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
// Create the adapter that will return a fragment for each of the three
// primary sections of the activity.
mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());
// Set up the ViewPager with the sections adapter.
mViewPager = (ViewPager) findViewById(R.id.container);
mViewPager.setAdapter(mSectionsPagerAdapter);
}
...
public void updateListItemFragment() {
android.support.v4.app.FragmentManager fragmentManager = getSupportFragmentManager();
android.support.v4.app.Fragment currentFragment = fragmentManager.findFragmentById(R.id.container);
if (currentFragment instanceof PlaceholderFragment && currentFragment != null) {
fragmentManager.beginTransaction().remove(currentFragment).commit();
fragmentManager.beginTransaction().add(R.id.container, currentFragment).commit();
((PlaceholderFragment)currentFragment).fillList();
}
//PlaceholderFragment
public class PlaceholderFragment extends Fragment {
private static final String LOG_TAG = "Logs";
private static final String ARG_SECTION_NUMBER = "section_number";
private ListView listViewItem;
public static PlaceholderFragment newInstance(int sectionNumber) {
PlaceholderFragment fragment = new PlaceholderFragment();
Bundle args = new Bundle();
args.putInt(ARG_SECTION_NUMBER, sectionNumber);
fragment.setArguments(args);
return fragment;
}
public PlaceholderFragment() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
if(getArguments().getInt(ARG_SECTION_NUMBER)==1)
return onCreateViewInputChart(inflater, container, savedInstanceState);
if(getArguments().getInt(ARG_SECTION_NUMBER)==2)
return onCreateViewManual(inflater, container, savedInstanceState);
return onCreateViewInputData(inflater, container, savedInstanceState);
}
private View onCreateViewManual(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_howto, container, false);
return rootView;
}
private View onCreateViewInputData(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_list_data, container, false);
findViewsByIdListItem(rootView);
fillList();
return rootView;
}
private View onCreateViewInputChart(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_chart, container, false);
return rootView;
}
//listViewItems
private void findViewsByIdListItem(View v) {
listViewItem = (ListView) v.findViewById(R.id.listViewItems);
}
public void fillList(){
Context activity = getActivity();
DatabaseManipulation dm = new DatabaseManipulation(activity);
ArrayList<DialogDataModel> aldm = dm.getItemsList();
dm.destroy();
ItemsAdapter innerAdapter = new ItemsAdapter(aldm, activity);
listViewItem.setAdapter(innerAdapter);
( (BaseAdapter) listViewItem.getAdapter() ).notifyDataSetChanged();
}
#Override
public void onAttach(Context activity) {
super.onAttach(activity);
}
#Override
public void onDetach(){
super.onDetach();
}
}
//SectionsPagerAdapter - just for your information
public class SectionsPagerAdapter extends FragmentPagerAdapter {
public SectionsPagerAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int position) {
// getItem is called to instantiate the fragment for the given page.
return PlaceholderFragment.newInstance(position);
}
#Override
public int getCount() {
return 3;
}
#Override
public CharSequence getPageTitle(int position) {
switch (position) {
case 0:
return "Enter your data";
case 1:
return "Chart";
case 2:
return "How to use it";
}
return null;
}
}
//ItemsAdapter is placed higher
This did not work. Finally I found out that the bottleneck is in linking PlaceholderFragment and ListAdapter: listViewItem is null everywhere except onCreateViewInputData. I could not figure out why. Google didn't give me the answer and I took it as a magic. Btw due to this feature I cannot use PlaceholderFragment's onAttach and onDetach for saying ItemsAdapter to register/unregister for receiving the event. This:
#Override
public void onAttach(Context activity) {
super.onAttach(activity);
( (ItemsAdapter) listViewItem.getAdapter() ).registerEventBus();
}
doesn't work for the same reason listViewItem is null. Maybe there is some way to manage with this more accurately?
Finally I cope with this: the bottleneck is in linking PlaceholderFragment and ListAdapter: listViewItem is null everywhere except onCreateViewInputData . Below is rewised SectionsPagerAdapter:
public class SectionsPagerAdapter extends FragmentPagerAdapter {
Context activity;
SparseArray<Fragment> registeredFragments = new SparseArray<Fragment>();
public SectionsPagerAdapter(FragmentManager fm, Context activity) {
super(fm);
this.activity = activity;
}
#Override
public Fragment getItem(int position) {
// getItem is called to instantiate the fragment for the given page.
return PlaceholderFragment.newInstance(position);
}
#Override
public int getCount() {
return 3;
}
#Override
public CharSequence getPageTitle(int position) {
switch (position) {
case 0:
return activity.getString(R.string.chart);
case 1:
return activity.getString(R.string.weight_diary);
case 2:
return activity.getString(R.string.how_to);
}
return null;
}
#Override
public Object instantiateItem(ViewGroup container, int position) {
Fragment fragment = (Fragment) super.instantiateItem(container, position);
registeredFragments.put(position, fragment);
return fragment;
}
#Override
public void destroyItem(ViewGroup container, int position, Object object) {
registeredFragments.remove(position);
super.destroyItem(container, position, object);
}
public Fragment getRegisteredFragment(int position) {
return registeredFragments.get(position);
//maybe use adapater.getRegisteredFragment(viewPager.getCurrentItem());
}
}
and here is my call to the widget in the one of 3 fragments:
public void updateListItemFragment() {
android.support.v4.app.Fragment inputData = mSectionsPagerAdapter.getRegisteredFragment(1);
ListView lv = ((PlaceholderFragment)inputData).listViewItem;
//for test purposes I refill listview with no items - here you need to hand filled BaseAdapter instance: CustomAdapter innerAdapter = new CustomAdapter(ArrayList<yourDataModel>, getContext());
lv.setAdapter(null);
}
And as you've may already wondered there is no need for event bus anymore. Btw if the fragment is not crated yet you need to check it: details are here. Thanks to CommonsWare for quik reply and advice.
I have a ListFragment OnItemClick is not working; it's basically not getting called. I have been unable to find any errors in my code. Is this somehow a problem with getListView?
I've explored Google and StackOverflow but did not find a solution. Here's my code:
public class FragmentSongs extends ListFragment implements OnFragmentCreatedListener,OnItemClickListener {
static ListView lv;
private static final String TAG = FragmentSongs.class.getSimpleName();
MediaMetadataRetriever metaRetriver1;
Vector<ForPLaylist> gtuMcaBean= new Vector<ForPLaylist>();
Forplaylistadapter adaptor;
String artist="";
static int i=0,f=0,k=0;;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
trial t=new trial();
t.onCreate();
ViewGroup root = (ViewGroup) inflater.inflate(R.layout.playlist, container,false);
metaRetriver1 = new MediaMetadataRetriever();
lv=(ListView) root.findViewById(android.R.id.list);
//data calculation here
lv.setAdapter(adaptor);
// lv = getListView();//showing error here if i call it
lv.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view,int position, long id) {
int songIndex=position;
Intent in = new Intent(getActivity(), AndroidBuildingMusicPlayerActivity.class);
in.putExtra("songIndex",songIndex);
startActivityForResult(in, 100);
closefragment();
}
});
return root;
}
OnFragmentCreatedListener listener;
private void closefragment() {
getActivity().getFragmentManager().popBackStack();
}
#Override
public void onFragmentCreated(ArrayList<String> msg) {
}
#Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
int songIndex=position;
Intent in = new Intent(null, AndroidBuildingMusicPlayerActivity.class);
in.putExtra("songIndex",songIndex);
startActivityForResult( in,100);
finish();
}
private void finish() {
getActivity().getSupportFragmentManager().beginTransaction().remove(this).commit();
}
#Override
public void onActivityCreated(Bundle savedInstanceState) {
Log.d(TAG, "onActivityCreated");
if(savedInstanceState != null) {
Log.d(TAG, "saved variable number: " + savedInstanceState.getInt("number"));
}
super.onActivityCreated(savedInstanceState);
// lv=getListView();
}
#Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
// lv = getListView();
}
}
try move your getListView();to onActivityCreated and make sure you call super.onActivityCreated(savedInstanceState) first
I think you should check the code in onItemClick of ListFragment (not the code of onItemClick inside onCreateView method):
Intent in = new Intent(null, AndroidBuildingMusicPlayerActivity.class);
Just change it to:
Intent in = new Intent(getActivity(), AndroidBuildingMusicPlayerActivity.class);
I wanna add clickableItems to Listview. But my problem is, that my class extands Fragment and i can not do that easily. Additionaly i wanna by clicking the item change the page to another page. My code is:
public class MyCoursesFragment extends Fragment implements OnItemClickListener, OnClickListener {
private static final String TAG = MyCoursesFragment.class.getName();
private ListView listView;
String listItem[]={"Vl1", "VL2"};
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View layout = inflater.inflate(R.layout.mycourses_layout, container, false);
listView = (ListView) layout.findViewById(R.id.Course);
listView.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
Intent intent = new Intent(getActivity().getBaseContext(), DocumentFragment.class);
intent.putExtra("id", "1");
listView.getContext().startActivity(intent);
FrontPageFragment.this.startActivity(intent);
}
});
return layout;
}
#Override
public void onItemClick(AdapterView<?> adapter, View view, int position, long id) {
}
}
can you guys pls help me.
THX
Change this line because your context is wrong:
listView.getContext().startActivity(intent);
FrontPageFragment.this.startActivity(intent);
You need to start a new activity in the following way:
Intent intent = new Intent(getActivity(),DocumentFragment.class);
startActivity(intent);
What you can to do is set up a callback Interface and control everything from your parent Activity.
Your Activity should implement the interface and your Fragment should call those methods when your View or list item gets clicked.
Something along the lines of:
interface
public interface OnListItemClickCallback {
public void onListItemShortClick(View view);
public void onListItemLongClick(View view);
}
Fragment
public MyListFragment extends ListFragment
implements OnItemClickListener, OnItemLongClickListener {
private OnListItemClickCallback clickCallback;
...
#Override
public void onAttach(Activity activity)
{
super.onAttach(activity);
// make sure the parent activity implements your interface
try
{
clickCallback = (OnListItemClickCallback) activity;
}
catch (ClassCastException e)
{
Log.e(TAG, "Parent Activity doesn't implement OnListItemClickCallback");
throw new ClassCastException(activity.toString()
+ " must implement OnListItemClickCallback");
}
}
#Override
public View onCreateView(Bundle b) {
...
getListView().setOnListItemClickListener(this):
getListView().setOnListItemLongClickListener(this):
}
#Override
public void onItemClick(...) {
clickCallback.onListItemShortClick(view)
}
// same idea for long click if you want it
}
Activity
public MyActivity extends Activity
implements OnListItemClickCallback {
...
#Override
public void onListItemShortClick(View view) {
// do something like replace your fragment
}
#Override
public void onListItemLongClick(View view) {
// do something else
}
}