0

编辑:

这就是我在片段中调用该方法的方式。

public void setTheTextView(int id) { //id is the id of the tab
    if(parsed==true) { //parsed - ensures if s, p1 are not null
        DummySectionFragment dummy = new DummySectionFragment();
        if(id==0) dummy.setTheText(s, p1);
        else if(id==1) dummy.setTheText(s, p2); 
        else dummy.setTheText(s, p3);
    }
}

我有以下片段,它在 AsyncTask 完成后显示文本。我经历了几个线程,从中我可以收集到我们必须使用 View 来获取 TextView 实例,然后对其进行更新。因此我宣布

View rView

可悲的是,它没有任何效果,并且 TextView 始终为空。该片段存在于 MainActivity.java 中,其源代码如下。

public static class DummySectionFragment extends Fragment {
    TextView tView;
    View rView;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        rView = inflater.inflate(R.layout.fragment_main_dummy, container, false);
        tView = (TextView)rView.findViewById(R.id.section_label);
        return rView;
    }

    public void setTheText(ArrayList<String> a, ArrayList<String> b) {  
        Log.d("bh","TextView:"+tView); //Always null here
        if(tView != null) { 
            for(int i=0;i<6;i++) {
                tView.append(a.get(i)+" "+b.get(i)+"\n\n");
            }
        }
    }
}
4

2 回答 2

1

好吧,这一行修复了它。

DummySectionFragment dummy = (DummySectionFragment) mViewPager.getAdapter().instantiateItem(mViewPager, id);

通过这次努力,我了解到我需要详细阅读 Fragments,然后再继续阅读。

public void setTheTextView(int id) { 
    if(parsed==true) { 
    DummySectionFragment dummy = (DummySectionFragment) mViewPager.getAdapter().instantiateItem(mViewPager, id);
    if(id==0) dummy.setTheText(s, p1);
    else if(id==1) dummy.setTheText(s, p2); 
    else dummy.setTheText(s, p3);
    }
}
于 2013-07-16T06:26:15.587 回答
0

请尝试使用下面的代码,因为它最终可以正常工作。// 活动

公共类 MainActivity 扩展 FragmentActivity {

/** Called when the activity is first created. */

ListView listview;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    DummySectionFragment dummySectionFragment = new DummySectionFragment();
    FragmentTransaction fragmentTransaction = getSupportFragmentManager()
            .beginTransaction();

    fragmentTransaction.replace(R.id.layout, dummySectionFragment);
    fragmentTransaction.commit();
    /*
     * RestClient rc = new RestClient();
     * 
     * // the account key might not work. Please insert your own set.
     * ArrayList<Weather> list1 = rc.getPlaces("", ""); int index = 3;
     * String[] listArray = new String[list1.size()]; StringBuilder sb = new
     * StringBuilder(); int counter = 0; Log.e("N", "listArray::" +
     * list1.size());
     * 
     * for (int i = 0; i < list1.size(); i++) { sb.append("\n\nEntry #: " +
     * i); sb.append("\n" + "Title: \"" + list1.get(i).getTitle() + "\"");
     * sb.append("\n" + "Publish Date: \"" + list1.get(i).getPubDate() +
     * "\""); sb.append("\n" + "Condition: \"" + list1.get(i).getCondition()
     * + "\""); sb.append("\n" + "Forecast: \"" + list1.get(i).getForecast()
     * + "\"");
     * 
     * listArray[counter] = sb.toString(); counter++; sb.delete(0,
     * sb.capacity()); } Log.e("N", "listArray::" + listArray.length);
     * 
     * listview.setAdapter(new ArrayAdapter<String>(this,
     * android.R.layout.simple_list_item_1, listArray));
     * 
     * }
     */
}

}

// 片段

公共类 DummySectionFragment 扩展了 Fragment

{ 文本视图 tView; 查看 rView;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    rView = inflater.inflate(R.layout.frg, container, false);
    tView = (TextView) rView.findViewById(R.id.textView1);
    ArrayList<String> arrayList = new ArrayList<String>();
    ArrayList<String> arrayList1 = new ArrayList<String>();
    for (int i = 0; i < 6; i++) {
        arrayList.add("" + i);

        arrayList1.add("" + i + 10);
    }
    setTheText(arrayList, arrayList1);
    return rView;
}

public void setTheText(ArrayList<String> a, ArrayList<String> b) {
    Log.d("bh", "TextView:" + tView); // Always null here
    if (tView != null) {
        for (int i = 0; i < 6; i++) {
            tView.append(a.get(i) + " " + b.get(i) + "\n\n");
        }
    }
}

}

于 2013-07-16T06:45:14.947 回答