【问题标题】:Android Studio change TextView fragment from activityAndroid Studio 从活动中更改 TextView 片段
【发布时间】:2016-06-22 20:14:35
【问题描述】:

我在活动文件夹中创建了一个活动,并在片段文件夹中创建了一个片段。看看我的片段是如何构建的: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);

从片段中

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

不工作:(

【问题讨论】:

    标签: android android-activity fragment


    【解决方案1】:

    我可以推荐一个替代解决方案 - 使用名为 EventBus 的第 3 方库。它非常易于使用,并且对于不同的场景非常有用。 创建一个自定义事件,如下所示:

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

    只需在您的活动中,使用您希望发送到片段的文本发布此事件:

    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
    }
    

    【讨论】:

    • 谢谢你的回答,我需要添加这个库吗?如果是的话,如何以及放在哪里?
    • 你确实需要添加这个库。按照链接中提供的入门指南进行操作。
    • 不错!该库将解决您的许多问题 :) 如果您对解决方案感到满意,请考虑接受答案 :)
    • 谢谢,一切都很好,现在如果我想将每个事件的字符串添加到 ListView。这样做是对的: List orderList; orderList = new ArrayList();课程 lst = 新课程(); lst.name = 文本; //此文本来自事件 orderList.add(lst); ListAdapter 适配器 = new ListAdapter(orderList, context); listOrder.setAdapter(适配器);
    • 是的,好的。似乎是什么问题?
    【解决方案2】:
    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;
    }
    

    }

    和查看项目

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-04-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多