-2

I have 2 fragment classes. In Fragment1.java,

 listview.setOnItemClickListener(new AdapterView.OnItemClickListener()
        {
            @Override
            public void onItemClick(AdapterView<?> parent, View view,
                                    int position, long id) {
                // TODO Auto-generated method stub
                String favorite_message =(parent.getItemAtPosition(position).toString());
                Log.d("hi","favorite" + favorite_message);
                //Populate the edit text of Fragment1
            }
        });

In Fragment2.java,

     @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
     mytext= (EditText) view.findViewById(R.id.myEditText);
     getText();
    }

    public void getText(){
     String message = mytext.getEditableText().toString();
   }

I want the favorite_message to be present in the mytext edittext. Since these 2 fragments are like tabs, upon onItemClick I want the tab position to be automatically shifted to Fragment2 and edit text should be favorite_message.

How do I do this?

Mark023
  • 105
  • 12

3 Answers3

1

Do you have any Button or ImageButton in your ListView-Item If yes then, this attribute has to be added to the top most layout of your XML where you have provided the ListView elements.

 android:descendantFocusability="blocksDescendants"

if not then can you post your both classes, by the way now you must use recyclerview

Sathish Kumar J
  • 4,280
  • 1
  • 20
  • 48
Sachin
  • 107
  • 1
  • 6
1

Make your viewpager of activity global as below in your Activity file where you are setting your viewpager.

 public static ViewPager YOUR_VIEW_PAGER;

Make your String global first as below in your Fragment1:

public static boolean isListClicked=false;
public static String favorite_message ="";

listview.setOnItemClickListener(new AdapterView.OnItemClickListener()
    {
        @Override
        public void onItemClick(AdapterView<?> parent, View view,
                                int position, long id) {
            // TODO Auto-generated method stub
           isListClicked=true; 
           favorite_message =(String)parent.getItemAtPosition(position);
           //set second fragment in your viewpager by below line
           YOURACTIVITY.YOUR_VIEW_PAGER.setCurrentItem(1);
            Log.d("hi","favorite" + favorite_message);
            //Populate the edit text of Fragment1
        }
    });

In onCreateView of Fragment2,write as below:

 if (Fragment1.isListClicked){
    if(!Fragment1.favorite_message.equals(""))
      mytext.setText(Fragment1.favorite_message);

 }

Also add below method in your Fragment2

 @Override
public void setUserVisibleHint(boolean isVisibleToUser) {
    super.setUserVisibleHint(isVisibleToUser);
    if (isVisibleToUser) {
        if (Fragment1.isListClicked){
            if(!Fragment1.favorite_message.equals(""))
                txt.setText(Fragment1.favorite_message);
        }

    }
}
Dhruvi
  • 1,971
  • 2
  • 10
  • 18
  • Thanks for the answer. I implemented it and the view of the fragment changes correctly I mean it gets shifted to Fragment2 upon click. But the edit text does not have the favorite_message – Mark023 Jul 22 '16 at 12:09
  • Check my edited code in onItemClick to get your selecteditem. – Dhruvi Jul 22 '16 at 12:14
  • Try to print Fragment1.favorite_message in your onCreateView of Fragment2 before if(!Fragment1.favorite_message.equals("")) mytext.setText(Fragment1.favorite_message); – Dhruvi Jul 22 '16 at 12:19
  • Ok I did it...But there is no log coming. It comes when the fragment is created for first time after which I move to fragment1 and do onClick. – Mark023 Jul 22 '16 at 12:23
  • Ok also write above lines in your onResume method in Fragment2. – Dhruvi Jul 22 '16 at 12:24
  • I tried it but still same problem... It is not coming to onResume also upon itemclick – Mark023 Jul 22 '16 at 12:27
  • @Mark023 Please check my edited answer, it solves your problem.:-) – Dhruvi Jul 22 '16 at 12:43
  • Can you please tell what is OneFragment.isListClicked. Is it Fragment1.isListClicked? If yes, it shows isListClicked cannot be resolved – Mark023 Jul 22 '16 at 12:48
  • Yeah sorry it is Fragment1.isListClicked and add public static boolean isListClicked=false; in Fragment1 and not in activity as I edited above. – Dhruvi Jul 22 '16 at 12:52
  • :) Happiiee Coding. – Dhruvi Jul 22 '16 at 12:57
  • Keeping reference of `View`s in static fields is a very bad way. See this link about how a Fragment should interact with other fragments and its parent Activity - https://developer.android.com/training/basics/fragments/communicating.html – Sufian Jul 27 '16 at 07:55
0

You can get the fragment by using the fragment's list from fragment manager

if using support fragment manager: getSupportFragmentManager().getFragments();

if using fragment manager: getFragmentManager();

u knw that Fragment2 is in second position in your fragment list

so you can set the text by doing this

((Fragment2)getSupportFragmentManager().getFragments().get(1)).setText("Text");

where setText(String) is a public method you can make in Fragment2

What do you mean by tab btw ???