I'm having the problem that for the following activity
public class MainActivity extends FragmentActivity implements UpdateActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.tabbed_main_activity); <-- error here
I get the follwoing error: Cannot resolve method setContentView(int)
What am I doing wrong?
PS: I also cross checked with http://developer.android.com/training/basics/fragments/fragment-ui.html
Related
I try to flash an image view for 3 seconds and then return it to its original image. here is my code:
public class MainActivity extends AppCompatActivity
implements View.OnClickListener{
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
findViewById(R.id.imageView).setOnClickListener(this);
}
#Override
public void onClick(View v) {
ImageView pic = findViewById(R.id.imageView);
pic.setImageDrawable(getResources().getDrawable(R.drawable.devil));
//Wait Block
try
{
Thread.sleep(3000);
} catch (InterruptedException e){
Toast.makeText(this,"Problem occurred",Toast.LENGTH_LONG).show();
}
//End of wait block
pic.setImageResource(R.drawable.angel);
Toast.makeText(this,"Done",Toast.LENGTH_LONG).show();
}
So basically I want this code flow:
While the actual flow is the following :
Could you tell me the reason for this behavior as well as how to solve this?
i am a new comer in android development and facing some problems using firebase ui.
i wanted to retrieve data from my firebase database and show it on my recyclerView.
but i am constantly getting an error while making FirebaseRecyclerAdapter.
Here is My Code Sample:
public class UsersActivity extends AppCompatActivity {
private Toolbar userToolbar;
private RecyclerView usersList;
private DatabaseReference mUsersDatabase;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_users);
userToolbar = (Toolbar) findViewById(R.id.users_toolbar);
setSupportActionBar(userToolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setTitle("Users");
mUsersDatabase = FirebaseDatabase.getInstance().getReference().child("Users");
usersList = (RecyclerView) findViewById(R.id.users_list);
usersList.setHasFixedSize(true);
usersList.setLayoutManager(new LinearLayoutManager(this));
}
#Override
protected void onStart() {
super.onStart();
FirebaseRecyclerAdapter<Users, UsersViewHolder> firebaseRecyclerAdapter = new FirebaseRecyclerAdapter<Users, UsersViewHolder>(
Users.class,
R.layout.single_user_list_layout,
UsersViewHolder.class,
mUsersDatabase
) {
#Override
protected void onBindViewHolder(#NonNull UsersViewHolder holder, int position, #NonNull Users model) {
holder.setName(model.getName());
}
#NonNull
#Override
public UsersViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
return null;
}
};
usersList.setAdapter(firebaseRecyclerAdapter);
}
public static class UsersViewHolder extends RecyclerView.ViewHolder
{
View mView;
public UsersViewHolder(#NonNull View itemView) {
super(itemView);
mView = itemView;
}
public void setName(String name)
{
TextView userNameView = (TextView) mView.findViewById(R.id.single_user_name);
userNameView.setText(name);
}
}
}
The Error i'm getting is this:
please help!!
Sorry wanted to comment but i ain't got enough points to do so.
Try this first, if it doesnt work try using FirebaseRecyclerOptions
(The latest version (3.x) of FirebaseUI implements a different method ofinitializing a FirebaseRecyclerAdapter than previous versions.)
Make your " firebaseRecyclerAdapter " variable global, so you would have something like this.
firebaseRecyclerAdapter = new FirebaseRecyclerAdapter<Users, UsersViewHolder>(
Users.class,
R.layout.single_user_list_layout,
UsersViewHolder.class,
mUsersDatabase
)
My question is simple how to send data from Main activity to fragment through interface but not through intent or bundle in android.
Please guide me thanks.
Here is my code from fragment to Activity data send through interface but I want against it Activity to fragment which I don't know how to do.
public interface OnRestaurantSelectedListener {
public void onRestaurantSelected(String s);
}
MainActivity extends AppCompatActivity implements OnRestaurantSelectedListener{
String mPosition;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
#Override
public void onRestaurantSelected(String s) {
mPosition =s;
}
}
In fragment:
public class ListFragment extends Fragment {
...
private OnRestaurantSelectedListener mOnRestaurantSelectedListener;
#Override
public void onAttach(Context context) {
super.onAttach(context);
try {
mOnRestaurantSelectedListener = (OnRestaurantSelectedListener) context;
} catch (ClassCastException e) {
throw new ClassCastException(context.toString() + e.getMessage());
}
}
mOnRestaurantSelectedListener.onRestaurantSelected("AliResturant");
}
Create interface FragmentCommunicator as,
public interface FragmentCommunicator{
public void passDataToFragment(String someValue);
}
In your main Activity we can use interface and pass data to fragment. Like below,
public class MainActivity extends FragmentActivity{
//interface through which communication is made to fragment
public FragmentCommunicator fragmentCommunicator;
private Button fragmentButton;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.title_viewpager);
fragmentButton = (Button)findViewById(R.id.fragmentButton);
setUIListeners();
}
private void setUIListeners(){
fragmentButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
if(fragmentCommunicator != null)
fragmentCommunicator.passDataToFragment("Hi from FragmentActivity");
}
});
}
}
Access passed data in fragment like below,
public class CustomFragment extends Fragments implements FragmentCommunicator{
public Context context;
private Button activityButton;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
View fragmentView = inflater.inflate(R.layout.activity_frag, container, false);
activityButton =(Button)fragmentView.findViewById(R.id.activityButton);
textView =(TextView)fragmentView.findViewById(R.id.textView);
setRetainInstance(true);
return fragmentView;
}
//FragmentCommunicator interface implementation
#Override
public void passDataToFragment(String someValue){
String activityAssignedValue = someValue;
textView.setText(activityAssignedValue);
}
}
I tried but i am getting errors "Onclick listener can't be applied here" Please help me guys
Here is my Fragment.java
public class Fragment extends Fragment {
public Fragment() {
// Required empty public constructor
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_layout, container, false);
final Button button = (Button) rootView.findViewById(R.id.payment_info);
final Button button1 = (Button) rootView.findViewById(R.id.redeem);
final Button button2 = (Button) rootView.findViewById(R.id.make_payment);
final Button button3 = (Button) rootView.findViewById(R.id.payment_histories);
// Inflate the layout for this fragment
return rootView;
}
#Override
public void onAttach(Context context) {
super.onAttach(context);
}
#Override
public void onDetach() {
super.onDetach();
}
}
OnClickListener is an interface, so you have not to only extend the fragment but to implement the OnClick.
Try this on your declaration of the class:
public class Fragment extends Fragment implements OnClickListener
Hope it helps.
I am a novice programmer on android studio, I want to know why does the
delay function does an error on handler.postDelayed (new Runnable(){ do I have to import any library? and what is the shortcut key for :#override..." whenever I does a code. thanks.
heres the code:
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Handler handler = new Handler();
handler.postDelayed (new Runnable(){
#.
public void run() {
msg();
}
},500);
}
sry i write it as an answer cause i cant post comments yet but can you post the exact error?
i'm pretty sure you should write
public class MainActivity extends AppCompatActivity {
like this :
public class MainActivity extends AppCompatActivity implements runnable {
but thats just an educated guess since I dont know whats the exact error you are getting.