Animating a shared element transition between two fragments seems to be a pain. I have it working elsewhere in my app with the exact same code.
here is my on click code inside a recyclerview adapter:
private void click(int position, ItemViewHolder holder){
final ItemDescription itemDescription = itemListFiltered.get(position);
FragmentManager fm = mContext.getSupportFragmentManager();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP && itemDescription != null) {
//animate transition
TransitionSet transitionSet = new TransitionSet();
transitionSet.addTransition(new ChangeTransform());
transitionSet.addTransition(new ChangeBounds());
transitionSet.addTransition(new ChangeImageTransform());
transitionSet.setDuration(300);
//set up fragment
ScanDetailsFragment frag = ScanDetailsFragment.newInstance(itemDescription, holder.productImage.getTransitionName());
frag.setEnterTransition(transitionSet);
frag.setExitTransition(transitionSet);
fm.beginTransaction().replace(R.id.fragment_container, frag)
.addSharedElement(holder.productImage, "product_image")
.addToBackStack("item details")
.commit();
}else {
ScanDetailsFragment itemDetails = ScanDetailsFragment.newInstance(itemDescription);
fm.beginTransaction().replace(R.id.fragment_container, itemDetails).addToBackStack("item details").commit();
}
I just can't seem to get this animation to work.
here is how I did it
Fragment with the RecyclerView
public class DogFragment extends Fragment implements DogAdapter.OnItemCLickListener {
public static final String TAG = "DogFragment";
#InjectView(R.id.recyclerview_dog)
RecyclerView mRecyclerView;
DogAdapter adapter;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
super.onCreateView(inflater, container, savedInstanceState);
final View v = inflater.inflate(R.layout.fragment_dog, container, false);
ButterKnife.inject(this, v);
return v;
}
#Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
final List<Integer> images = new ArrayList<>();
images.add(R.drawable.dog1);
images.add(R.drawable.dog2);
images.add(R.drawable.dog3);
final RecyclerView.LayoutManager layoutManager = new GridLayoutManager(getActivity(), 1);
mRecyclerView.setLayoutManager(layoutManager);
adapter = new DogAdapter(images);
adapter.setOnItemClickListener(this);
mRecyclerView.setAdapter(adapter);
}
public String getTAG() {
return TAG;
}
#Override
public void onItemClick(int position, int imageRefId, ImageView imageView) {
SingleDogFragment fragment = SingleDogFragment.getInstance(getActivity(), adapter.getImageTransitionName(getActivity(), position), imageRefId);
getFragmentManager().beginTransaction()
.addSharedElement(imageView, adapter.getImageTransitionName(getActivity(), position))
.replace(R.id.container, fragment, fragment.getTag())
.addToBackStack(null)
.commit();
}
}
The adapter
public class DogAdapter extends RecyclerView.Adapter<DogAdapter.ViewHolder> {
private OnItemCLickListener mItemClickListener;
private List<Integer> mImages;
public DogAdapter(List<Integer> images) {
mImages = images;
}
public int getItem(int position) {
return mImages.get(position);
}
#Override
public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
View v = LayoutInflater.from(viewGroup.getContext())
.inflate(R.layout.gridview_element_dog, viewGroup, false);
return new ViewHolder(v);
}
#Override
public void onBindViewHolder(ViewHolder viewHolder, int i) {
viewHolder.mImageView.setImageResource(getItem(i));
viewHolder.mImageView.setTransitionName(
getImageTransitionName(viewHolder.mImageView.getContext(), i)
);
}
public String getImageTransitionName(Context context, int position) {
return context.getString(R.string.dog_transition_name) + position;
}
#Override
public int getItemCount() {
return mImages.size();
}
public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
#InjectView(R.id.dog_imageview)
ImageView mImageView;
public ViewHolder(final View view) {
super(view);
ButterKnife.inject(this, view);
view.setOnClickListener(this);
}
#Override
public void onClick(View v) {
if (mItemClickListener != null) {
mItemClickListener.onItemClick(getPosition(), getItem(getPosition()), mImageView);
}
}
}
public interface OnItemCLickListener {
public void onItemClick(int position, int refId, ImageView imageView);
}
public void setOnItemClickListener(final OnItemCLickListener mItemClickListener) {
this.mItemClickListener = mItemClickListener;
}
}
Target fragment
public class SingleDogFragment extends Fragment {
private static final String ARG_TRANSITION_NAME = "ARG_TRANSITION_NAME";
private static final String ARG_IMAGE_REF_ID = "ARG_IMAGE_REF_ID";
#InjectView(R.id.single_dog_imageview)
ImageView mDogImage;
public static SingleDogFragment getInstance(Context context, String transitionName, int imageRefId) {
SingleDogFragment fragment = new SingleDogFragment();
Bundle bundle = new Bundle();
bundle.putString(ARG_TRANSITION_NAME, transitionName);
bundle.putInt(ARG_IMAGE_REF_ID, imageRefId);
fragment.setSharedElementEnterTransition(TransitionInflater.from(context).inflateTransition(R.transition.change_transform));
fragment.setEnterTransition(TransitionInflater.from(context).inflateTransition(R.transition.slide_left));
fragment.setExitTransition(TransitionInflater.from(context).inflateTransition(R.transition.slide_left));
fragment.setArguments(bundle);
return fragment;
}
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
super.onCreateView(inflater, container, savedInstanceState);
View v = inflater.inflate(R.layout.fragment_single_dog, container, false);
ButterKnife.inject(this, v);
mDogImage.setTransitionName(getArguments().getString(ARG_TRANSITION_NAME));
mDogImage.setImageResource(getArguments().getInt(ARG_IMAGE_REF_ID));
return v;
}
}
change_transform.xml
<?xml version="1.0" encoding="utf-8"?>
<transitionSet xmlns:android="http://schemas.android.com/apk/res/android">
<targets>
<target android:excludeId="#android:id/statusBarBackground"/>
<target android:excludeId="#android:id/navigationBarBackground"/> <!-- exclude the status bar and the navigation bar of the animation -->
</targets>
<changeBounds/>
<changeTransform/>
</transitionSet>
And here is what it look like
Hope I could help !
Related
I'm trying to make a tab layout with each page having a RecyclerView inside of it. I'm using fragments with RecyclerView in them, then i put them inside of ViewPager. The problem is that even though tabs are being shown it's content is always empty.
My Activity class
public class Spells_Act extends AppCompatActivity {
private ViewPager pager;
private Spells_Slide slider;
private TabLayout tabLayout;
#Override
protected void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.spells);
tabLayout = findViewById(R.id.tab);
pager = findViewById(R.id.pager);
slider = new Spells_Slide(getSupportFragmentManager());
slider.AddFragment(new Fragment1(), "one");
slider.AddFragment(new Fragment1(), "two");
slider.AddFragment(new Fragment1(), "three");
pager.setAdapter(slider);
tabLayout.setupWithViewPager(pager);
}
}
My ViewPager adapter
public class Spells_Slide extends FragmentPagerAdapter {
private Context context;
LayoutInflater inflater;
private List<RecyclerView> pages;
private List<String> list = new LinkedList<>();
private final List<Fragment> stFragment = new ArrayList();
public Spells_Slide(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int position) {
return stFragment.get(position);
}
#Nullable
#Override
public CharSequence getPageTitle(int position) {
return list.get(position);
}
#Override
public int getCount() {
return list.size();
}
#Override
public boolean isViewFromObject(#NonNull View view, #NonNull Object
object) {
return false;
}
public void AddFragment(Fragment fragment, String title) {
list.add(title);
stFragment.add(fragment);
}
My Fragment class
public class Fragment1 extends Fragment {
View v;
private RecyclerView recycler;
Adapter_Rec adapter;
private List<Spells> Spells = new LinkedList<>();
#Override
public void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Spells.add(new Spells("Ghost Sound", "Figment sounds", 0));
Spells.add(new Spells("Disrupt Undead", " Deals 1d6 damage to one undead", 0));
Spells.add(new Spells("Resistance", "Subject gains +1 on saving throws", 0));
Spells.add(new Spells("Ray of Frost", "Ray deals 1d3 cold damage", 0));
}
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, Bundle savedInstanceState) {
v = inflater.inflate(R.layout.frag1, container, false);
recycler = v.findViewById(R.id.frag_recycler);
recycler.setLayoutManager(new LinearLayoutManager(getActivity()));
adapter = new Adapter_Rec(Spells, getContext());
recycler.setAdapter(adapter);
return v;
}
}
And finally my RecyclerViewAdapter
public class Adapter_Rec extends
RecyclerView.Adapter<Adapter_Rec.MyViewHolder> {
private List<Spells> list = new LinkedList<>();
Context context;
public Adapter_Rec(List<Spells> list, Context context)
{
this.context=context;
this.list=list;
}
#Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v;
v = LayoutInflater.from(context).inflate
(R.layout.spells_recycle,parent,false);
MyViewHolder vholder = new MyViewHolder(v);
return vholder;
}
#Override
public void onBindViewHolder(MyViewHolder holder, int position) {
holder.check.setOnCheckedChangeListener(null);
holder.name.setText(list.get(position).getName());
holder.benefits.setText(list.get(position).getDescription());
}
#Override
public int getItemCount() {
return list.size();
}
public static class MyViewHolder extends RecyclerView.ViewHolder
{
TextView name;
TextView benefits;
CheckBox check ;
public MyViewHolder(View item)
{ super(item);
name = item.findViewById(R.id.namee);
benefits = item.findViewById(R.id.desc);
check =item.findViewById(R.id.check);
}
}
}
delete isViewFromObject method, or return super.isViewFromObject(view, object);
#Override
public boolean isViewFromObject(#NonNull View view, #NonNull Object
object) {
//return false; // return false will never show this fragment
return super.isViewFromObject(view, object);
}
I am trying to add a button inside my recyclerview's cardview holder.
When the button is pressed, the user is directed to an external website when clicked.I also want to add multiple buttons to each cardview so that each button leads to a different website when clicked. How to do this?
My code:
Fragment class (i am using fragments):
public class BooksFragment extends Fragment {
private RecyclerView mRecyclerView;
private RecyclerView.Adapter mAdapter;
private RecyclerView.LayoutManager mLayoutManager;
#Nullable
#Override
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_books, container, false);
mLayoutManager = new LinearLayoutManager(this.getActivity());
return view;
}
#Override
public void onViewCreated(#NonNull View view, #Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
ArrayList<exampleItemBooks> exampleList = new ArrayList<>();
exampleList.add(new exampleItemBooks(R.drawable.thritysixstrategems, "The Thirty-Six Stratagems", "300 years ago, an unknown author compiled the Stratagems, a strangely seductive meditation on deception.", "-Unknown Author"));
exampleList.add(new exampleItemBooks(R.drawable.medidations, "Meditations", "A book by the great Stoic Roman Emperor Marcus Aurelius. The book was written in Koine Greek, during the Emperor's reign.", "-Marcus Aurelius"));
mRecyclerView = view.findViewById(R.id.recyclerViewBooks);
mRecyclerView.setHasFixedSize(false);
mAdapter = new exampleBooksAdapter(exampleList);
mRecyclerView.setLayoutManager(mLayoutManager);
mRecyclerView.setAdapter(mAdapter);
}
#Override
public void onActivityCreated(#Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
}
}
Adapter:
public class exampleBooksAdapter extends RecyclerView.Adapter<exampleBooksAdapter.ExampleViewHolder> {
private ArrayList<exampleItemBooks> mExampleList;
public static class ExampleViewHolder extends RecyclerView.ViewHolder {
public ImageView mImageView;
public TextView mTextView1;
public TextView mTextView2;
public TextView mTextView3;
public ExampleViewHolder(View itemView) {
super(itemView);
mImageView = itemView.findViewById(R.id.imageViewCards);
mTextView1 = itemView.findViewById(R.id.textViewCard1);
mTextView2 = itemView.findViewById(R.id.textViewCard2);
mTextView3 = itemView.findViewById(R.id.textViewCard3);
}
}
public exampleBooksAdapter(ArrayList<exampleItemBooks> exampleItemBooks) {
mExampleList = exampleItemBooks;
}
#Override
public ExampleViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.example_item_books, parent, false);
ExampleViewHolder evh = new ExampleViewHolder(v);
return evh;
}
#Override
public void onBindViewHolder(#NonNull ExampleViewHolder holder, int position) {
exampleItemBooks currentItem = mExampleList.get(position);
holder.mImageView.setImageResource(currentItem.getImageResource());
holder.mTextView1.setText(currentItem.getText1());
holder.mTextView2.setText(currentItem.getText2());
holder.mTextView3.setText(currentItem.getText3());
}
#Override
public int getItemCount() {
return mExampleList.size();
}
}
ArrayList class:
private int mImageResource;
private String mText1;
private String mText2;
private String mText3;
public exampleItemBooks(int imageResource, String text1, String text2, String text3) {
mImageResource = imageResource;
mText1 = text1;
mText2 = text2;
mText3 = text3;
}
public int getImageResource() {
return mImageResource;
}
public String getText1() {
return mText1;
}
public String getText2() {
return mText2;
}
public String getText3() {
return mText3;
}
}
Add a button to your viewholder layout and
#Override
public void onBindViewHolder(#NonNull ExampleViewHolder holder, int position) {
exampleItemBooks currentItem = mExampleList.get(position);
...
holder.mButton.setTag("URL OF THE SITE");
holder.mButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// Take user to browser.
}
});
}
I have created a RecyclerView and added cards in fragment.I want to open different activities by clicking on different cards in CardView.But I am only able to open the same activity for all the cards.I searched different answers in StackOverFlow.But I couldn't.
could you tell me how to solve this issue...? If you can give full code,I'm happy.
Here are my codes...
PDFFragment.java
public class PDFFragment extends Fragment {
View v;
List<Pdf> listBook;
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
v= inflater.inflate(R.layout.fragment_pdf,container,false);
listBook = new ArrayList<>();
listBook.add(new Pdf("1-pdf name",R.drawable.pdf1image));
listBook.add(new Pdf("2-pdf name",R.drawable.pdf2image));
listBook.add(new Pdf("3-pdf name",R.drawable.pdf3image));
listBook.add(new Pdf("4-pdf name",R.drawable.pdf4image);
listBook.add(new Pdf("5-pdf name",R.drawable.pdf5image));
listBook.add(new Pdf("6-pdf name",R.drawable.pdf6image));
listBook.add(new Pdf("7-pdf name",R.drawable.pdf7image));
RecyclerView myTV= (RecyclerView) v.findViewById(R.id.recyclerview_id);
RecyclerViewAdapterPDF myAdapter = new RecyclerViewAdapterPDF(getActivity(),listBook);
//Part 1 -Start Changing number of columns
myTV.setLayoutManager(new GridLayoutManager(getActivity(),calculateNoOfColumns(getActivity())));
//Part 1 -End Changing number of columns
myTV.setAdapter(myAdapter);
return v;
}
//Part 2 -Start Changing number of columns
public int calculateNoOfColumns(Context context) {
DisplayMetrics displayMetrics = context.getResources().getDisplayMetrics();
float dpWidth = displayMetrics.widthPixels / displayMetrics.density;
int noOfColumns = (int) (dpWidth / 120);
return noOfColumns;
}
//Part 2 -End Changing number of columns
}
RecyclerViewAdapterPDF.java
public class RecyclerViewAdapterPDF extends RecyclerView.Adapter<RecyclerViewAdapterPDF.MyViewHolder> {
private Context mContext;
private List<Pdf> mData;
public RecyclerViewAdapterPDF(Context mContext, List<Pdf> mData) {
this.mContext = mContext;
this.mData = mData;
}
#Override
public MyViewHolder onCreateViewHolder( ViewGroup parent, int viewType) {
View view;
LayoutInflater mInflater = LayoutInflater.from(mContext);
view = mInflater.inflate(R.layout.cardview_item_pdf,parent,false);
return new MyViewHolder(view);
}
#Override
public void onBindViewHolder( MyViewHolder holder, int position) {
holder.tv_book_title.setText(mData.get(position).getTitle());
holder.img_book_thumbnail.setImageResource(mData.get(position).getPhoto());
}
#Override
public int getItemCount() {
return mData.size();
}
public static class MyViewHolder extends RecyclerView.ViewHolder{
TextView tv_book_title;
ImageView img_book_thumbnail;
public MyViewHolder(View itemView) {
super(itemView);
tv_book_title = (TextView) itemView.findViewById(R.id.book_title_id);
img_book_thumbnail = (ImageView) itemView.findViewById(R.id.book_image_id);
}
}
}
ViewPagerAdapter.java
public class ViewPagerAdapter extends FragmentPagerAdapter {
private final List<Fragment> fragmentList = new ArrayList<>();
private final List<String> FragmentListTitles = new ArrayList<>();
public ViewPagerAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int position) {
return fragmentList.get(position);
}
#Override
public int getCount() {
return FragmentListTitles.size();
}
#Nullable
#Override
public CharSequence getPageTitle(int position) {
return FragmentListTitles.get(position);
}
public void AddFragment(Fragment fragment,String Title){
fragmentList.add(fragment);
FragmentListTitles.add(Title);
}
}
You need a reference to your card on the view holder:
public static class MyViewHolder extends RecyclerView.ViewHolder{
TextView tv_book_title;
ImageView img_book_thumbnail;
CardView cardview;
public MyViewHolder(View itemView) {
super(itemView);
tv_book_title = (TextView) itemView.findViewById(R.id.book_title_id);
img_book_thumbnail = (ImageView) itemView.findViewById(R.id.book_image_id);
cardview = itemView.findViewById(R.id.yourCardId);
}
}
And on the onBindViewHolder() you set a click listener responsible for opening the activity:
#Override
public void onBindViewHolder( MyViewHolder holder, int position) {
holder.tv_book_title.setText(mData.get(position).getTitle());
holder.img_book_thumbnail.setImageResource(mData.get(position).getPhoto());
holder.cardView.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent intent;
switch (position) {
case 0:
intent = new Intent(mContext, pdfone.class);
mContext.startActivity(intent);
break;
case 1:
intent = new Intent(mContext, pdftwo.class);
mContext.startActivity(intent);
break;
...
//do the same for every card possible
}
}
}
Use reflection like this
So in your case, something like:
String activityToStart = "com.pkg.pdf"+position;
try {
Class<?> c = Class.forName(activityToStart);
Intent intent = new Intent(this, c);
startActivity(intent);
} catch (ClassNotFoundException e) {
// print exception
}
Put it in adapter when page is to be changed.
I am trying to implement RecyclerView inside a Fragment. I have designed a CustomAdapter(StateAdapter) class to load the contents of RecyclerView and an interface StateAdapterOnClickHandler to handle onClicks of the items inside RecyclerView but clicking on item doesn't work.
Here's my HomeFragment.java which extends Fragment. I am unable to figure out how to initialize the StateAdapterOnClickHandler and where to call its method onItemClick()
public class HomeFragment extends Fragment implements StateAdapter.StateAdapterOnClickHandler {
private View homeFragment;
private RecyclerView recyclerView;
private StateAdapter stateAdapter;
private List<String> states;
#Override
public void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
public static HomeFragment newInstance() {
HomeFragment homeFragment = new HomeFragment();
return homeFragment;
}
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
homeFragment = inflater.inflate(R.layout.fragment_home,container,false);
recyclerView = homeFragment.findViewById(R.id.recyclerview_states);
states = Arrays.asList(getResources().getStringArray(R.array.india_states));
LinearLayoutManager layoutManager = new LinearLayoutManager(container.getContext(),LinearLayoutManager.VERTICAL,false);
recyclerView.setLayoutManager(layoutManager);
recyclerView.setHasFixedSize(true);
stateAdapter = new StateAdapter();
stateAdapter.setStateNames(states);
recyclerView.setAdapter(stateAdapter);
return homeFragment;
}
#Override
public void onItemClick(String state) {
Intent intent = new Intent(getActivity(),DetailActivity.class);
intent.putExtra("State",state);
startActivity(intent);
}
}
Here's my StateAdapter class
public class StateAdapter extends RecyclerView.Adapter<StateAdapter.StateViewHolder> {
private List<String> stateNames;
private final StateAdapterOnClickHandler stateAdapterOnClickHandler;
StateAdapter(StateAdapterOnClickHandler stateAdapterOnClickHandler){
this.stateAdapterOnClickHandler = stateAdapterOnClickHandler;
}
public interface StateAdapterOnClickHandler {
void onItemClick(String state);
}
#Override
public StateAdapter.StateViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
Context context = parent.getContext();
int idForListItem = R.layout.state_layout;
LayoutInflater inflater = LayoutInflater.from(context);
View view = inflater.inflate(idForListItem,parent,false);
return new StateViewHolder(view);
}
#Override
public void onBindViewHolder(StateAdapter.StateViewHolder holder, int position) {
String stateName = stateNames.get(position);
holder.mTextView.setText(stateName);
}
#Override
public int getItemCount() {
if(stateNames == null) return 0;
return stateNames.size();
}
public class StateViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
public final TextView mTextView;
public StateViewHolder(View itemView) {
super(itemView);
mTextView = (TextView) itemView.findViewById(R.id.tv_state_name);
itemView.setOnClickListener(this);
}
#Override
public void onClick(View v) {
int clickedPostion = getAdapterPosition();
String state = stateNames.get(clickedPostion);
stateAdapterOnClickHandler.onItemClick(state);
}
}
public void setStateNames(List<String> states) {
stateNames = states;
notifyDataSetChanged();
}
}
Please, tell me what's wrong with the code while handling the clicks. I am new to Android Programming and this is first time I am working with Fragments. Thanks for helping.
I have Fragment.java and its XML. In XML I put ListView and I want to update the ListView in Fragment and add some String.
I am trying to make a custom adapter. When I run the ListView in Activity with custom adapter all works great, but when I put the same code in Fragment nothing happens. I am using EventBus3.
This is my Activity send Event
EventBus.getDefault().post(new SendDataHelper(nameOfItem[myItemInt]));
This is my Fragment:
public class MyOrderFragment extends Fragment {
TextView name;
ListView listOrder;
private Context context;
List<cources> collegeList;
public MyOrderFragment() {
// Required empty public constructor
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
EventBus.getDefault().register(this);
}
#Subscribe
public void onMessageEvent(SendDataHelper event){
Toast.makeText(getActivity(), event.getText(), Toast.LENGTH_SHORT).show();
String text = event.getText();
name.setText(text);
cources college;
collegeList = new ArrayList<cources>();
college = new cources();
college.name = "sharon";
collegeList.add(college);
listOrder.setVisibility(View.VISIBLE);
ListAdapter adapter = new ListAdapter(collegeList, context);
listOrder.setAdapter(adapter);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.my_order_fragment, container, false);
name = (TextView) rootView.findViewById(R.id.textView);
listOrder = (ListView) rootView.findViewById(R.id.listOrder);
return rootView;
}
}
This is my adapter
public class ListAdapter extends BaseAdapter
{
Context context;
List<cources> valueList;
public ListAdapter(List<cources> listValue, Context context)
{
this.context = context;
this.valueList = listValue;
}
#Override
public int getCount()
{
return this.valueList.size();
}
#Override
public Object getItem(int position)
{
return this.valueList.get(position);
}
#Override
public long getItemId(int position)
{
return position;
}
#Override
public View getView(int position, View convertView, ViewGroup parent)
{
ViewItem viewItem = null;
if(convertView == null)
{
viewItem = new ViewItem();
LayoutInflater layoutInfiater = (LayoutInflater)this.context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
//LayoutInflater layoutInfiater = LayoutInflater.from(context);
convertView = layoutInfiater.inflate(R.layout.list_adapter_view, null);
viewItem.txtNamePlanche = (TextView)convertView.findViewById(R.id.Tx_name_planche);
viewItem.txtPriceBaget = (TextView)convertView.findViewById(R.id.Tx_price_baget);
viewItem.txtPricePlate = (TextView)convertView.findViewById(R.id.Tx_price_plate);
convertView.setTag(viewItem);
}
else
{
viewItem = (ViewItem) convertView.getTag();
}
viewItem.txtNamePlanche.setText(valueList.get(position).name);
viewItem.txtPriceBaget.setText(valueList.get(position).price_baget);
viewItem.txtPricePlate.setText(valueList.get(position).price_plate);
return convertView;
}
}
This is my ItemView
import android.widget.TextView;
class ViewItem
{
TextView txtNamePlanche;
TextView txtPricePlate;
TextView txtPriceBaget;
}
This is cources:
public class cources
{
public String name;
public String price_plate;
public String price_baget;
}
Change extends Fragment to extends ListFragment