2014-09-03 113 views
0

我有一个数据库搜索功能在Fragment工作。现在,我希望搜索片段在用户单击搜索结果列表中的项目时用细节片段替换。我的理解是,片段事务不能在片段中启动,所以我所做的是将片段归入主活动类中,并且一切正常,直到您希望单击搜索结果中的项目。我的最小API是10,所以我使用的支持库,当我尝试通过这样设置setOnClickListener()中的交易时:FragmentTransaction ft = getSupportFragmentManager().beginTransaction();我收到警告:Cannot make a static reference to the non-static method getSupportFragmentManager() from the type FragmentActivity那么如何将我的搜索片段替换为setOnClickListener()中的细节片段?下面是我得到了什么:替换ListView片段与细节片段上的项目点击

public class SearchInterface extends ActionBarActivity { 
ActionBar bar_AB; 
private static EditText searchbox_ET; 
private static DBAdapter dbHelper; 
static View searchRootView; 
static ListView listView_LV; 
String searchResultStr; 
static Cursor getPathCursor; 
static String cursorSDFStr = null; 
static String cursorCalDateStr = null; 
static String cursorURLStr = null; 
static String cursorTitleStr = null; 
static String cursorinfoBodyStr = null; 

String videoURLStr = null; 
String sdfStr = null; 
String calDateStr = null; 
String titleStr = null; 
String infoBodyStr = null; 

static FragmentManager fragMan; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    overridePendingTransition(R.anim.fadein, R.anim.fadeout); 

    // --- set up Action Bar 
    bar_AB = getSupportActionBar(); 
    bar_AB.setDisplayHomeAsUpEnabled(true); 

    fragMan = getSupportFragmentManager(); 
    if (fragMan.findFragmentById(android.R.id.content) == null) { 
     SSISearchFragEm searchEm_FRG = new SSISearchFragEm(); 
     fragMan.beginTransaction().add(android.R.id.content, searchEm_FRG) 
       .commit(); 
    } 

}// --- END onCreate 

public static class SSISearchFragEm extends Fragment { 
    LinearLayout list_LL; 
    private Button search_BTN; 

    // --- Create the ROOT VIEW 
    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
      Bundle savedInstanceState) { 
     searchRootView = inflater.inflate(R.layout.ssi_search_frag, 
       container, false); 
     searchbox_ET = (EditText) searchRootView 
       .findViewById(R.id.ssi_Search1_et1); 
     search_BTN = (Button) searchRootView 
       .findViewById(R.id.ssi_search_btn1); 
     list_LL = (LinearLayout) searchRootView 
       .findViewById(R.id.ssi_search_list_LL); 

     // --- Button 
     search_BTN.setOnClickListener(new OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       dbHelper = new DBAdapter(getActivity()); 
       dbHelper.open(); 
       String searchTermStr = searchbox_ET.getText().toString(); 
       Cursor cursor = dbHelper.searchDB(searchTermStr); 

       if (cursor != null) { 
        String[] columns = new String[] { DBAdapter.KEY_TITLE, 
          DBAdapter.KEY_CAL_DATE, DBAdapter.KEY_PATH, 
          DBAdapter.KEY_SDF, DBAdapter.KEY_INFOBODY, 
          DBAdapter.KEY_KEYWORDS }; 

        int[] to = new int[] { R.id.slp_title_tv, R.id.slp_date_tv, 
          R.id.slp_url_tv, R.id.slp_sdf_tv, R.id.slp_infoBody_tv, 
          R.id.slp_keywords_tv }; 

        SimpleCursorAdapter dataAdapter = new SimpleCursorAdapter(
          getActivity(), R.layout.slp_list_item, cursor, columns, to, 
          0); 

        listView_LV = (ListView) searchRootView 
          .findViewById(R.id.ssisearch_list_lv); 
        listView_LV.setEmptyView(searchRootView 
          .findViewById(R.id.ssiempty_list_tv)); 
        listView_LV.setAdapter(dataAdapter); 
       }     
       dbHelper.close(); 
       //--- onClick 

       // --- pass to ListVideo 
       listView_LV.setOnItemClickListener(new OnItemClickListener() { 
        public void onItemClick(AdapterView<?> parent, View view, 
          int position, long id) { 
         getPathCursor = (Cursor) listView_LV 
           .getItemAtPosition(position); 
         cursorSDFStr = getPathCursor.getString(getPathCursor 
           .getColumnIndexOrThrow("sdfdate")); 
         cursorCalDateStr = getPathCursor.getString(getPathCursor 
           .getColumnIndexOrThrow("caldate")); 
         cursorURLStr = getPathCursor.getString(getPathCursor 
           .getColumnIndexOrThrow("path")); 
         cursorTitleStr = getPathCursor.getString(getPathCursor 
           .getColumnIndexOrThrow("title")); 
         cursorinfoBodyStr = getPathCursor.getString(getPathCursor 
           .getColumnIndexOrThrow("details")); 
         Toast.makeText(getActivity(), "Item Be Clicked!", Toast.LENGTH_SHORT).show(); 

         //--- Detail fragment here? 
         FragmentTransaction ft = getSupportFragmentManager().beginTransaction(); 


        } 

       });//--- END onClick 

      } 
     }); 
     // --- END Button 

     return searchRootView; 
    }// --- END Create the ROOT VIEW 



} 
} 

回答

0

我想通了。 @jitain sharma让我走上了正确的道路,尽管我发现getChildFragmentManager()不是我的解决方案。这是我的解决方案:

fragTrans = fragMan.beginTransaction(); 
listFrag = fragMan.findFragmentByTag("com.myapp.SearchFragEm"); 
if(listFrag != null)fragTrans.remove(listFrag); 
video_FRG = new SSIVideoFrag(); 
fragTrans.replace(R.id.frag5_main_FL, video_FRG); 
fragTrans.addToBackStack(null); 
fragTrans.commit(); 
0

尝试使用

FragmentTransaction fragmentTransaction = 
       getChildFragmentManager().beginTransaction(); 
     fragmentTransaction.replace(R.id.graph_layout, checkUpGraphFragment); 
     fragmentTransaction.commit(); 
+2

添加一些说明为什么这是问题的解决方案。现在答案是质量很低。 – Cheesebaron 2014-09-03 15:39:30

+0

我认为这个解决方案可能会起作用,除了我在android.support.v4.app.BackStackRecord.doAddOp(BackStackRecord.java:394)处得到关于BackStackRecord java.lang.NullPointerException的错误' – kirktoon1882 2014-09-03 17:04:13