android setting textview from another class with Fragments - android

I'm trying to set a textview text from another class
the textview is in the main activity and i need to set it from another class thats not an activity
In the past I have used the following
public class DemoActivity extends Activity {
public static TextView textViewObj1;
public static TextView textViewObj2;
public static TextView textViewObj3;
public static TextView textViewObj4;
public static TextView textViewObj5;
public void onCreate(final Bundle savedInstanceState) {
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
textViewObj1 = (TextView) findViewById(R.id.Sitename);
textViewObj2 = (TextView) findViewById(R.id.Building);
textViewObj3 = (TextView) findViewById(R.id.Floor);
textViewObj4 = (TextView) findViewById(R.id.Roomno);
textViewObj5 = (TextView) findViewById(R.id.Drawref);
I then set it in the other class by using
DemoActivity.textViewObj1.setText(c.getString(1));
DemoActivity.textViewObj2.setText(c.getString(2));
DemoActivity.textViewObj3.setText(c.getString(3));
DemoActivity.textViewObj4.setText(c.getString(4));
DemoActivity.textViewObj5.setText(c.getString(5));
The problem I have is the app im trying to set it for uses fragments and so the main activity instead of starting like this
public class DemoActivity extends Activity {
Starts like this
public class DemoActivity extends FragmentActivity {
for some reason when I try to set the textview from the other class using
DemoActivity.textViewObj5.setText(c.getString(5));
It gives the error : DemoActivity cannot be resolved
Any Ideas?
Your help is as always appreciated
Mark

Best practice is:
1) Define a listener interface in your fragment
public interface FragmentInteractionListener{
void onFragmentInteraction(String textToPut);
}
2) Implement this interface in your parentActivity:
public class MyParentActivity extends Activity implements FragmentInteractionListener {
...
#Override
public void onFragmentInteraction(String textToPut) {
//Update the textView here
}
3) In your fragment, call the parent Activity method:
((FragmentInteractionListener )getActivity()).onFragmentInteraction("This is the new text");
You may want to check if the parent activity is implementing the required interface in fragment's onAttach() method

Try Using DemoActivity.getActivity() or this.getActivity(). I know that when using an activity, we just call the context, but using fragments involves using getActivity.Hope this helps

Related

Android upper limit for no of OnclickListeners on an Activity

I am making a keyboard app. For what I need to know if I can assign seperate onclicklistener to each button, ie around 70 onclicklisteners on a single activity.
Is it a right way to do it??
Implement View.OnClickListener in your activity.Give different id for the views.
public class KeyBoardActivity extends Activity implements View.OnClickListener{
#Override
protected void onCreate(Bundle savedInstanceState) {
}
#Override
public void onClick(View v) {
//Write your code for the corresponding view ids
}
}

How to use a FragmentDialog in a class that extends RecyclerView.ViewHolder?

I am trying to display a Fragment Dialog when a user clicks on an item on my recycler view.
The bit I am stuck on is how to use a FragmentDialog in a class that extends RecyclerView.ViewHolder?
This is what I have so far:
public class FinalHolder extends RecyclerView.ViewHolder{
public FinalHolder(View view){
super(view);
TextView username= (TextView)view.findViewById(R.id.username);
TextView address = (TextView)view.findViewById(R.id.address);
view.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
ViewFragmentDialog viewFragmentDialog = new ViewFragmentDialog();
viewFragmentDialog.show() // THIS IS WHERE I AM HAVING TROUBLE. THE `show` METHOD EXPECTS A FRAGMENTACTIVITY
}
});
}
}
The viewFragmentDialog.show() method is where I am having some difficulty - because it is expecting Fragment Activity but I am using RecyclerView.ViewHolder
I won't use a DialogFragment in this case, if you want explicitly call it from RecyclerView.ViewHolder. The main reason is that you need access to the FragmentManager to show() your DialogFragment's subclass, and you don't. I see two possible solution. The easiest one is to use a normal AlertDialog. You need a context to access it that you can easily retrieve with itemView.getContext(), or use a Delegate (aka Listener) to communicate with the Activity/Fragment, and show the DialogFragment from there

Various activities use same views from one layout - how to refactor?

I have six activities like this. I tried to make custom activity that hold ImageViews so I won't have to repeat myself in every activity. Should I leave it as it is or can I make it somehow be in one place and let it be used by everyone (like layout is - it's just one and works):
public class ActivityOne extends AppCompatActivity implements View.OnClickListener {
#Bind(R.id.iv1) ImageView iv1;
#Bind(R.id.iv2) ImageView iv2;
#Bind(R.id.iv3) ImageView iv3;
#Bind(R.id.iv4) ImageView iv4;
#Bind(R.id.iv5) ImageView iv5;
#Bind(R.id.iv6) ImageView iv6;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ButterKnife.bind(this);
iv1.setImageDrawable(getResources().getDrawable(R.drawable.c1));
iv2.setImageDrawable(getResources().getDrawable(R.drawable.c2));
iv3.setImageDrawable(getResources().getDrawable(R.drawable.c3));
iv4.setImageDrawable(getResources().getDrawable(R.drawable.c4));
iv5.setImageDrawable(getResources().getDrawable(R.drawable.c5));
iv6.setImageDrawable(getResources().getDrawable(R.drawable.c6));
}
You can create an abstract activity ImageryActivty that needs to override some method like getContentView which provides a layout id:
public abstract class ImageryActivity extends AppCompatActivity {
#Bind(R.id.iv1) ImageView iv1;
#Bind(R.id.iv2) ImageView iv2;
#Bind(R.id.iv3) ImageView iv3;
#Bind(R.id.iv4) ImageView iv4;
#Bind(R.id.iv5) ImageView iv5;
#Bind(R.id.iv6) ImageView iv6;
public abstract int getContentView();
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(getContentView());
ButterKnife.bind(this);
iv1.setImageDrawable(getResources().getDrawable(R.drawable.c1));
iv2.setImageDrawable(getResources().getDrawable(R.drawable.c2));
iv3.setImageDrawable(getResources().getDrawable(R.drawable.c3));
iv4.setImageDrawable(getResources().getDrawable(R.drawable.c4));
iv5.setImageDrawable(getResources().getDrawable(R.drawable.c5));
iv6.setImageDrawable(getResources().getDrawable(R.drawable.c6));
}
}
And your child activities must inherit from this one:
public class ActivityOne extends ImageryActivity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
public int getContentView() {
return R.layout.activity_one;
}
}
Of course this layout must contain all the ImageView's with the proper id's. For this I´d recommend you to create a reusable layout imagery_layout and include it in each of your child activities:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<include
layout="#layout/imagery_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<!-- And here it comes the content for this particular activity in case there's one -->
</LinearLayout>
You can create one abstract BaseActivity activity that will have all the functionality common to those activities and then you can just extend the other activities with it that needs to have that common functionality
or you can simply use one activity and maintained all the states in it using some sort of switch statements all depends on your requirement
You should be able to do this with one Activity. You can pass arguments to Activities as Intent extras. Define some String constants in the Activity:
public static final String ARG_IN_IMAGE_ONE = "ActivityOne.ImageOne";
public static final String ARG_IN_IMAGE_TWO = "ActivityOne.ImageTwo";
...
Set your Drawable ids on creating the Intent:
intent.putIntExtra(ARG_IN_IMAGE_ONE, R.drawable.c1);
...
And read from the intent in onCreate:
iv1.setImageDrawable(getResources().getDrawable(getIntent().getIntExtra(ARG_IN_IMAGE_ONE)));
...
You can also optionally make a static builder which takes the 6 image ids and returns the Intent.

Static Application Contex on android

I am Trying to inflate one ListView When clicking a member of another ListView Which is in a ListActivity. So when I try to set Adapter it asks me for getApplicationContext, so the ListView is in another activity and I am trying to call that that applicationContext from another the ListActivity which will inflate the ListView if you click it.
I am Doing this and I dont Know If I am right or wrong
public class FirstList extends ActionBarActivity{
public static ListView firstL;
public static ArrayList<Friend> friendSelected= new ArrayList<>();
public static Context context;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.conversation_list_layout);
context = getApplicationContext();
firstL= (ListView)findViewById(R.id.listView1);
}
}
Second Class
private class FriendSelectorAdapter extends ArrayAdapter<Friend> {
public ChatListAdapter(){
super(FirstList.context,R.layout.list_layout, FirstList.friendSelected);
}
In the second class there is a ListActivity which is displaying all my contacts and this adapter is meant to be called when one of the contacts is clicked so it will populate the listView of the first class.
Am I wrong what i am doing here? I would appreciate any answer, Thanks
Change constructor to some think like this(without any static in activity):
public ChatListAdapter(Context context, List<Friend> friendSelected){
super(context,R.layout.list_layout,friendSelected);
}

Issuue while implementing onClickListener

See this simple code,But i am getting Description
The method onClick(View) of type CheckboxActivity must override a superclass method CheckboxActivity.java on my onClick(View v) method.
public class CheckboxActivity extends Activity implements Button.OnClickListener{
/** Called when the activity is first created. */
Button b1;
CheckBox c1, c2;
EditText et1;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
et1 = (EditText) this.findViewById(R.id.text1);
c1 = (CheckBox) findViewById(R.id.check1);
c2 = (CheckBox) findViewById(R.id.check2);
b1 = (Button) findViewById(R.id.button1);
b1.setOnClickListener(this);
}
#Override
public void onClick(View v){
et1.setText("");
if (c1.isChecked())
et1.setText("Android ");
if (c2.isChecked())
et1.setText(et1.getText()+"iPhone ");
}
}
Anyone please help
.
Try:
public class CheckboxActivity extends Activity implements OnClickListener
You delete your onClick() first.
Then change ur first line as
public class CheckboxActivity extends Activity implements OnClickListener
and the import should be
android.view.View.OnClickListener;
Then after your onCreate() do a right click -> Source -> Override/Implemented Methods
the onClick() will be automatically selected in the dialog box. Click ok and type in ur required things into the method.
I don't have sources of Activity and don't have working environment to write code and run javac, so I'm just guessing, please check yourself:
Activity has onClick method already - make anonymous OnClickListener
instance and pass it to the button (or remove inheritance from
Button.OnClickListener and don't forget to call super when view is
not your button). Could be also that contract has different access
level in activity.
Level of language is 1.5 and you can't use annotation #override for interfaces.

Categories

Resources