使用list fragmenttabhost,简单的游标适配器与2.3及更高版本兼容

时间:2012-12-09 18:44:04

标签: listview android-listview android-asynctask android-tabhost android-2.3-gingerbread

我正在创建应用以加载特定公司的推文。为了使其与Android 2.3及更高版本兼容,我需要使用FragmentActivity& ListFragment。我正在使用AsyncTask在FragmentAcitivity类中获取关于app start的推文并添加选项卡并将它们加载到sqlite DB中。现在,在选项卡上单击我需要从我正在使用LoaderManager的sqllite DB加载这些推文,但是在选项卡点击上没有显示任何内容。这是我的代码片段:

FragmentActivity:

     @Override
    public void onTabChanged(String tag) {  
Log.d(TAG, "onTabChanged");  
TabInfo newTab = this.mapTabInfo.get(tag);  
if (mLastTab != newTab) {  
  FragmentTransaction ft = this.getSupportFragmentManager().beginTransaction();  
if (mLastTab != null) {  
if (mLastTab.fragment != null) {  
 ft.detach(mLastTab.fragment);  
 }  
}  
if (newTab != null) {  
if (newTab.fragment == null) {  
newTab.fragment = (ListFragment) ListFragment.instantiate(this, newTab.clss.getName(), newTab.args);  
ft.add(R.id.realtabcontent, newTab.fragment, newTab.tag);  
} else {ft.attach(newTab.fragment);  
}  
}     
mLastTab = newTab;  
ft.commit();  
this.getSupportFragmentManager().executePendingTransactions();  
}    
}  

ListFragment:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {  
Log.d(TAG, "onCreateView");  
if (container == null) {  
return null;  
}   
adapter = new SimpleCursorAdapter(getActivity().getApplicationContext(), R.layout.row, null,TweetStatusProvider.FROM,TweetStatusProvider.TO,CursorAdapter.FLAG_REGISTER_CONTENT_OBSERVER);  
adapter.setViewBinder(VIEW_BINDER);  
getLoaderManager().initLoader(STATUS_LOADER, null, this);  
Log.d(TAG, "initLoader executed");  
View view = inflater.inflate(R.layout.list_fragment_layout, container, false);      
ListView listView = (ListView)view.findViewById(android.R.id.list);  
listView.setAdapter(adapter);  
return view;  
}  

布局: main.xml中:

 <?xml version="1.0" encoding="utf-8"?>  
 '<'TabHost xmlns:android="http://schemas.android.com/apk/res/android"  
  android:id="@android:id/tabhost"  
  android:layout_width="match_parent"  
  android:layout_height="match_parent" '>'  
'<'LinearLayout  
android:layout_width="match_parent"  
android:layout_height="match_parent"  
android:background="@drawable/background"  
android:orientation="vertical"  
android:padding="10dp" '>'  
'<'HorizontalScrollView  
android:layout_width="match_parent"  
android:layout_height="wrap_content"  
android:scrollbars="none" '>'  
'<'TabWidget  
android:id="@android:id/tabs"  
android:layout_width="54dp"  
android:layout_height="60dp" '/>'  

'<'/HorizontalScrollView'>'  
'<'FrameLayout  
android:id="@android:id/tabcontent"  
android:layout_width="match_parent"  
android:layout_height="match_parent"  
android:padding="5dp" '>'  
'<'/FrameLayout'>'  
'<'FrameLayout  
android:id="@+android:id/realtabcontent"  
android:layout_width="fill_parent"  
android:layout_height="0dp"  
android:layout_weight="1" /'>'  
'<'/LinearLayout'>'  
'<'/TabHost'>'  

list_fragment_layout.xml:

 '<'?xml version="1.0" encoding="utf-8"?'>'  
 '<'LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"     
 android:layout_width="match_parent"    
 android:layout_height="match_parent"    
 android:orientation="vertical" '>'    

 '<'ListView  
 android:id="@android:id/list"  
 android:layout_width="match_parent"  
 android:layout_height="wrap_content" '>'  
 '<'/ListView'>'    
'<'/LinearLayout'>'  

row.xml:

'<'?xml version="1.0" encoding="utf-8"?'>'  
'<'RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
    android:layout_width="match_parent"  
    android:layout_height="match_parent" '>'  
    '<'TextView  
 android:id="@+id/text_user"  
 android:layout_width="wrap_content"  
 android:layout_height="wrap_content"  
 android:layout_alignParentLeft="true"  
 android:layout_alignParentTop="true"  
 android:text="Large Text"  
 android:textAppearance="?android:attr/textAppearanceLarge" /'>'  

'<'TextView  
 android:id="@+id/text_created_at"  
 android:layout_width="wrap_content"  
 android:layout_height="wrap_content"  
 android:layout_alignParentRight="true"  
 android:layout_alignParentTop="true"  
 android:text="10 minutes ago"  
 android:textAppearance="?android:attr/textAppearanceMedium" /'>'  
'<'TextView  
  android:id="@+id/text_text"  
  android:autoLink="all"  
      android:layout_width="wrap_content"  
  android:layout_height="wrap_content"  
  android:layout_alignParentLeft="true"  
  android:layout_alignParentRight="true"  
  android:layout_below="@+id/text_user"  
  android:text="Small Text"  
  android:textAppearance="?android:attr/textAppearanceSmall" /'>'  
'<'/RelativeLayout'>'  

0 个答案:

没有答案