编辑:
这就是我在片段中调用方法的方法。
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始终为null。该片段存在于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");
}
}
}
}
答案 0 :(得分:1)
嗯,这一行修好了。
DummySectionFragment dummy = (DummySectionFragment) mViewPager.getAdapter().instantiateItem(mViewPager, id);
通过这项努力,我明白我需要详细阅读有关碎片的信息,然后再继续阅读。
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);
}
}
答案 1 :(得分: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
{ TextView 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");
}
}
}
}