Intent的Android Open片段

时间:2015-12-10 09:53:16

标签: android android-fragments android-intent

我开发了带有查询片段的应用程序Android,该查询片段有3个步骤,最后可以通过点击保存所有参数查询,之后当我转到已保存查询的片段时如果我单击我打开显示的活动直接查询的结果这是我的代码:

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);
        }
    });

现在当我点击我想打开片段查询而不是Intent的活动。 有什么帮助吗?

由于 此致

2 个答案:

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

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