0

我想更改TextViewinonActivityResult方法的文本。它似乎设置正确,但我在view.

TextView是在第三FragmentViewPager

所以这是我的代码

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;
        }

    }
}

如您所见,我尝试过使用invalidate,但它什么也没做。我该如何解决这个问题?

4

3 回答 3

0

我已经解决了这个答案:https ://stackoverflow.com/a/18487696/1088643

Fragment我在这里得到了我的和访问方法的实例:

LecturaActivity lecturaFragment = (LecturaActivity) 
                        getSupportFragmentManager().findFragmentByTag(
                           "android:switcher:"+R.id.pager+":"+2);
lecturaFragment.changeImgText(imgName);
于 2013-08-29T10:00:08.530 回答
0

尝试textViewImg.requestLayout();

于 2013-08-29T08:30:56.720 回答
0

你的片段可能有问题。可能在您可能已加载的另一个片段中存在另一个具有相同 ID 的文本视图,并且正在设置它们的文本。尝试打印 textview id 的值。

于 2013-08-29T08:42:04.073 回答