如何保存每个项目的明细清单?

时间:2017-01-22 11:21:48

标签: android android-layout listview android-fragments

我使用Android Studio制作管理我的销售的应用程序,我使用销售清单,如果我们点击某个销售,这将导航我们自己销售(另一个活动)

在销售细节活动中,我使用一个添加我们写的文本的按钮。

例如:写文字" 1 blue castro Tshirt"在编辑文本中单击添加5次将生成一个包含5行此字符串的列表。

问题在于保存每笔销售的详细信息。

如何保存每笔销售的详细信息,以便我可以重新启动应用程序并查找每个销售及其详细信息?

细节销售活动的Java代码:

import...

public class ItemDetailActivity extends ListActivity {


private ArrayList<String> list = new ArrayList<String>();
private ArrayAdapter<String> adapter;

@Override
public void onCreate(final Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_item_detail);
    final Button btn = (Button) findViewById(R.id.btnAdd);
    adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, list);

    final OnClickListener listener = new OnClickListener() {
    @Override
    public void onClick(View v) {
            EditText edit = (EditText) findViewById(R.id.txtItem);
            list.add(edit.getText().toString());
            edit.setText("");
            adapter.add(edit.getText().toString());
            adapter.notifyDataSetChanged();




}   
};



    btn.setOnClickListener(listener);
    setListAdapter(adapter);
}

项目列表(销售清单)活动的Java代码:

package com.galilsoftware.myapplication;

import...

public class ItemListActivity extends AppCompatActivity {

private boolean mTwoPane;

@Override
protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_item_list);

    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    Button AddCust;
    setContentView(R.layout.activity_item_list);
    AddCust = (Button)findViewById(R.id.AddCust);


    AddCust.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG).setAction("Action", null).show();

        }
    }
);

    View recyclerView = findViewById(R.id.item_list);
    assert recyclerView != null;
    setupRecyclerView((RecyclerView) recyclerView);

    if (findViewById(R.id.item_detail_container) != null) {

        mTwoPane = true;
    }
}

private void setupRecyclerView(@NonNull RecyclerView recyclerView) {
    recyclerView.setAdapter(new SimpleItemRecyclerViewAdapter(DummyContent.ITEMS));
}

public class SimpleItemRecyclerViewAdapter
        extends RecyclerView.Adapter<SimpleItemRecyclerViewAdapter.ViewHolder> {

    private final List<DummyContent.DummyItem> mValues;

    public SimpleItemRecyclerViewAdapter(List<DummyContent.DummyItem> items)     {
        mValues = items;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.item_list_content, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(final ViewHolder holder, int position) {
        holder.mItem = mValues.get(position);
        holder.mIdView.setText(mValues.get(position).id);
        holder.mContentView.setText(mValues.get(position).content);

        holder.mView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mTwoPane) {
                    Bundle arguments = new Bundle();
                    arguments.putString(ItemDetailFragment.ARG_ITEM_ID, holder.mItem.id);
                    ItemDetailFragment fragment = new ItemDetailFragment();
                    fragment.setArguments(arguments);
                    getSupportFragmentManager().beginTransaction()
                            .replace(R.id.item_detail_container, fragment)
                            .commit();
                } else {
                    Context context = v.getContext();
                    Intent intent = new Intent(context, ItemDetailActivity.class);
                    intent.putExtra(ItemDetailFragment.ARG_ITEM_ID, holder.mItem.id);

                    context.startActivity(intent);
                }
            }
        });
    }

    @Override
    public int getItemCount() {
        return mValues.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        public final View mView;
        public final TextView mIdView;
        public final TextView mContentView;
        public DummyContent.DummyItem mItem;

        public ViewHolder(View view) {
            super(view);
            mView = view;
            mIdView = (TextView) view.findViewById(R.id.id);
            mContentView = (TextView) view.findViewById(R.id.content);
        }

        @Override
        public String toString() {
            return super.toString() + " '" + mContentView.getText() + "'";
        }
    }
}
}

XML for sale Detail:

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

    <EditText
        android:id="@+id/txtItem"
        android:layout_width="240dp"
        android:layout_height="wrap_content"
        android:inputType="text"
        android:hint="@string/hintTxtItem"
        />

    <Button
        android:id="@+id/btnAdd"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/lblBtnAdd"
        android:layout_toRightOf="@id/txtItem"
        />

    <TextView
        android:id="@android:id/empty"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/txtItem"
        android:text="@string/txtEmpty"
        android:gravity="center_horizontal"
        />

    <android.support.v7.widget.Toolbar
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="?attr/colorPrimary"
        android:theme="?attr/actionBarTheme"
        android:minHeight="?attr/actionBarSize"
        android:id="@+id/toolbar2"
        android:layout_below="@+id/txtItem"
        android:layout_alignParentStart="true" />

    <ListView
        android:id="@android:id/list"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/toolbar2"
        android:layout_alignParentStart="true" />

</RelativeLayout>

销售清单的XML:

    <?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:context="com.galilsoftware.a415hgufhdcgh.ItemListActivity">

    <android.support.design.widget.AppBarLayout
        android:id="@+id/app_bar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/AppTheme.AppBarOverlay">

        <Button
            android:text="הוספת לקוח"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/AddCust"
            android:layout_weight="1" />

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            app:popupTheme="@style/AppTheme.PopupOverlay" />

    </android.support.design.widget.AppBarLayout>

    <FrameLayout
        android:id="@+id/frameLayout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior">

        <include layout="@layout/item_list" />
    </FrameLayout>




</android.support.design.widget.CoordinatorLayout>

非常感谢。

2 个答案:

答案 0 :(得分:1)

您可以使用Sqlite数据库或共享首选项来保存销售详情。这样您就可以从共享首选项或Sqlite数据库中检索数据供以后使用。 希望这有帮助。

答案 1 :(得分:0)

您可以创建一个模型类来存储详细信息,并使其像Parceable一样:

public class Sale implements Parcelable{
String name;
String details;

protected Sale(Parcel in) {
    name = in.readString();
    details = in.readString();
}

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

public String getDetails() {
    return details;
}

public void setDetails(String details) {
    this.details = details;
}

public static final Creator<Sale> CREATOR = new Creator<Sale>() {
    @Override
    public Sale createFromParcel(Parcel in) {
        return new Sale(in);
    }

    @Override
    public Sale[] newArray(int size) {
        return new Sale[size];
    }
};

@Override
public int describeContents() {
    return 0;
}

@Override
public void writeToParcel(Parcel parcel, int i) {
    parcel.writeString(name);
    parcel.writeString(details);
}

}

现在,您可以使用GSON在共享首选项中存储和检索此ArrayList,它可以像gradle一样添加到gradle中:

 compile 'com.google.code.gson:gson:2.2.+'

在首选项中存储ArrayList:

ArrayList<Sale> list=new ArrayList<>();

在此处添加您的促销及其详细信息.. 将其保存为首选项的字符串。

Gson gson = new Gson();
String json = gson.toJson(list);
Utils.storeUserPreferences(context,"LIST",json);

要检索此ArrayList, 首先从首选项中反转为字符串,然后在Gson

的帮助下将其转换为List
Gson gson = new Gson();
String json = Utils.getUserPreferences(context,"LIST");
Type type = new TypeToken<List<Sale>>(){}.getType();
final List<Sale> list=gson.fromJson(json, type);