2015-12-10 25 views
-1

我已經開發了應用程序Android與查詢片段,有3個步驟,並在最後可以保存所有參數查詢通過點擊後,當我去的片段,查詢保存,如果我點擊我打開直接顯示查詢結果的活動,這是我的代碼:意大利Android開放片段

listview = (ListView)findViewById(R.id.ListView); 
    QueryListAdapter adapter = new QueryListAdapter(this); 
    listview.setAdapter(adapter); 
    listview.setOnItemClickListener(new AdapterView.OnItemClickListener() { 
     @Override 
     public void onItemClick(AdapterView<?> parent, View view, 
           int position, long id) { 

      Intent intent = new Intent(QueriesActivity.this, CustomListViewActivityQuery.class); 
      Bundle bundle = new Bundle(); 
      FirstProjectApplication.query = FirstProjectApplication.allQueries.get(position); 
      bundle.putBoolean("query", true); 
      intent.putExtras(bundle); 
      startActivity(intent); 
     } 
    }); 

現在,當我點擊我想通過意向來打開片段查詢,而不是活動。 有什麼幫助嗎?

感謝 問候

+2

您無法打開新的片段。碎片需要始終由一個活動託管。 –

+0

所以我必須改變我的片段查詢到活動? – DEV

+0

你可以用新的替代之前的片段。 –

回答

0
listview = (ListView)findViewById(R.id.ListView); 
    QueryListAdapter adapter = new QueryListAdapter(this); 
    listview.setAdapter(adapter); 
    listview.setOnItemClickListener(new AdapterView.OnItemClickListener() { 
     @Override 
     public void onItemClick(AdapterView<?> parent, View view, 
           int position, long id) { 

      CustomListViewActivityQuery fragment2= new CustomListViewActivityQuery(); 
      Bundle bundle = new Bundle(); 
      FirstProjectApplication.query = FirstProjectApplication.allQueries.get(position); 
      bundle.putBoolean("query", true); 
      intent.putExtras(bundle); 
      fragment2.setArguments(bundle); 
      FragmentManager fragmentManager = getFragmentManager(); 
      FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction(); 
      fragmentTransaction.replace(R.id.fragment1, fragment2); 
      fragmentTransaction.addToBackStack(null); 
      fragmentTransaction.commit(); 
      } 
     }); 

指出,R.id.fragment1是你QueriesActivity框架佈局ID。

0

下面是一個簡單的例子,從文檔:

// Create fragment and give it an argument specifying the article it should show 
ArticleFragment newFragment = new ArticleFragment(); 
Bundle args = new Bundle(); 
args.putInt(ArticleFragment.ARG_POSITION, position); 
newFragment.setArguments(args); 

FragmentTransaction transaction = getSupportFragmentManager().beginTransaction(); 

// Replace whatever is in the fragment_container view with this fragment, 
// and add the transaction to the back stack so the user can navigate back 
transaction.replace(R.id.fragment_container, newFragment); 
transaction.addToBackStack(null); 

// Commit the transaction 
transaction.commit();