I want to create fragments in my Android app activity which I can swipe through. More specifically those fragments contain images and I want to swipe through those.
Now to accomplish this I make use of the FragmentPagerAdapter. The problem I have now is that one of my methods in a class which extends FragmentPagerAdaptor requires a Fragment object to be returned but according to the compiler it finds a different object.
This is strange, because the object I return is from a class which extends Fragment and therefore it should work in my opinion.
The problematic method is getItem(int position) in the SlidesPageAdaptor class and the error is
Incompatible types
Required: android.support.v4.app.Fragment
Found: ourpackagename.fragment_slides
SlidesPageAdaptor.java
import android.content.Context;
import android.content.res.Resources;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
public class SlidesPageAdaptor extends FragmentPagerAdapter {
String[] devices;
String[] deviceDescription;
public SlidesPageAdaptor(FragmentManager fm, Context context) {
super(fm);
Resources resources = context.getResources();
devices = resources.getStringArray(R.array.devices);
deviceDescription = resources.getStringArray(R.array.device_description);
}
#Override
public Fragment getItem(int position) {
Bundle bundle = new Bundle();
bundle.putString(fragment_slides.DescriptionKey, deviceDescription[position]);
bundle.putInt(fragment_slides.ImageIDKey, getImageID(position));
fragment_slides fragmentSlide = new fragment_slides();
fragmentSlide.setArguments(bundle);
return fragmentSlide;
}
private int getImageID(int position) {
int id = 0;
switch (position)
{
case 0:
id = R.drawable.abc_btn_check_material;
break;
case 1:
id = R.drawable.abc_ab_share_pack_mtrl_alpha;
break;
case 2:
id = R.drawable.abc_btn_radio_material;
break;
}
return id;
}
#Override
public CharSequence getPageTitle(int position) {
return devices[position];
}
#Override
public int getCount() {
return devices.length;
}
}
And the fragment_slides.class (I know the name is bad, but not important now):
import android.app.Activity;
import android.net.Uri;
import android.os.Bundle;
import android.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
public class fragment_slides extends Fragment {
public static final String ImageIDKey = "imagekey";
public static final String DescriptionKey = "descriptionkey";
public fragment_slides() {
// Required empty public constructor
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_slides_images, container, false);
Bundle bundle = getArguments();
if(bundle != null)
{
int imageID = bundle.getInt(ImageIDKey);
String description = bundle.getString(DescriptionKey);
setValues(view, imageID, description);
}
return view;
//return inflater.inflate(R.layout.fragment_slides_images, container, false);
}
private void setValues(View view, int imageID, String description) {
ImageView imageview = (ImageView) view.findViewById(R.id.imageViewSlide);
imageview.setImageResource(imageID);
TextView textView = (TextView) view.findViewById(R.id.tvSlideTest);
textView.setText(description);
}
}
I don't think other activities/classes are required but I can provide them if needed.
In fragment_slides.class you should import android.support.v4.app.Fragment, not android.app.Fragment.
The problem is here:
fragment_slides fragmentSlide = new fragment_slides();
fragmentSlide.setArguments(bundle);
Since you're declaring a fragment_slides object rather than a Fragment object, the compiler thinks it isn't a Fragment. Try this instead:
Fragment fragmentSlide = new fragment_slides();
fragmentSlide.setArguments(bundle);
Related
Howdy, I'm working on a project and I'm running into some issues. Namely, I have two fragments, each with their own adapter as both fragments are meant to be able to swipe horizontally. A photo should be attached of the layout I have for further clarity. I would like to be able to tap the bottom fragment, which when I tap a place on the top fragment, the image I tapped or "Structure" will be placed down. However, what I can't figure out is how to smoothly transfer this data and if I were to do it through the activity, how do I do it as the activity has already run through all its code. Could somebody please help, it would be greatly appreciated.
Main Activity
public class MainMap extends AppCompatActivity
{
MapData data = MapData.get();
StructureData structure = StructureData.get();
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main_map);
FragmentManager frag = getSupportFragmentManager(); //To manage the map fragment
MapCellFragment rv = (MapCellFragment) frag.findFragmentById(R.id.map);
FragmentManager selectionFrag = getSupportFragmentManager(); //To manage the selection fragment
SelectionCellFragment cf = (SelectionCellFragment) selectionFrag.findFragmentById(R.id.bar);
if (rv == null)
{
rv = new MapCellFragment(data);
frag.beginTransaction().add(R.id.map, rv).commit();
}
if (cf == null)
{
cf = new SelectionCellFragment(structure);
selectionFrag.beginTransaction().add(R.id.bar, cf).commit();
}
}
Map Fragment
public MapData data;
public MapCellFragment() {
// Required empty public constructor
}
public MapCellFragment(MapData data)
{
this.data = data;
}
/**
* Use this factory method to create a new instance of
* this fragment using the provided parameters.
*
* #param param1 Parameter 1.
* #param param2 Parameter 2.
* #return A new instance of fragment gridCellFragment.
*/
// TODO: Rename and change types and number of parameters
public static MapCellFragment newInstance(String param1, String param2) {
MapCellFragment fragment = new MapCellFragment();
Bundle args = new Bundle();
args.putString(ARG_PARAM1, param1);
args.putString(ARG_PARAM2, param2);
fragment.setArguments(args);
return fragment;
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getArguments() != null) {
mParam1 = getArguments().getString(ARG_PARAM1);
mParam2 = getArguments().getString(ARG_PARAM2);
}
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.map_fragment, container, false);
RecyclerView rv = (RecyclerView) view.findViewById(R.id.recyclerview);
rv.setLayoutManager(new GridLayoutManager(getActivity(), MapData.HEIGHT, GridLayoutManager.HORIZONTAL,false));
MapAdapter myAdapter = new MapAdapter(data);
rv.setAdapter(myAdapter);
return view;
}
Bottom Bar Fragment
This was essentially a copy and paste of the other fragment, just changing the data which goes in and the XML it's attached to.
Map Adapter
package com.example.map;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class MapAdapter extends RecyclerView.Adapter<MapViewHolder>
{
MapData data;
public MapAdapter(MapData data)
{
this.data = data;
}
#NonNull
#Override
public MapViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType)
{
LayoutInflater layoutInflater = LayoutInflater.from(parent.getContext());
View view = layoutInflater.inflate(R.layout.grid_cells,parent,false);
//Resizes the images within the grid cells xml file
int size = parent.getMeasuredHeight() / MapData.HEIGHT + 1;
ViewGroup.LayoutParams lp = view.getLayoutParams();
lp.width = size;
lp.height = size;
MapViewHolder myViewHolder = new MapViewHolder(view);
return myViewHolder;
}
#Override
public void onBindViewHolder(#NonNull MapViewHolder holder, int position)
{
int row = position % MapData.HEIGHT;
int col = position / MapData.HEIGHT;
MapElement ele = data.get(row, col);
holder.ne.setImageResource(ele.getNorthEast());
holder.nw.setImageResource(ele.getNorthWest());
holder.se.setImageResource(ele.getSouthEast());
holder.sw.setImageResource(ele.getSouthWest());
if(ele.getStructure() != null)
{
holder.mapStruct.setImageResource(ele.getStructure().getDrawableId());
}
}
#Override
public int getItemCount()
{
return 300;
}
}
Map View Holder
package com.example.map;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import java.util.Objects;
public class MapViewHolder extends RecyclerView.ViewHolder {
ImageView nw;
ImageView ne;
ImageView sw;
ImageView se;
ImageView mapStruct;
public MapViewHolder(#NonNull View itemView)
{
super(itemView);
nw = itemView.findViewById(R.id.northWest);
ne = itemView.findViewById(R.id.northEast);
sw = itemView.findViewById(R.id.southWest);
se = itemView.findViewById(R.id.southEast);
mapStruct = itemView.findViewById(R.id.structure);
}
}
Bar Adapter
package com.example.map;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class SelectionAdapter extends RecyclerView.Adapter<SelectionViewHolder>
{
StructureData data;
public SelectionAdapter(StructureData data)
{
this.data = data;
}
#NonNull
#Override
public SelectionViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType)
{
LayoutInflater layoutInflater = LayoutInflater.from(parent.getContext());
View view = layoutInflater.inflate(R.layout.list_selection,parent,false);
SelectionViewHolder myViewHolder = new SelectionViewHolder(view);
return myViewHolder;
}
#Override
public void onBindViewHolder(#NonNull SelectionViewHolder holder, int position)
{
Structure s = data.get(position);
holder.structure.setImageResource(s.getDrawableId());
holder.label.setText(s.getLabel());
holder.bind(s);
}
#Override
public int getItemCount()
{
return data.size();
}
}
Bar ViewHolder
package com.example.map;
import android.annotation.SuppressLint;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import java.util.Objects;
public class SelectionViewHolder extends RecyclerView.ViewHolder
{
Structure selectObject;
ImageView structure;
TextView label;
public SelectionViewHolder(#NonNull View itemView)
{
super(itemView);
structure = itemView.findViewById(R.id.image);
label = itemView.findViewById(R.id.label);
structure.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View view)
{
SelectionCellFragment.selectedData = selectObject;
}
});
}
public void bind(Structure s)
{
selectObject = s;
}
//Maybe, I need to place this bind function in MapViewHolder as well
//When I press on the button, I must share the information over to the Map fragment
//Then the new information will form a ImageView on the big one
}
You could use ViewModel to solve your use-case. You could have something like SelectedStructureViewModel that will be set in the SelectionCellFragment when the user taps to select the structure. While the MapCellFragment can observe this SelectedStructureViewModel to be notified whenever the user selects the structure. You can then query it when user taps a place on the Map.
I would like to change the fragment when listview item clicked under Bottom navigation activity
But I have not idea how to write the OnClickListener
Anyone can provide some hints or tell me what wrong in this program?
Here is the program
And thank you for spend the time to view my program
Thank you very much
package com.example.campus.ui.campus;
import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
import com.example.c.MainActivity;
import com.example.c.R;
import com.example.c.database.campus.CampusData;
import com.example.c.ui.campusInformation.CampusInformationActivity;
import com.example.c.ui.campusInformation.CampusInformationFragment;
import java.util.ArrayList;
import java.util.List;
public class CampusListLayoutAdapter extends BaseAdapter {
private LayoutInflater layoutInflater;
private List<CampusData> campusList = new ArrayList<CampusData>();
private int resourceViewID;
private Context context;
private Context mContext;
static class ViewHolder{
LinearLayout llCampusCard;
TextView tvCampusName;
TextView tvCampusAddress;
ImageView ivCampusImage;
}
public CampusListLayoutAdapter(Context c, List<CampusData> campusList){
context = c;
layoutInflater = LayoutInflater.from(c);
this.campusList = campusList;
}
#Override
public int getCount() {
return campusList.size();
}
#Override
public Object getItem(int i) {
return i;
}
#Override
public long getItemId(int i) {
return i;
}
#Override
public View getView(int i, View view, ViewGroup viewGroup) {
mContext = viewGroup.getContext();
ViewHolder holder = new ViewHolder();
view = layoutInflater.inflate(R.layout.listview_campus, null);
if(view != null){
holder.tvCampusName = view.findViewById(R.id.tvCampusName);
holder.tvCampusAddress = view.findViewById(R.id.tvCampusAddress);
holder.ivCampusImage = view.findViewById(R.id.ivCampusImage);
holder.tvCampusName.setText(campusList.get(i).name);
holder.tvCampusAddress.setText(campusList.get(i).address);
String image = campusList.get(i).image;
resourceViewID = context.getResources().getIdentifier(image, "drawable", context.getPackageName());
holder.ivCampusImage.setImageResource(resourceViewID);
view.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
}
});
return view;
}else {
return null;
}
}
}
First of all, create an interface in your adapter class.
public interface Callbacks {
void onItemClick(YourObject object, int position); // arguments as per the requirement
}
change your constructor of adapter as
Callbacks callback;
public CampusListLayoutAdapter(Context c, List<CampusData> campusList, Callbacks callback){
.....
this.callback = callback;
}
Now in your onClick() use..
callback.onItemClick(yourClickedItem, position)
then pass an anonymous or simply implement interface in your activity.
adapter = new CampusListLayoutAdapter(context, list, new CampusListLayoutAdapter.Callbacks() {
#Override
public void onItemClick(Alert_bean alert, int position) {
// do what you want here in activity like changing fragment or view updates
}
});
I am making a tour app, it structure is like this:
First activity shows seasons like Summer, Winter, Spring, Autumn. After selecting any season, another activity pops up with locations around the world best suited for that season. After selecting any one location, another activity will pop up with 2 to 3 images with the description, and we will slide through these images. That's it. To make it more accessible, I tried adding this code to NawDrawer activity:
Debugging Error Message Image
Originally the ViewPagerAdapter.java was for Hawaii (check the image), but I created exact same as Yoshimiteadapter.java for Yosemite, but it's not working.
I apologize for the inconvenience caused by the files :) ,it's messy ,but desperate help is needed:
Link to android studio project zip file
There are total 7 java files and 12 activity files although I have an error in yosemiteadapter.Java line no 35 , error message is as follows:
04-16 12:00:09.563 12511-12511/com.example.android.myapplication E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.android.myapplication, PID: 12511
java.lang.ArrayIndexOutOfBoundsException: length=1; index=1
at com.example.android.myapplication.yoshimiteadapter.instantiateItem(yoshimiteadapter.java:35)
**Code for the app as follows,I have block quoted the line of error **
yoshimiteadapter.java
package com.example.android.myapplication;
import android.content.Context;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
/**
* Created by sanchitdeshmukh on 17/03/18.
*/
public class yoshimiteadapter extends PagerAdapter {
private Context context;
private LayoutInflater layoutInflater;
private Integer []images1 ={R.drawable.yosemite1,R.drawable.yoshemite2,R.drawable.yoshemite3};
private Integer []strings1 = {R.string.yoshimite};
public yoshimiteadapter(Context context) {
this.context = context;
}
#Override
public Object instantiateItem(ViewGroup container, int position) {
layoutInflater =(LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view =layoutInflater.inflate(R.layout.custom_layout_yos, null);
ImageView imageView = (ImageView)view.findViewById(R.id.imageView);
imageView.setImageResource(images1[position]);
TextView textView = (TextView)view.findViewById(R.id.textView);
textView.setText(strings1[position]);
ViewPager vp = (ViewPager) container;
vp.addView(view,0);
return view;
}
#Override
public void destroyItem(ViewGroup container, int position, Object object) {
ViewPager vp = (ViewPager) container;
View view = (View) object;
vp.removeView(view);
}
#Override
public int getCount() {
return images1.length;
}
#Override
public boolean isViewFromObject(View view, Object object) {
return view==object;
}
}
yosemite.java
package com.example.android.myapplication;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
public class yoshemite extends AppCompatActivity {
ViewPager viewPager;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_yoshemite);
viewPager = (ViewPager)findViewById(R.id.yoshimite);
yoshimiteadapter yoshimiteadapter = new yoshimiteadapter(this);
viewPager.setAdapter(yoshimiteadapter);
}
}
ViewPagerAdapter.java
package com.example.android.myapplication;
import android.content.Context;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
/**
* Created by sanchitdeshmukh on 17/03/18.
*/
public class ViewPagerAdapter extends PagerAdapter {
private Context context;
private LayoutInflater layoutInflater;
private Integer []images ={R.drawable.napalicoast,R.drawable.haleakaaa,R.drawable.roadtohana};
private Integer []strings = {R.string.haleaka,R.string.napali,R.string.Paragraph_hawaii};
public ViewPagerAdapter(Context context) {
this.context = context;
}
#Override
public int getCount() {
return images.length;
}
#Override
public boolean isViewFromObject(View view, Object object) {
return view==object;
}
#Override
public Object instantiateItem(ViewGroup container, int position) {
layoutInflater =(LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view =layoutInflater.inflate(R.layout.custom_layout, null);
ImageView imageView = (ImageView)view.findViewById(R.id.imageView);
imageView.setImageResource(images[position]);
TextView textView = (TextView)view.findViewById(R.id.textView);
textView.setText(strings[position]);
ViewPager vp = (ViewPager) container;
vp.addView(view, 0);
return view;
}
#Override
public void destroyItem(ViewGroup container, int position, Object object) {
ViewPager vp = (ViewPager) container;
View view = (View) object;
vp.removeView(view);
}
}
I found the solution , it's related to this:
textView.setText(strings1[position]);
I have only one string, three images, which causes the error. [position] should be set to '0'; so the modification is like this:
textView.setText(strings1[0]);
This question already has answers here:
What does the "Assigned value is never used" warning mean?
(5 answers)
Closed 7 years ago.
After creating my activity, I get this warning:
Variable 'station' is never used
I not certain as to what that warning means nor how to fix it. I'm guessing the 'station' variable hasn't been declared however I don't know the correct way to declare it. All help would be appreciated.
FragmentWCLine.java
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;
public class FragmentWCLine extends android.support.v4.app.Fragment {
public final static String EXTRA_MESSAGE = "Station_key";
private class WC {
private CharSequence station;
private CharSequence zone;
private Class<? extends Activity> activityClass;
private Class<? extends android.support.v4.app.Fragment> fragmentClass;
public WC(int stationResId, int zoneResId, Class<? extends Activity> activityClass, Class<? extends android.support.v4.app.Fragment> fragmentClass) {
this.fragmentClass = fragmentClass;
this.activityClass = activityClass;
this.station = getResources().getString(stationResId);
this.zone = getResources().getString(zoneResId);
}
#Override
public String toString() { return station.toString(); }
public String getzone(){ return zone.toString(); }
}
private static WC[] mWC;
private boolean mTwoPane;
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
View v = inflater.inflate(R.layout.fragment_wc_line, container, false);
// Instantiate the list of stations.
mWC = new WC[]{
new WC(R.string.bank, R.string.zone_1, WCBankActivity.class, FragmentWCBank.class),
new WC(R.string.wat, R.string.zone_1, WCWATActivity.class, FragmentWCWAT.class)
};
final ListView listView = (ListView)v.findViewById(R.id.list_wc);
listView.setAdapter(new MyAdapter(getActivity(), mWC));
listView.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
if(mTwoPane){
setItemNormal();
View rowView = view;
setItemSelected(rowView);
}
else{
Intent intent = new Intent(getActivity(), mWC[position].activityClass);
String station = mWC[position].station.toString();
intent.putExtra(EXTRA_MESSAGE, station);
startActivity(intent);
}
}
public void setItemSelected(View view){
View rowView = view;
view.setBackgroundColor(Color.parseColor("#66CCCC"));
TextView tv0 = (TextView)rowView.findViewById(R.id.list_item_station);
tv0.setTextColor(Color.parseColor("#000099"));
TextView tv1 = (TextView)rowView.findViewById(R.id.list_item_zone);
tv1.setTextColor(Color.parseColor("#000099"));
}
public void setItemNormal()
{
for (int i=0; i< listView.getChildCount(); i++) {
View v = listView.getChildAt(i);
v.setBackgroundColor(Color.TRANSPARENT);
TextView tv0 = ((TextView) v.findViewById(R.id.list_item_station));
tv0.setTextColor(Color.WHITE);
TextView tv1 = ((TextView) v.findViewById(R.id.list_item_zone));
tv1.setTextColor(Color.parseColor("#B5B5B5"));
}
}
});
return v;
}
static class MyAdapter extends BaseAdapter {
static class ViewHolder {
TextView station;
TextView zone;
}
LayoutInflater inflater;
WC[] mWC;
public MyAdapter(Context contexts, WC[] samples) {
this.mWC = samples;
inflater = LayoutInflater.from(contexts);
}
#Override
public int getCount() {
return mWC.length;
}
#Override
public Object getItem(int position) {
return mWC[position];
}
#Override
public long getItemId(int position) {
return 0;
}
/**set selected position**/
private int selectPosition = -1;
public void setSelectPosition(int position){
if(position!=selectPosition){
selectPosition = position;
}
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder viewHolder;
if (convertView == null) {
convertView = inflater.inflate(R.layout.list_item_dualline, null);
viewHolder = new ViewHolder();
viewHolder.station = (TextView) convertView.findViewById(R.id.list_item_station);
viewHolder.zone = (TextView) convertView.findViewById(R.id.list_item_zone);
convertView.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) convertView.getTag();
}
viewHolder.station.setText(mWC[position].station);
viewHolder.zone.setText(mWC[position].getzone());
//change item color
if(position==selectPosition){
convertView.setBackgroundColor(Color.parseColor("#000099"));
viewHolder.station.setTextColor(Color.parseColor("#000099"));
}else {
}
return convertView;
}
}
}
WCBankActivity.java
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.ActionBarActivity;
public class WCBankActivity extends ActionBarActivity {
public final static String EXTRA_MESSAGE = "Station_key";
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fragment_wc_bank);
if (savedInstanceState == null) {
// Get the message from the intent
Intent intent = getIntent();
// Notice to specify the sender Activity for the message
String station = intent.getStringExtra(WCBankActivity.EXTRA_MESSAGE);
FragmentWCBank newFragment = new FragmentWCBank();
FragmentTransaction transaction = this.getSupportFragmentManager().beginTransaction();
transaction.replace(R.id.detail_container, newFragment);
transaction.commit();
}
}
}
FragmentWCBank.java
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class FragmentWCBank extends android.support.v4.app.Fragment {
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_wc_bank, container, false);
return v;
}
}
What you are seeing is a Warning rather than an Error. The difference being a Warning will not prevent your program from running, but an Error would.
The warning you are seeing is due to the fact that you are declaring a variable station but then never using it. Your IDE is smart enough to notice this and is trying to warn you as a reminder that you aren't using the variable that you created.
In this case it's this line:
String station = intent.getStringExtra(WCBankActivity.EXTRA_MESSAGE);
You declare a variable station and give it a value but then you never read that value or do anything else with it. Therefore you could theoretically remove the variable with no other impact to your app.
I'm very new to fragments and I'm attempting to load a videoView into a fragment . I have my layout files laid out correctly (as I can swipe among images just fine), but I'm unsure of how to load a videoView into a fragment. My code for my activities is as follows:
package com.example.PLS;
import android.content.Context;
import android.net.Uri;
import android.os.Bundle;
import android.os.Parcelable;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.VideoView;
public class CustomPagerAdapter extends PagerAdapter
{
public Object instantiateItem(View collection, int position) {
LayoutInflater inflater = (LayoutInflater) collection.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
int resId = 0;
switch (position) {
case 0: {
resId = R.layout.page1;
break;
}
case 1: {
resId = R.layout.page2;
break;
}
case 2: {
resId = R.layout.page3;
break;
}
}
View view = inflater.inflate(resId, null);
((ViewPager) collection).addView(view, 0);
return view;
}
#Override
public void destroyItem(View arg0, int arg1, Object arg2) {
((ViewPager) arg0).removeView((View) arg2);
}
#Override
public boolean isViewFromObject(View arg0, Object arg1) {
return arg0 == ((View) arg1);
}
#Override
public Parcelable saveState() {
return null;
}
#Override
public int getCount() {
return 3;
}
}
package com.example.PLS;
import android.app.Fragment;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;
import android.view.Menu;
import android.widget.VideoView;
public class MyActivity extends FragmentActivity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// Create and set adapter
CustomPagerAdapter adapter = new CustomPagerAdapter();
ViewPager myPager = (ViewPager) findViewById(R.id.customviewpager);
myPager.setAdapter(adapter);
myPager.setCurrentItem(0);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
I'm not sure where or how to place the code that tells the videoView to play a video in page1 for example. Where would I place that and how would it look?
A bit offtopic:
Are you sure, that you are using Fragments and not just Views? As far as I can see, you add and remove Views.
Or do you do something within the layout files? If so, could you provide the xml layout resources?
See can i use view pager with views (not with fragments)
For your question:
After you call addView, you could do something like to following (or add that to a button listener):
String videoUri = getVideoUriForPosition(position);
// TODO check if videoUri is not null or something
mPlayerView = (VideoView) mView.findViewById(R.id.video_player);
mPlayerView.requestFocus();
if(mPlayerView == null) {
Log.e(TAG, "Video view null");
} else {
mPlayerView.setVideoURI(Uri.parse(videoUri));
mPlayerView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
public void onPrepared(MediaPlayer mp) {
mPlayerView.start();
});
}