Textview Is Null On First Click But Updates On Second Click
Solution 1:
Instead of doing details.setTextView(index) in DetailActivity , set the value of TextView in the DetailFragment's onActivityCreated while passing the value to be set in fragments setArgument method in the Detailactivity...
DetailsFragment details = new DetailsFragment();
details.setArguments(getIntent().getExtras()); // pass the value of text view here
And In fragment onActivityCreated get that value via getArguments() and set it in textview..
EDIT for sending value Selected to loaded Fragment
In List Activity
detailFragment = getFragmentbyTag
if(detailFragment == null)
Create Fragment and Add it and Set Arguments here as well
else
detailFragment.setTextView(value); // fragment already loaded no need to set arguments
if you want to replace each time and not add once and use the added/loaded fragment , use setarguments each time.... and remove previous fragment and add new fragment( with Arguments) But added once and reused is preferred instead of removing and adding/ replacing on each click
Solution 2:
Updated2
Just initialize your TextView
in onCreateView()
no need to create DetailFragment
with parametrize just put in comment
Check the details object whether its null or not if null initialize as you do and then called setTextView()
if(details==null)
details = new DetailFragment(index);
else
setTextView(index);
TextView inside onCreateView()
and used their
public class DetailFragment extends Fragment {
String[] capitals;
int index;
private TextView textView;
public DetailFragment(int index){
this.index = index;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (container == null)
return null;
capitals = new String[] { "delhi", "karachi", "colombo", "beijing",
"dhaka", "katmandu", "Afghanistan", "pyongyang", "seoul",
"tokyo" };
View v = inflater.inflate(R.layout.detail_fragment, container, false);
// Initialize textView
textView = (TextView) v.findViewById(R.id.detailView);
setTextView(index); // set value value here for first time
return v;
}
public void setTextView(int index) {
if(textView!=null)
textView.setText(capitals[index]);
}
public int getShownIndex() {
return getArguments().getInt("index", 0);
}
}
Solution 3:
did you tried using this
TextView view = (TextView) findViewById(R.id.detailView);
instead of this
TextView view = (TextView) getView().findViewById(R.id.detailView);
Post a Comment for "Textview Is Null On First Click But Updates On Second Click"