Can't set text in fragments TextView - android

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;

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

How to get views from fragments in onClick

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

EditText inside a ListView on a onCreateView

I have a List view with two EditText in and want to modify the problem is that I'm working it from a fregment and I only onCreateView to work
could help me?
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
rootView = inflater.inflate(R.layout.calcu, container, false);
c = getActivity().getApplicationContext();
lista = (ListView) rootView.findViewById(R.id.listView1);
lista.setAdapter(adapter);
cantidad = (EditText) rootView.findViewById(R.id.cantidad);
precio = (EditText) rootView.findViewById(R.id.precio);
id = (TextView) rootView.findViewById(R.id.id);
// how to get the position of the selected EditText to access him
position( X ) cantidad.getText().toString();
position( X ) precio.getText().toString();
return rootView;
}

Update textview in fragment

This might be a very simple problem, but I can't seem to get it to work.
I'm trying to update a textview in a Fragment on "OnCreateView"
My code looks like this
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_display_product, container, false);
txtAge = (TextView)v.findViewById(R.id.txtAge);
txtCountry = (TextView)v.findViewById(R.id.txtCountry);
txtName = (TextView)v.findViewById(R.id.txtName);
txtAge.setText("Hallo world");
return inflater.inflate(R.layout.fragment_display_product, container,false);
}
The text in textview txtAge never get set and I can't seem to find a reason.
I'm new to android so sorry if this is a stupid question.
/Birger
Do it like this-
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
View v = inflater.inflate(R.layout.fragment_display_product, null);
txtAge = (TextView)v.findViewById(R.id.txtAge);
txtCountry = (TextView)v.findViewById(R.id.txtCountry);
txtName = (TextView)v.findViewById(R.id.txtName);
txtAge.setText("Hallo world");
return v;
}
Return the same view which you have inflated already.

How to get an id of a View inside a Fragment

Hi I am trying to get an id of some View in a Fragment from a string, however it doesn't seem to work... Is there a special case for fragments?
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View singleplayerView = inflater.inflate(R.layout.singleplayer_tab, container, false);
setupMap();
return singleplayerView;
}
private void setupRank() {
TextView view = (TextView) getByStringId("rank0");
view.setText("hello");
}
private final View getByStringId(final String id) {
return findViewById(getResources().getIdentifier(id, "id", getActivi
ty().getPackageName()));
}
So I am trying to set the textView field rank0 in the layout that is used for my fragment... but right now it gets a NUllPointer because it couldnt find the id
Thank you
you can define your Layout In your onCreateView() inside Fragments like this:
View V = new View(getActivity());
V = inflater.inflate(R.layout.fragment_layout,container, false);
Then
TextView SomeField = (TextView)V.findViewById(R.id.TxtSomeField);
Inside fragment, add getActivity before findViewById:
getActivity().findViewById(...);
Try this,
String id;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Bundle extra = getArguments();
id = extra.getString("id", "");
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_calendar_details, container, false);
Button b = (Button) view.findViewById(id);
return view;
}

Categories

Resources