在Android中,单击项目上的详细信息片段替换ListView片段

时间:2014-09-03 14:32:12

标签: android listview android-fragments

我有Fragment的数据库搜索功能。现在,当用户单击搜索结果列表中的项目时,我希望将搜索片段替换为细节片段。我的理解是片段交易不能在片段中启动,所以我所做的就是将主要活动类中的片段分类,一切正常,直到你想点击搜索结果中的一个项目为止。我的最低API是10,所以我正在使用支持库,当我尝试在setOnClickListener()中设置事务时执行此操作:FragmentTransaction ft = getSupportFragmentManager().beginTransaction();我收到警告:Cannot make a static reference to the non-static method getSupportFragmentManager() from the type FragmentActivity那么我如何用setOnClickListener()中的细节片段替换我的搜索片段?这是我所拥有的:

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



}
}

2 个答案:

答案 0 :(得分:0)

尝试使用

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

答案 1 :(得分: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();