How to get views from fragments in onClick - android

I have an activity with a String array and a Drawable array. I create fragments using a for loop in the onCreate according to the length of the arrays and use the String array to fill TextViews in the fragments.
I made the fragments clickable and added onClick in the fragment class.
I want to get the text from the fragment the user clicks, but when I click it only gets the text from the first fragment. How can I read the textviews in the different fragments.
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
int layoutId = getArguments().getInt("layout");
View rootView = inflater.inflate(layoutId, container, false);
TextView fragmentText = (TextView) rootView.findViewById(R.id.fragTextView);
fragmentText.setText(getArguments().getString("fragText"));
ImageView fragmentImage = (ImageView) rootView.findViewById(R.id.fragImageView);
fragmentImage.setImageResource(getArguments().getInt("pic"));
rootView.setOnClickListener(this);
return rootView;
}
#Override
public void onClick(View v) {
String s = ((TextView)v.getRootView().findViewById(R.id.fragTextView)).getText().toString();
Intent intent = new Intent(v.getContext(), LevelActivity.class);
intent.putExtra("exerciseId", s);
startActivity(intent);
}
Thanks

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

Can't set text in fragments TextView

I have strange problem with setText() function. I want to display listview item details when i click on specific item. I want to display this details in 3 tabs so I use fragments. This is my code from one of them.
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
Intent intent = getActivity().getIntent();
int position = intent.getExtras().getInt("Position");
View rootView = inflater.inflate(R.layout.album_tab1, container, false);
Log.e("Position", String.valueOf(position));
Log.e("Value: ", ParseJSONDetail.overview[position]);
final TextView title = (TextView) rootView.findViewById(R.id.album_title);
final TextView overview = (TextView) rootView.findViewById(R.id.album_overview);
final TextView band = (TextView) rootView.findViewById(R.id.album_band);
final ImageView okladka = (ImageView) rootView.findViewById(R.id.album_okladka);
overview.setText(ParseJSONDetail.overview[position]);
imgload.getInstance().displayImage(ParseJSONDetail.image[position], okladka);
band.setText(ParseJSONDetail.band[position]);
title.setText(ParseJSONDetail.title[position]);
return inflater.inflate(R.layout.album_tab1, container, false);
}
Strange is that in Log i can display this data what i want.
Any ideas what's wrong with this code?
replace
return inflater.inflate(R.layout.album_tab1, container, false);
with
return rootView;

how can i call fragment from listadapter

Hi i am calling fragment from my custom adapter but its displaying error can any one help me here is the MyListAdapter snippet
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
final TextView view = new TextView(parent.getContext());
view.setText(values.get(position));
view.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view1) {
// Get the position
Log.w(MyListAdapter.LOG_KEY, "MyListAdapter Lable Clicked");
Intent intent= new Intent(context, SingleItemView.class);
context.startActivity(intent);
}
});
return view;
}
and my SingleItemView.class
public class SingleItemView extends Fragment{
LinearLayout linearLayout;
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
linearLayout = (LinearLayout) inflater.inflate(R.layout.listview_item, container, false);
setHasOptionsMenu(true);
return linearLayout;
}
}
and here is the error message
enter image description here
You can not start Fragment by Intent. Fragment have to be hosted by any Activity. You have to navigate to this Activity.
If you want to start special Fragment in those Activity (for example any Fragment in ViewPager) put any flag in your Intent. By this flag in your Activity you can determine which Fragment should be started.
It is because you can't call Fragments via Intent, Fragment is a part of an FragmentActivity
All in all Fragment is a content not container, so you need to create a FragmentActivity and add Fragment(Favourite) in that, and then call
Intent intent1 = new Intent(context, SomeFragmentActivity.class);
startActivity(intent1);
A Fragment is a piece of an application's user interface or behavior that can be placed in an Activity.
http://developer.android.com/reference/android/app/Fragment.html

How to call and pass parameters to a fragment from a button click event on the listview group

I have an expandable list view with a button and a textview in the group header.
I uses an adapter that implements the view holder.
I want to include a click event on the button such that I get the textview text that is in the group header and I want to pass the string to another fragment.
I also want to show the other fragment.
I am stuck as I can`t figure out,
How to read the textview value from the expandable listview in the fragment on the button click
and
How to pass the value of Title to another fragment.
Do I need to use
GViewHolder.myButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
}
or
public void onItemClick(AdapterView<?> parent, View view,int position, long id) {
});
Please note that I can`t see public void onItemClick being hit when the button is clicked!
I have implemented an adapter as follows:
Part of the code is
public class ListViewAdapter extends BaseExpandableListAdapter {
class GViewHolder {
public TextView Title;
public Button myButton;
}
GViewHolder.myButton.setFocusable(false);
GViewHolder.myButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
// How to implement this in the fragment ???
}
});
}
Part of the fragment that implements the Adapter as follows:
private ListView eList = null;
InterFragmentNavigator interFragmentNavigator;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_event_list, container, false);
eList = (ListViewAdapter) view.findViewById(R.id.eView);
ListViewAdapter listDataAdapter = new ListViewAdapter(getActivity(), groupes);
eList.setAdapter(listDataAdapter);
eList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view,int position, long id) {
// Here, I want to read the textview value of Title next to button
TextView textViewItem = ((TextView) view.findViewById(R.id.Title));
String Title = textViewItem.getText().toString();
**//Here, I want to call another fragment and pass Title as parameter.**
return view;
}
lets say you are creating new fragment from the click then add something like.
Fragment detailFragment = new Fragment();
Bundle bundle = new Bundle();
bundle.putString("TITLE", "your title");
detailFragment.setArguments(bundle);
Then do the transection here. Then in the detailFragment you can do
Bundle bundle = getArguments();
String title = bundle.getString("TITLE") ;

How to pass data from Fragment to Activity

I want to pass Bitmap from fragment to the MainActivity. I want to save the bitmap i.e in "image = a.getParcelable("BITMAP"); " on ActionBar button click. For that i need this image in my MainActivity. How can i do that?
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// TODO Inflate the layout for this fragment
setHasOptionsMenu(true);
Bundle a = getArguments();
image = a.getParcelable("BITMAP");
View v = inflater.inflate(R.layout.main_layout, container, false);
qImage = (ImageView) v.findViewById(R.id.qImage);
qImage.setImageBitmap(image);
return v;
}
In MainActivity
Bitmap a;
In Fragment
MainAcivity main;
main = (MainActivity)getActivity();
main.a = image
or call a method on the MainActivity to update Bitmap

Categories

Resources