Add an item to ListView (using Tabs) - android

I'm trying to add an item to a ListView which is in one of my 3 tabs, the object will be created on another activity and after creating I want to return to the Tab and there's already shown the object in the ListView, I wanted to do FindViewById in the java class of the Tab but this doesn't work, does anyone know how i could do this?
Code:
Create Idea Activity
public class CreateIdeaActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_create_idea);
final EditText title = (EditText) findViewById(R.id.ideaTitle);
final EditText text = (EditText) findViewById(R.id.ideaText);
final Button create = (Button) findViewById(R.id.btn_create);
create.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View view)
{
Idea idea = new Idea(title.getText().toString(),text.getText().toString(),"");
Manager.AddObjectToIdeaList(idea); // Manager Class: List.add(idea)
}
});
}
One of my tabs (xml contains ListView) where I want to add items from the Manager-List to the ListView (findViewById does not work):
public class ideastab_main extends Fragment{
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.activity_ideastab_main, container, false);
return rootView;
}
}

Related

Android - Getting Objects in Multiple RootView

public class OccuMechanical extends android.support.v4.app.Fragment {
private View v_schedule;
private LinearLayout layout_schedule;
private ImageButton iv_add_schedule;
private int i = 0;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_occu_mechanical, container, false);
layout_schedule = (LinearLayout) v.findViewById(R.id.layout_mech_schedule);
iv_add_schedule = (ImageButton) v.findViewById(R.id.iv_add_schedule);
iv_add_schedule.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
i++;
v_schedule = LayoutInflater.from(getActivity()).inflate(R.layout.layout_mech_schedule, null);
layout_schedule.addView(v_schedule);
EditText et = (EditText) layout_schedule.getRootView().findViewById(R.id.layout_description);
et.setText("Test: " + String.valueOf(i));
}
});
return v;
}
}
idk if my term is right in the title but here's what im doing...
when I click the 'Add more equipment' it adds layout to my app so
I want to get all the EditText inside the layout i added in the RootView,
I tried setting the text to test it but the first editText of the first rootView is the only one updating. All the codes in my fragment listed above. attached image is the result of this code.
image result
I've done this task by using *RecyclerView as #hjchin suggested. Thank You!
RecyclerView Tutorial # Developer.Android

findViewByID not work in tab layout

i use tab layout matching this tutorial:
tab layout tutorial androidhive
It works fine, but when I add a button to fragment_one.xml , i cant use setOnClickListener for this button Because findViewById not work in MainActivity.java
the MainActivity bellow code:
public class MainActivity extends AppCompatActivity {
Button button;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ViewPager viewPager = (ViewPager) findViewById(R.id.viewpager);
setupViewPager(viewPager);
TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs);
tabLayout.setupWithViewPager(viewPager);
button = (Button) findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action",Snackbar.LENGTH_LONG).setAction("Action", null).show();
}
});
}
the app stopped with this error:
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener' on a null object reference
Where should the button be defined and setOnClickListener?
Even though a fragment is hosted by an activity, it has its own layout (which is contained in the activity's layout) and therefore you'll have to define the fragment's view in its java class.
In most cases, a fragment would have its own Java class for you to manage and manipulate it, so in your case, it should look like this:
public class OneFragment extends Fragment{
public OneFragment() {
// Required empty public constructor
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view=inflater.inflate(R.layout.fragment_one, container, false);
Button button=view.findViewById(R.id.myButtonInFragment);
return view;
}
}
If you'd like to access this button from its parent activity, you can achieve it by making the button public:
public class OneFragment extends Fragment{
public Button button;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view=inflater.inflate(R.layout.fragment_one, container, false);
button=view.findViewById(R.id.myButtonInFragment);
return view;
}
}
and then, access it using the fragment's instance in your activity:
OneFragment myFragment=new OneFragment();
After you have attached the fragment to the activity, you can use:
myFragment.button.setClickEnabled(false);
However, accessing fragment's children outside of the fragment is not recommended, and you should avoid it if you can.
public class MainActivity extends AppCompatActivity {
Button button;
Fragment fragment;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
fragment = new Fragment();
View view = fragment.getView();
if (view !=null) {
view.findViewById(R.id.button);
}
}
}

ImageButton on Fragment which goes to another Activity. Android Studio

I have been searching this but I haven't found anything that could help me.
I have a main activity with 2 fragments which I use as tabs in my toolbar. Is there any possibility of connecting an ImageButton from a fragment in my MainActivity to an other Activity. I know how to connect Activity to Activity through an imagebuttom, i just don't know how to do it from Fragment-> Activity. Thanks.
I have an image button on my fragment, and I want to open an activity when I press that ImageButton.
public class Movies extends Fragment {
public Movies() {
// Required empty public constructor
}
ImageButton imageButton2;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_movies, container, false);
imageButton2 = (ImageButton) findViewById(R.id.imageButton2);
imageButton2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intentLoadNewActivity = new Intent(Movies.this, Activity_Civil_War.class);
startActivity(intentLoadNewActivity);
}
});
}
}
I am getting lots of errors. I also tried doing it in the MainActivity but I get the null object exception.
MainActivity Class:
public class MainActivity extends AppCompatActivity {
Toolbar toolbar;
TabLayout tabLayout;
ViewPager viewPager;
view_pager_adapter viewPagerAdapter;
ImageButton imageButton2;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
toolbar=(Toolbar)findViewById((R.id.toolBar));
tabLayout=(TabLayout)findViewById((R.id.tabLayout));
viewPager=(ViewPager)findViewById((R.id.ViewPager));
viewPagerAdapter = new view_pager_adapter(getSupportFragmentManager());
viewPagerAdapter.addFragments(new Showcase(),"Showcase");
viewPagerAdapter.addFragments(new Movies(),"Movie List");
viewPagerAdapter.addFragments(new Menu(),"Menu");
viewPagerAdapter.addFragments(new Login(),"Login");
viewPager.setAdapter(viewPagerAdapter);
tabLayout.setupWithViewPager(viewPager);
}
}
Few things first :
return inflater.inflate(R.layout.fragment_movies, container, false);
If your return a value here, the rest of the code below it will not be called. What you need to do is keep a reference to it and return it at the end of your method.
Then
Intent intentLoadNewActivity = new Intent(Movies.this, Activity_Civil_War.class);
I assume that Movies is a Fragment and Activity_Civil_War is an Activity (correct me if I'm wrong).
There is no constructor in the Intent class that takes a Fragment as a parameter. What you are trying to do by calling 'this' is to get a Context. In a Fragment you can do so by calling getActivity()
If we sum things up we get :
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_movies, container, false);
imageButton2 = (ImageButton) findViewById(R.id.imageButton2);
imageButton2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intentLoadNewActivity = new Intent(getActivity(), Activity_Civil_War.class);
startActivity(intentLoadNewActivity);
}
});
return view;
}

Why setText won't output to string inside fragment?

After I c/p code to fragment, my function won't set the string inside the app. But it worked at first without fragment, and I can't find the problem. App runs normally, but it won't output setText string on screen, while string is visible with it default string
public class MainFragment extends FragmentActivity{
EditText editNumber;
Button calculate;
TextView result;
int two = 2;
int seven = 7;
int inputNumber;
#Override
protected void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//find elements
editNumber = (EditText) findViewById(R.id.editNumber);
calculate = (Button) findViewById(R.id.calculateButton);
result = (TextView) findViewById(R.id.resultText);
//listener
editNumber.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
inputNumber = Integer.parseInt(editNumber.getText().toString());
}
});//onClick
calculate.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
result.setText("Output: " + (inputNumber / two) + seven);
}
});//onclick
}//onCreate
}//main
[code]
In FragmentActivity you should define a Fragment object, insert it into the Activity's frame and display things in it. Please refer to:
http://developer.android.com/guide/components/fragments.html
Are you sure your MainFragment is a fragment? It extends activity: FragmentActivity. If it is just a typo issue then you have to provide your layout in the onCreateView() method of the fragment. Something like this:
#Override
public View onCreateView(final LayoutInflater inflater, final ViewGroup container, final Bundle savedInstanceState) {
final View v = inflater.inflate(R.layout.activity_main, container, false);
result = (TextView) v.findViewById(R.id.resultText);
return v;
}
Finding a view in a fragment in other method than onCreateView() you can call:
getView().findViewById(R.id.resultText);

Null Pointer Exception when setting new text to text view

I am trying to run an AndroidApp on my phone. I am able to show my welcome fragment, moreover I can trigger the log-message. Unfortunately I am getting a null pointer exception if I want to change the text value from 'welcome' to 'Neuer Text'. What went wrong? I am quite new to android development.
public class WelcomeFragment extends Fragment implements OnClickListener {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_welcome, container, false);
Button button1 = (Button) view.findViewById(R.id.button1);
button1.setOnClickListener((OnClickListener) this);
return view;
}
#Override
public void onClick(View v) {
//Log.d("JobaApp", "Logtext"); // see LogCat
TextView text1 = (TextView) v.findViewById(R.id.welcome_text);
text1.setText("NeuerText");
}
}
In the onClick(), v is the view item that was clicked, e.g. the button, not the view that is inflated in onCreateView().
You should use getView():
public class WelcomeFragment extends Fragment implements OnClickListener {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_welcome, container, false);
Button button1 = (Button) view.findViewById(R.id.button1);
button1.setOnClickListener((OnClickListener) this);
return view;
}
#Override
public void onClick(View v) {
//Log.d("JobaApp", "Logtext"); // see LogCat
switch (v.getId) {
case R.id.button1:
TextView text1 = (TextView) getView().findViewById(R.id.welcome_text);
text1.setText("NeuerText");
break;
}
}
}
Also, if you don't want that action taking place for every button you may want to consider using a switch statement in your onClick().
You cant get the TextView from the button's onClick passed parameter ("View v"), since this is the actual button's view.
You should do something like:
public class WelcomeFragment extends Fragment implements OnClickListener {
View view;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
view = inflater.inflate(R.layout.fragment_welcome, container, false);
Button button1 = (Button) view.findViewById(R.id.button1);
button1.setOnClickListener((OnClickListener) this);
return view;
}
#Override
public void onClick(View v) {
//Log.d("JobaApp", "Logtext"); // see LogCat
TextView text1 = (TextView) view.findViewById(R.id.welcome_text);
text1.setText("NewerText"); //Also fixed typo
}
}
In OnClick(View v) , v is the button that you are clicking on. The TextView does not belong to Button, it belongs to R.layout.fragment_welcome. So you find and initialize the TextView inside onCreateView() of the fragment, and then use it inside onClick();
Some what like this:
public class WelcomeFragment extends Fragment implements OnClickListener {
TextView tv;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_welcome, container, false);
Button button1 = (Button) view.findViewById(R.id.button1);
button1.setOnClickListener((OnClickListener) this);
tv = (TextView) v.findViewById(R.id.welcome_text);
return view;
}
#Override
public void onClick(View v) {
//Log.d("JobaApp", "Logtext"); // see LogCat
tv.setText("NeuerText");
}
}

Categories

Resources