Android 如何在从另一个片段返回时保留搜索到的数据

Android 如何在从另一个片段返回时保留搜索到的数据,android,listview,android-fragments,Android,Listview,Android Fragments,我有两个片段(A和B)。在片段A中,我有两个编辑框和一个搜索按钮 单击“搜索”按钮后,我将在按钮下方填充一个列表视图,其中包含搜索结果。在单击列表项时,我使用以下代码使用bundle将值传递给片段B: FragmentB details = new FragmentB(); Bundle detailsBundle = new Bundle(); detailsBundle.putString("ProdCode", _prodCode); detailsBundle.putString("

我有两个片段(A和B)。在片段A中,我有两个编辑框和一个搜索按钮

单击“搜索”按钮后,我将在按钮下方填充一个列表视图,其中包含搜索结果。在单击列表项时,我使用以下代码使用bundle将值传递给片段B:

FragmentB details = new FragmentB();

Bundle detailsBundle = new Bundle();

detailsBundle.putString("ProdCode", _prodCode);
detailsBundle.putString("ProdName", _prodName);
detailsBundle.putString("UnitPrice", _unitPrice);
detailsBundle.putString("StockValue", _stockValue);
detailsBundle.putString("Indicator", _indicator);
detailsBundle.putString("OpenOrderValue", _openOrderValue);
detailsBundle.putString("OpenInvoiceValue", _openInvoiceValue);

details.setArguments(detailsBundle);


FragmentTransaction ft = getFragmentManager().beginTransaction();
ft.replace(R.id.container, details, "Details");
ft.addToBackStack(null);
ft.commit();
但当我点击片段B上的后退按钮时,片段A中填充的列表消失了。如何在listview中保留搜索结果,以及如何在从片段B返回到片段A时清除编辑框中的文本

[编辑]

public class MainActivity extends Activity implements OnClickListener
{

private Button button1, button2, button3, button4;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main_activity);

        button1 = (Button) findViewById(R.id.btnFrgament1);
        button2 = (Button) findViewById(R.id.btnFrgament2);
        button3 = (Button) findViewById(R.id.btnFrgament3);
        button4 = (Button) findViewById(R.id.btnFrgament4);


        FragmentManager fm = getFragmentManager();
        FragmentTransaction ft = fm.beginTransaction();
        FirstFragment initialFragment = new FirstFragment();
        ft.add(R.id.containerLinearLayout, initialFragment);
        ft.commit();


        button1.setOnClickListener(this);
        button2.setOnClickListener(this);
        button3.setOnClickListener(this);
        button4.setOnClickListener(this);
}

@Override
    public void onClick(View v) {

        Fragment newFragment;

        int id = v.getId();

        switch(id)
        {
        case R.id.btnFragment1:

        newFragment = new FirstFragment();

        break;

        case R.id.btnFragment2:

        newFragment = new SecondFragment();

        break;

        case R.id.btnFragment3:

        newFragment = new ThirdFragment();

        break;

        case R.id.btnFragment4:

        newFragment = new FourthFragment();

        break;

        default:            
            newFragment = new FirstFragment();          
            break;

        }

        FragmentTransaction transaction = getFragmentManager().beginTransaction();
        transaction.replace(R.id.containerLinearLayout, newFragment);
        transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
        transaction.commit();


        }

SecondFragment有两个编辑框和一个按钮(搜索)。我正在按钮下方的listview中显示搜索结果。当我单击列表项时,我正在使用如上所示的包将详细信息传递给下一个片段。当我从细节片段返回到上一个片段时,列表消失了。

尝试用id:
android.R.id.list
android:id=“@android:id/list”
)定义你的
ListView
,并使你的
片段成为
ListFragment
class FragmentA扩展了ListFragment
)。然后,您可以让listview调用方法
getListView()
并填充它。您还可以定义适配器,调用
setListAdapter()

我曾经遇到过这样的问题,但自从我做了这件事以后,我从来没有遇到过关于列表的问题。

你可以这样做

editTextName.setText("");
在片段A中的onResume()方法中

同样在onResume()中,像这样填充数据并调用listAdapter

adapter = new ExampleListAdapter(getActivity());
Listv.setAdapter(adapter);

不确定,因此将其保留为注释。1)将EditText作为字段变量,并在onCreate中执行setRatainInstance(true)。这应该(我认为)保留您存储的任何值(当然,除非您在添加时实例化了一个全新的片段)。2) 您可以在返回时在B中的EditText上使用setText(“”),或者在onCreateViewsHow中,您的片段是实现吗?我会回答你,因为我想我知道怎么回事,但为了确保我需要实现。@Renanbandera我已经将我的活动分为两部分。在左边我有4个按钮。点击按钮,我打开右侧的相应片段。我已经发布了我的代码。请查收。现在在右边,我有两个编辑框和一个按钮(搜索)。我正在按钮下方的列表视图中显示搜索结果。在列表项上单击“我正在使用bundle将详细信息传递给下一个片段”,如查询中所示。当我从细节片段返回到上一个片段时,列表就消失了。老实说,我希望看到的是片段代码,而不是活动代码。Renan,我使用的是自定义数组适配器。适配器的类型无关紧要
ArrayAdapter…
然后,
setListAdapter(adapter)
这是一种很好的方法,因为listfragment知道您正在处理列表。