我正在尝试从另一个活动更改片段布局中的TextView文本。 我试图通过引用本网站的旧帖子来应用各种方法。但那些对我不起作用。
我的活动有自定义标题的AlertDialog框。当我在AlertDialog box中触摸自定义标题的保存按钮时,我需要使用setText命令在fragment_invone.xml中更改R.id.InOneFrag_amount的文本。我可以这样做吗?
这里是InvoBill.Java:
public class InvoBill extends AppCompatActivity {
private DbAdapter dbHelper;
View item_to_invo;
AlertDialog ad;
String ItmId,_qoh,_sale,_cost;
private SimpleCursorAdapter dataAdapter;
AutoCompleteTextView acTextView;
private Toolbar toolbar;
private TabLayout tabLayout;
private ViewPager viewPager;
private int[] tabIcons = {
R.drawable.customer,
R.drawable.report,
R.drawable.invoice
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_invo_bill);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
setTitle("INVOICE#" + Invoice._id_in);
dbHelper = new DbAdapter(this);
dbHelper.open();
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
try {
AddItmToInvo();
}
catch (Exception e){
Log.e("Error", e.getMessage());
}
}
});
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
viewPager = (ViewPager) findViewById(R.id.viewpager_cus);
setupViewPager(viewPager);
tabLayout = (TabLayout) findViewById(R.id.tabs_cus);
tabLayout.setupWithViewPager(viewPager);
//setupTabIcons();
}
private void setupTabIcons() {
tabLayout.getTabAt(0).setIcon(tabIcons[0]);
tabLayout.getTabAt(1).setIcon(tabIcons[1]);
tabLayout.getTabAt(2).setIcon(tabIcons[2]);
}
private void setupViewPager(ViewPager viewPager) {
ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());
adapter.addFragment(new InvOne(), "Items");
adapter.addFragment(new InvTwo(), "Sales");
adapter.addFragment(new InvThree(), "Payments");
viewPager.setAdapter(adapter);
}
class ViewPagerAdapter extends FragmentPagerAdapter {
private final List<Fragment> mFragmentList = new ArrayList<>();
private final List<String> mFragmentTitleList = new ArrayList<>();
public ViewPagerAdapter(FragmentManager manager) {
super(manager);
}
@Override
public Fragment getItem(int position) {
return mFragmentList.get(position);
}
@Override
public int getCount() {
return mFragmentList.size();
}
public void addFragment(Fragment fragment, String title) {
mFragmentList.add(fragment);
mFragmentTitleList.add(title);
}
@Override
public CharSequence getPageTitle(int position) {
return mFragmentTitleList.get(position);
}
}
public void AddItmToInvo(){
LayoutInflater inflater = getLayoutInflater();
View myView = inflater.inflate(R.layout.add_itm_to_invo_heder, null);
ImageButton save, close;
save = (ImageButton)myView.findViewById(R.id.btn_itm_invo_save);
close = (ImageButton)myView.findViewById(R.id.btn_itm_invo_close);
AlertDialog.Builder alt = new AlertDialog.Builder(this);
alt.setCustomTitle(myView);
alt.setView(R.layout.item_to_invo);
ad = alt.show();
final int[] to = new int[] { android.R.id.text1 };
final String[] from = new String[] { "_name" };
acTextView = (AutoCompleteTextView)ad.findViewById(R.id.acTextView2);
SimpleCursorAdapter adapter = new SimpleCursorAdapter(
this,
android.R.layout.simple_dropdown_item_1line,
null,
from,
to);
acTextView.setAdapter(adapter);
acTextView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Cursor cursor = (Cursor) parent.getItemAtPosition(position);
ItmId = cursor.getString(cursor.getColumnIndexOrThrow("_id"));
_qoh = cursor.getString(cursor.getColumnIndexOrThrow("_qoh"));
_sale = (cursor.getString(cursor.getColumnIndexOrThrow("_sale")));
_cost = (cursor.getString(cursor.getColumnIndexOrThrow("_cost")));
if (!(_qoh.isEmpty())) {
((EditText) ad.findViewById(R.id._qoh_invAdd)).
setText(_qoh);
}
if (!(_sale.isEmpty())) {
((EditText) ad.findViewById(R.id._sale_invAdd)).
setText(_sale);
}
}
});
adapter.setCursorToStringConverter(new SimpleCursorAdapter.CursorToStringConverter() {
@Override
public CharSequence convertToString(Cursor cursor) {
final int columnIndex = cursor.getColumnIndexOrThrow("_name");
final String str = cursor.getString(columnIndex);
return str;
}
});
adapter.setFilterQueryProvider(new FilterQueryProvider() {
@Override
public Cursor runQuery(CharSequence constraint) {
return dbHelper.fetchCountriesByName((constraint != null ? constraint.toString() : null));
}
});
save.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
try {
EditText qty, rate;
qty = (EditText) ad.findViewById(R.id._qoh_invAdd);
rate = (EditText) ad.findViewById(R.id._sale_invAdd);
dbHelper.AddUpInvItm(Invoice._id_in, ItmId, qty.getText().toString(), rate.getText().
toString(), String.valueOf(Float.valueOf(qty.getText().toString()) *
Float.valueOf(rate.getText().toString())), String.valueOf(Float.
valueOf(qty.getText().toString()) * Float.valueOf(_cost)));
dbHelper.AdjustQoh(ItmId, String.valueOf(Float.valueOf(_qoh)
- Float.valueOf(qty.getText().toString())));
//I need setText for R.id.InOneFrag_amount in fragment_invone.xml from here.
ad.dismiss();
} catch (Exception e) {
Log.e("Error", e.getMessage());
}
}
});
close.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
ad.dismiss();
}
});
}
}
这是我的InvOne.Java活动:
public class InvOne extends Fragment{
String[] columns;
int[] to;
Cursor cursor;
ListView listView;
TextView myTextView;
private DbAdapter dbHelper;
private SimpleCursorAdapter dataAdapter;
public InvOne() {
// Required empty public constructor
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
dbHelper = new DbAdapter(getContext());
dbHelper.open();
}
@Override
public View onCreateView(LayoutInflater inflat, ViewGroup container,
Bundle savedInstanceState) {
View v = inflat.inflate(R.layout.fragment_invone, container, false);
//I set default value to R.id.InOneFrag_amount from here.
myTextView = (TextView)v.findViewById(R.id.InOneFrag_amount);
myTextView.setText("test");
displayListView(v);
return v;
}
public void displayListView(View v){
try{
cursor = dbHelper.fetchInvoItemById(Invoice._id_in);
columns = new String[] {
DbAdapter.InvoItm_COL_5,
DbAdapter.InvoItm_COL_6,
DbAdapter.InvoItm_COL_7,
DbAdapter.InvoItm_COL_7,
};
to = new int[] {
R.id.InvItm_qty,
R.id.InvItm_rate,
R.id.InvItm_amount,
R.id.InvItm_des,
};
dataAdapter = new SimpleCursorAdapter(
getContext(), R.layout.row_inv_itm,
cursor,
columns,
to,
0);
listView = (ListView)v.findViewById(R.id.lViewFragInvOne);
listView.setAdapter(dataAdapter);
listView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
@Override
public boolean onItemLongClick(AdapterView<?> adapterView, View view, int i, long l) {
return true;
}
});
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
}
});
}
catch (Exception e){
Log.v("Error ", e.getMessage());
}
}
}
答案 0 :(得分:0)
要从其他活动更改textView的文本:
活动-1中的代码:
private void goToActivity(Class<?extends Activity> activityClass,String text)
{
Intent newActivity24=new Intent(this,activityClass);
newActivity24.putExtra("key",text);
startActivity(newActivity24);
}
活动-2中的代码:
TextView steamB=(TextView) findViewById(R.id.tv);
String textChange=getIntent().getExtras().get("key").toString();
steamB.setText(textChange);
答案 1 :(得分:0)
在您的第一个活动中创建一个新的公共方法
href="#"
并将您的活动实例传递给其他活动。然后从其他活动中,在需要时随时调用它。
public void setTextofTextView(String text)
{
textView.setText(text);
}