我有一個數據庫搜索功能在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
}
}
添加一些說明爲什麼這是問題的解決方案。現在答案是質量很低。 – Cheesebaron 2014-09-03 15:39:30
我認爲這個解決方案可能會起作用,除了我在android.support.v4.app.BackStackRecord.doAddOp(BackStackRecord.java:394)處得到關於BackStackRecord java.lang.NullPointerException的錯誤' – kirktoon1882 2014-09-03 17:04:13