Android Studio如何在Activity中更改TextView片段

3
我已经在activities文件夹中创建了一个活动,并在fragments文件夹中创建了一个片段。看看我的片段构建方式:http://i.stack.imgur.com/jIt9W.png 有人像我一样尝试过这个Android: Update Tab Layout(fragments) textviews from an activity,但我不明白解决方案是什么。
我该如何将数据(例如字符串)从活动发送到此片段,并希望它显示在tab2中(如图片所示)?
我尝试了很多方法,但都没有找到解决方案。
来自我的活动:
Bundle bundle = new Bundle();
bundle.putString("edttext", "From Activity");
// set Fragmentclass Arguments
Fragmentclass fragobj = new Fragmentclass();
fragobj.setArguments(bundle);

从 Fragment 中

String strtext = getArguments().getString("edttext");    

不起作用:(

2个回答

1
我可以推荐一种替代方案 - 使用一个名为EventBus的第三方库。它很容易使用,对于不同的情景非常有用。 创建一个自定义事件,就像这样:
public class MyEvent {
private String text;

public MyEvent(String text) {
    this.text = text;
}

public String getText() {
    return text;
}

public void setText(String text) {
    this.text= text;
}

在您的Activity中,只需发布此事件并附上您希望发送到Fragment的文本即可:
EventBus.getDefault().post(new MyEvent(yourTextString);

现在只需在您的Fragment的onCreate中注册eventBus即可:
EventBus.getDefault().register(this);

并创建一个监听此事件的方法,如下所示:

@Subscribe
public void onMyEvent(MyEvent myEvent){
    String text = myEvent.getText();
    //Now you can do whatever you wish with this text
}

谢谢您的回答, 我需要添加这个库吗? 如果是,我该如何添加它?在哪里添加? - sharon2469
你需要添加这个库。请按照链接中提供的入门指南进行操作。 - Vucko
谢谢,一切都很好。现在如果我想将每个事件的字符串添加到ListView中,是这样做正确吗: List orderList; orderList = new ArrayList(); cources lst = new cources(); lst.name = text; //此处的文本来自事件 orderList.add(lst); ListAdapter adapter = new ListAdapter(orderList, context); listOrder.setAdapter(adapter); - sharon2469
好的,有什么问题吗? - Vucko
你需要在ListAdapter类的实例上执行操作,而不是在类本身上执行。例如,你在哪里实例化了这个类?ListAdapter adapter = new ListAdapter(); 当文本发生变化时添加 adapter.notifyDataSetChanged(); - Vucko
显示剩余5条评论

-1
public class ListAdapter extends BaseAdapter
{
Context context;
List<cources> valueList;

public ListAdapter(List<cources> listValue, Context context)
{
    this.context = context;
    this.valueList = listValue;
}

@Override
public int getCount() 
{
    return this.valueList.size();
}

@Override
public Object getItem(int position) 
{
    return this.valueList.get(position);
}

@Override
public long getItemId(int position) 
{
    return position;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) 
{
    ViewItem viewItem = null;
    if(convertView == null)
    {
        viewItem = new ViewItem();
        LayoutInflater layoutInfiater = (LayoutInflater)this.context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
        //LayoutInflater layoutInfiater = LayoutInflater.from(context);
        convertView = layoutInfiater.inflate(R.layout.list_adapter_view, null);

        viewItem.txtNamePlanche = (TextView)convertView.findViewById(R.id.Tx_name_planche);
        viewItem.txtPriceBaget = (TextView)convertView.findViewById(R.id.Tx_price_baget);
        viewItem.txtPricePlate = (TextView)convertView.findViewById(R.id.Tx_price_plate);
        convertView.setTag(viewItem);
    }
    else
    {
        viewItem = (ViewItem) convertView.getTag();
    }

    viewItem.txtNamePlanche.setText(valueList.get(position).name);
    viewItem.txtPriceBaget.setText(valueList.get(position).price_baget);
    viewItem.txtPricePlate.setText(valueList.get(position).price_plate);
    return convertView;
}

}

和viewitem

class ViewItem
{
TextView txtNamePlanche;
TextView txtPricePlate;
TextView txtPriceBaget;
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接