I am moving my code from Activity to use it under Fragment view. The grid has multiple images which gets successfully displayed inside the fragment.I am trying to display detailed view based on the image clicked. But I am unable to get to do this, tried different ways but resulting one or other error.Earlier I used to have setContentView which does not seem to work when used for Fragment. I am already inflating the Fragment under OnCreateView method but not sure how load with the detailed view upon the image click.
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_one, container, false);
int iconSize=getResources().getDimensionPixelSize(android.R.dimen.app_icon_size);
CustomGrid adapter = new CustomGrid(view.getContext(), iconSize);
GridView gridview = (GridView) view.findViewById(R.id.grid);
gridview.setAdapter(adapter);
gridview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
Toast.makeText(view.getContext(), "You Clicked at " +web[+ position], Toast.LENGTH_SHORT).show();
//setContentView(R.layout.list_item_main);
mListView = (ListView) view.findViewById(R.id.item_list_view);
Item.getRecipesFromDB(OneFragment.this, DBReference);
}
});
return view;
}
#Override
public void newDataReceived(ArrayList<Item> itemList) {
ItemAdapter adapter = new ItemAdapter(mListView.getContext(), itemList);
mListView.setAdapter(adapter);
}
Open a new Fragment when a gridView is clicked and then pass the position from the bundle parameters. Then in NewFragment show the listview and the data from database.
gridview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
NextFragment nextFrag= new NextFragment();
Bundle bundle = new Bundle();
bundle.putInt("POSITION",position);
this.getFragmentManager().beginTransaction()
.replace(R.id.Layout_container, nextFrag,TAG_FRAGMENT)
.addToBackStack(null)
.commit();
}
});
In the NextFragment get the reference of listView and then update the data from database using proper adapter.
Related
I have an expandable list view with a button and a textview in the group header.
I uses an adapter that implements the view holder.
I want to include a click event on the button such that I get the textview text that is in the group header and I want to pass the string to another fragment.
I also want to show the other fragment.
I am stuck as I can`t figure out,
How to read the textview value from the expandable listview in the fragment on the button click
and
How to pass the value of Title to another fragment.
Do I need to use
GViewHolder.myButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
}
or
public void onItemClick(AdapterView<?> parent, View view,int position, long id) {
});
Please note that I can`t see public void onItemClick being hit when the button is clicked!
I have implemented an adapter as follows:
Part of the code is
public class ListViewAdapter extends BaseExpandableListAdapter {
class GViewHolder {
public TextView Title;
public Button myButton;
}
GViewHolder.myButton.setFocusable(false);
GViewHolder.myButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
// How to implement this in the fragment ???
}
});
}
Part of the fragment that implements the Adapter as follows:
private ListView eList = null;
InterFragmentNavigator interFragmentNavigator;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_event_list, container, false);
eList = (ListViewAdapter) view.findViewById(R.id.eView);
ListViewAdapter listDataAdapter = new ListViewAdapter(getActivity(), groupes);
eList.setAdapter(listDataAdapter);
eList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view,int position, long id) {
// Here, I want to read the textview value of Title next to button
TextView textViewItem = ((TextView) view.findViewById(R.id.Title));
String Title = textViewItem.getText().toString();
**//Here, I want to call another fragment and pass Title as parameter.**
return view;
}
lets say you are creating new fragment from the click then add something like.
Fragment detailFragment = new Fragment();
Bundle bundle = new Bundle();
bundle.putString("TITLE", "your title");
detailFragment.setArguments(bundle);
Then do the transection here. Then in the detailFragment you can do
Bundle bundle = getArguments();
String title = bundle.getString("TITLE") ;
I have this piece of code I want to show this dummy data in a static list but I get error in extends fragment and also in get activity, how can I solve them? Do I need to change any part of it?
public class AgendaFragment extends Fragment{
private ArrayAdapter<String> magendaAdapter;
public AgendaFragment() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
String[] data = {
"Monday going to restaurant at 14",
"Monday going to restaurant at 1",
"Monday going to restaurant at 1",
};
List<String> weekForecast = new ArrayList<String>(Arrays.asList(data));
magendaAdapter =
new ArrayAdapter<String>(
getActivity(), // The current context (this activity)
R.layout.list_item_agenda, // The name of the layout ID.
R.id.list_item_agenda_textview, // The ID of the textview to populate.
weekForecast);
View rootView = inflater.inflate(R.layout.fragment_main, container, false);
// Get a reference to the ListView, and attach this adapter to it.
ListView listView = (ListView) rootView.findViewById(R.id.listview_agenda);
listView.setAdapter(magendaAdapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
String forecast = magendaAdapter.getItem(position);
Toast.makeText(getActivity(), forecast, Toast.LENGTH_SHORT).show();
}
});
return rootView;
}
}
Add
import android.app.Fragment;
at the beginning of your code file.
(or android.support.v4.app.Fragment if you are using Android support library)
this is my first post on stackoverflow, so please excuse any shortcomings I might be having with norms.
I'm trying to have a tabbed view on an application, and have each tabbed view (just a normal fragment) be a gridview. I thought I could just put the initialization code for the gridview in the onCreateView, but ive been at this problem for a couple hours now, and the gridview is returning null in the findviewbyid call. Here's the relevant code (I'm trying not make this question overwhelming , so I won't post all the code
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// View rootView = inflater.inflate(
// R.layout.fragment_main_ingredient_dummy, container, false);
GridView gridview = (GridView) inflater.inflate(R.id.gridview, container);
gridview.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
Toast.makeText(getActivity(), "" + position, Toast.LENGTH_SHORT).show();
}
});
gridview.setAdapter(new ImageAdapter(getActivity()));
return gridview;
}
As I said. the fragment is an ordinary fragment, and its being "managed" by a FragmentPagerAdapter. However, I also have an extension of a BaseAdapter, for teh content inside the actual tab.
Thank you in advance!
You can't inflate a view like that. The inflate call needs an R.layout. reference, not an R.id. reference.
You should only create the view in onCreateView. Then do the other work in onViewCreated.
private GridView mGridView;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {;
mGridView = (GridView) inflater.inflate(R.layout.frag_grid, container);
return mGridView;
}
#Override
public void onViewCreated(){
mGridView.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
Toast.makeText(getActivity(), "" + position, Toast.LENGTH_SHORT).show();
}
});
mGridView.setAdapter(new ImageAdapter(getActivity()));
}
I am trying to get onItemClick on ListItems to work from a fragment. Here is my code:
public class MyBudgetPageMenuFragment extends Fragment {
private Context context;
private ListView listView;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View myFragmentView = inflater.inflate(R.layout.my_budget_listview,
container, false);
ListView listView = (ListView) myFragmentView
.findViewById(android.R.id.list);
context = this.getActivity().getApplicationContext();
String[] values = new String[4];
ListAdapter adapter = new ListAdapter(context, values);
listView.setAdapter(adapter);
listView.setOnItemClickListener( new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int position,
long arg3) {
selectItem(position);
}
});
return myFragmentView;
}
private void selectItem(int position) {
FragmentManager fragmentManager = getFragmentManager();
Fragment fragment;
switch (position) {
case 0:
fragment = new MyBudgetPageFragments();
fragmentManager.beginTransaction()
.replace(R.id.listFragment, fragment).commit();
break;
default:
String message1 = Integer.toString(position);
AlertDialog alertDialog = new AlertDialog.Builder(context).create();
alertDialog.setMessage("Position: " + message1);
alertDialog.show();
break;
}
}
}
But every time when I select an item, it isn't doing anything or throwing any exceptions. It seems that the event doesn't get registered. I debuged the code and it doesn't enter my event.
Can someoane tell me what I'm doing wrong?
Note that the ListView blocks clicks of an item that contains at least one focusable
descendant but it doesn’t make the content focus-reachable calling
setItemsCanFocus(true). One workaround is by disabling focusability of descendants, using
android:descendantFocusability="blocksDescendants"
in the layout defining your list item. (First learned of this myself from http://cyrilmottier.com/2011/11/23/listview-tips-tricks-4-add-several-clickable-areas/) Does your layout contain buttons? If so, you'd fall into this case.
Just go the simple way.
In your Adapter class, between getView method, initialize View v = convertView; then
v.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(context, "test "+ list.get(position).getS_name(), Toast.LENGTH_SHORT).show();
}
});
I can not get setOnItemClickListener of gridView in Fragment. What can be the problem?
Here is my code::
public class MainMenuFragment extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.main_menu_fragment, container, false);
itemsGridViewObj = (GridView) view.findViewById(R.id.itemsGridView);
itemsGridViewObj.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int position,
long arg3) {
Log.d(TAG, "--> onItemClick listener..."); // Can not getting this method.
/*if(position == 1) {
FruitMenuFragment fruitMenuFragment = new FruitMenuFragment();
fragmentTransaction.replace(android.R.id.content, fruitMenuFragment);
fragmentTransaction.commit();
}*/
}
});
return view;
}
}`
You may need to set the following in your ButtonView.
android:focusable="false"
android:focusableInTouchMode="false"
see adding CheckBox to list row loses my onItemClick events?
When using Fragments the initialisation of the view occurs over two stages.
The view is only inflated (and therefore accessible) after the onCreateView method. This method is only for inflating a view and returning it to the Fragment.
Therefore, any logic to do with finding views and setting up onClickListeners should be done in the onActivityCreated() function as this is the first point at which you can access the inflated view.
Have a look at the Google docs at http://developer.android.com/reference/android/app/Fragment.html#Lifecycle
Below is you code adjusted to comply to what I have described above:
public class MainMenuFragment extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.main_menu_fragment, container, false);
}
#Override
public void onActivityCreated(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
GridView itemsGridViewObj = (GridView) findViewById(R.id.itemsGridView);
itemsGridViewObj.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int position,
long arg3) {
Log.d(TAG, "--> onItemClick listener..."); // You should see this now
/*if(position == 1) {
FruitMenuFragment fruitMenuFragment = new FruitMenuFragment();
fragmentTransaction.replace(android.R.id.content, fruitMenuFragment);
fragmentTransaction.commit();
}*/
}});
}
}