gpt4 book ai didi

android - 从 ViewPager 中的 FragmentActivity 获取 Fragment 中 TextView 的 id

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:54:31 24 4
gpt4 key购买 nike

我正在使用带有 3 个 FragmentsViewPager,我想在第三页中更改 TextView 的文本。

在该页面中,我有一个 Button,当按下它时,转到 SD 图像以选择一个。完成后,返回页面并希望使用该图像的路径更新 TextView。问题是,当我尝试从 FragmentActivity 访问那个 TextView 时,它是空的。

这是我的代码

SherlockFragmentActivity:

public class TabsFacturasActivity extends SherlockFragmentActivity {

protected MyApplication myApplication;
private static final int FILE_SELECT_CODE = 0;

private MyAdapter mAdapter;
private ViewPager mPager;
private PageIndicator mIndicator;
private TextView textViewImg;

private int lecturas = 0;
private SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
private boolean adjunto = false;
private String filePath;
private boolean esLecturaAT = false;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fragment_pager);

// Get the application instance
myApplication = (MyApplication)getApplication();

//Need to get that view
textViewImg = (TextView) findViewById(R.id.textViewUrlImgLectura);

//Creamos la lista
LinkedList<String> direcciones = new LinkedList<String>();
ArrayList<TuplaCupsWS> dirs = myApplication.getUsuarioActual().getCups();
for(int dir = 0; dir < myApplication.getUsuarioActual().getCups().size(); dir++) {
direcciones.add(new String(dirs.get(dir).getDireccion()));
}

int tab = getIntent().getIntExtra("tab", 0);

mAdapter = new MyAdapter(getSupportFragmentManager());

mPager = (ViewPager)findViewById(R.id.pager);
mPager.setAdapter(mAdapter);
mPager.setCurrentItem(tab);

mIndicator = (TitlePageIndicator)findViewById(R.id.indicator);
mIndicator.setViewPager(mPager);

getSupportActionBar().setDisplayShowTitleEnabled(false);
getSupportActionBar().setIcon(R.drawable.logo_factorenergia_peque);

/** Create an array adapter to populate dropdownlist */
ArrayAdapter<String> adapter = new ArrayAdapter<String>(
getBaseContext(), android.R.layout.simple_spinner_dropdown_item, direcciones);

/** Enabling dropdown list navigation for the action bar */
getSupportActionBar().setNavigationMode(ActionBar.NAVIGATION_MODE_LIST);

/** Defining Navigation listener */
OnNavigationListener navigationListener = new OnNavigationListener() {

@Override
public boolean onNavigationItemSelected(int itemPosition, long itemId) {
return false;
}
};

/** Setting dropdown items and item navigation listener for the actionbar */
getSupportActionBar().setListNavigationCallbacks(adapter,
(com.actionbarsherlock.app.ActionBar.OnNavigationListener)
navigationListener);
}

@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];
textViewImg.setText(imgName); //Here textViewImg is null
filePath = path;
}
break;
}
super.onActivityResult(requestCode, resultCode, data);
}

//Method executed when Button is pressed
public void examinar(View view) {
mostrarFileChooser();
}

private void mostrarFileChooser() {
Intent intent = new Intent(Intent.ACTION_PICK);
intent.setType("image/*");

try {
startActivityForResult(intent, FILE_SELECT_CODE);
} catch (android.content.ActivityNotFoundException ex) {
}
}

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();
}
//return new MyFragment();
return null;
}

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

}

fragment_pager.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
tools:context=".TabsFacturasActivity" >

<com.viewpagerindicator.TitlePageIndicator
android:id="@+id/indicator"
android:padding="10dip"
android:layout_height="wrap_content"
android:layout_width="fill_parent" />

<android.support.v4.view.ViewPager
android:id="@+id/pager"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:background="#1184A4E8" />

</LinearLayout>

最佳答案

您无法从 fragment activity 访问 TextView ,因为它位于 fragment 而您的 fragment activity layout 没有任何 带有此 id 的 TextView 。您必须从您的 third fragment 在其 layout 中使用它的地方访问 TextView 。然后从您的 fragment Activity 访问该对象

在你的 fragment 中做这样的事情

TextView mTextView;
mTextView = (TextView)getView().findViewById(R.id.your_text_view);

像这样创建一个函数

public void changeText(String mText)
{
mTextView.setText(mText);
}

在您的 Activity 结果中

//Do not create new object each time you set text. Use the same fragment object which you use for view pager.
Your_Fragment mFragment;
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
switch (requestCode) {
case FILE_SELECT_CODE:
if (resultCode == RESULT_OK) {

//Set text from here
mFragment.changeText(imgName);

}

关于android - 从 ViewPager 中的 FragmentActivity 获取 Fragment 中 TextView 的 id,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18486952/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com