保持列表视图值

时间:2018-04-24 08:03:17

标签: android listview

在我看来,屏幕分为2个部分,左边是商品的品牌列表,在右边按品牌有一个商品清单,带有输入数量的字段,我需要实现在品牌之间转换时保存输入的数据。我无法弄清楚如何做到这一点。感谢

2 个答案:

答案 0 :(得分:0)

我相信你有一个输入的editText。

yourEditText.addTextChangedListener(new TextWatcher() {

    public void afterTextChanged(Editable s) {}

    public void beforeTextChanged(CharSequence s, int start,
        int count, int after) {

    }

    public void onTextChanged(CharSequence s, int start,
      int before, int count) {
        String yourValueToSave=yourEditText.getText().ToString();
    }
});

这是最简单的方法之一。每次用户更改editText的值时,它都将保存在“yourValueToSave”变量中。

如果没有代码片段或有关您已尝试过的详细信息,我可以做到最好。

答案 1 :(得分:0)

你可以这样使用:as main_activity.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:weightSum="1">

    <FrameLayout
        android:id="@+id/flFragmentBrandsList"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="0.4" />

    <FrameLayout
        android:id="@+id/flFragmentBrandsDetails"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="0.6" />

</LinearLayout>

<强> fragment_brands_list.xml

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <ListView
        android:id="@+id/ivBrands"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</FrameLayout>

<强> fragment_brands_details.xml

//品牌细节的用户界面

MainActivity.java onCreate()方法

fragmentTransaction = getSupportFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.flFragmentBrandsList, new BrandsListFragment());
fragmentTransaction.replace(R.id.flFragmentBrandsDetails, new BrandsListDetails());
fragmentTransaction.commitAllowingStateLoss();

BrandsListFragment.java onCreateView()方法

 View view = inflater.inflate(R.layout.fragment_brands_list, container, false);

        brandsList = getResources().getStringArray(R.array.brandsList);
        ivBrands = (ListView) view.findViewById(R.id.ivBrands);
        adapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1, brandsList);
        ivBrands.setAdapter(adapter);

        ivBrands.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                BrandsListDetails.setImageResource(position);
            }
        });

        return view;

BrandsListDetails.java onCreateView()方法

{
View view = inflater.inflate(R.layout.fragment_brands_details, container, false);   
return view;
}
public static void setImageResource(int position) {
        switch (position) {
            case 0:
                // Display First Brand Details
                break;
            case 1:
                // Display Second Brand Details
                break;
            case 2:
                // Display Third Brand Details
                break;

        }
    }