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

时间:2013-12-10 11:12:49

标签: android listview android-fragments

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

点击搜索按钮后,我正在填充列表视图,其中搜索结果位于按钮下方。在列表项单击时,我使用下面的代码使用bundle to fragment 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中显示搜索结果按钮。当我点击列表项时,我将使用该包将详细信息传递给下一个片段,如上所示。当我从详细信息片段到前一个片段按回按钮回来时,列表消失了。

2 个答案:

答案 0 :(得分:0)

尝试使用ID:ListViewandroid.R.id.list)定义android:id="@android:id/list",并将Fragment设为ListFragmentclass FragmentA extends ListFragment) 。然后,您可以让listview调用方法getListView()并填充它。您也可以定义适配器,调用setListAdapter()

我遇到过这些问题,但自从我做到了这一点以来,我从来没有遇到过关于列表的问题。

答案 1 :(得分:0)

你可以做到

editTextName.setText("");

在片段A中的onResume()方法

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

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