Question

I want to change text of TextViewin onActivityResultmethod. It seems like it's set correctly, but I can't see it in view.

That TextViewis in third Fragment of ViewPager.

So here is my code:

public class TabsFacturasActivity extends SherlockFragmentActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.fragment_pager);
        //...
        mAdapter = new MyAdapter(getSupportFragmentManager());

        mPager = (ViewPager)findViewById(R.id.pager);
        mPager.setAdapter(mAdapter);
       //...
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        switch (requestCode) {
            case FILE_SELECT_CODE:
            if (resultCode == RESULT_OK) {
                adjunto = true;
                // Get the Uri of the selected file 
                Uri uri = data.getData();
                // Get the path
                String path = "";
                try {
                    path = MyUtility.getPath(this, uri);
                } catch (URISyntaxException e) {
                    myApplication.throwException(this);
                    e.printStackTrace();
                }
                String imgName = path.split("/")[path.split("/").length-1];
                LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                View view = inflater.inflate(R.layout.activity_lectura, null, false);
                textViewImg = (TextView) view.findViewById(R.id.textViewUrlImgLectura);
                System.out.println("text view before: "+textViewImg.getText()); //It returns correct value "..."
                textViewImg.setText(imgName);
                textViewImg.invalidate(); //It does nothing
                System.out.println("text view after: "+textViewImg.getText()); //It returns correct value with image path
                filePath = path;
            }
            break;
        }
        super.onActivityResult(requestCode, resultCode, data);
    }

    private static class MyAdapter extends FragmentPagerAdapter {

        private String[] titles = { "VER FACTURAS", "VER CONSUMO", "INTRODUCIR LECTURA" };

        public MyAdapter(FragmentManager fragmentManager) {
            super(fragmentManager);
        }

        @Override
        public CharSequence getPageTitle(int position) {
            return titles[position];
        }

        @Override
        public Fragment getItem(int position) {
            switch (position) {
            case 0: // Fragment # 0
                return new FacturasActivity();
            case 1: // Fragment # 1
                return new ConsumoActivity();
            case 2:// Fragment # 2
                return new LecturaActivity(); //TextView is here
            }
            return null;
        }

        @Override
        public int getCount() {
            return titles.length;
        }

    }
}

As you can see, I've tried using invalidate, but it does nothing. How can I solve that issue?

Was it helpful?

Solution 3

I've solved with that answer: https://stackoverflow.com/a/18487696/1088643

I get the instance of my Fragment and access methods here:

LecturaActivity lecturaFragment = (LecturaActivity) 
                        getSupportFragmentManager().findFragmentByTag(
                           "android:switcher:"+R.id.pager+":"+2);
lecturaFragment.changeImgText(imgName);

OTHER TIPS

Try textViewImg.requestLayout();

There could be something wrong with your fragments. May be there is another textview with the same id in another fragment that you might have loaded, and the text is being set their. Try printing the values of the textview ids.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top