将数据从ListView保存到文件

时间:2016-09-22 21:28:21

标签: java android file listview arraylist

我一直在研究如何将数据从 ListView 保存到文件,但我无法找到与我的问题有关的任何实质内容。请任何人帮助我完成这一点,非常感谢。

我正在尝试将 ListView 的所有用户存储内容写入存储在内部存储器中的文件

[P.S]忽略我试图写入文件的代码的基础,原来是垃圾lol

请参阅下面的示例代码。

public class ShoppingList extends Fragment {

public static final String COUNT = "count";
public static final String BTAX = "btax";
public static final String ATAX = "atax";
private static List<ListItems> myItems = new ArrayList<ListItems>();
private static ListView listView;
private static ImageView cart;
private static RelativeLayout top,bottom;
private static ArrayAdapter<ListItems> adapter;
private static double newbTotal, newaTotal;
private static int count = 0;
String Tag;
View gView;
private static TextView Btotal,Atotal,itemCount;

DecimalFormatSymbols symbols = DecimalFormatSymbols.getInstance(Locale.US);
DecimalFormat decimalFormat = new DecimalFormat("###,###.##", symbols);

public ShoppingList() {
    // Required empty public constructor
}

public void setCart(String itemName, int qty, double beforeTax, double afterTax, double bTotal, double aTotal)
{
    if(!itemName.isEmpty())
    {
        newbTotal = newbTotal + bTotal;
        newaTotal = newaTotal + aTotal;
        cart.setVisibility(View.GONE);
        top.setVisibility(View.VISIBLE);
        bottom.setVisibility(View.VISIBLE);
        listView.setVisibility(View.VISIBLE);
        Btotal.setText(decimalFormat.format(newbTotal));
        Atotal.setText(decimalFormat.format(newaTotal));
        count = count + 1;
        if(count == 1)
        {
            itemCount.setText("Item("+count+")");
        }
        else
        {
            itemCount.setText("Items("+count+")");
        }

        myItems.add(new ListItems(R.drawable.thrash, itemName, qty, beforeTax, afterTax));
        adapter.notifyDataSetChanged();
    }
    else{
        Log.e(Tag,"Cart is Null");
        cart.setVisibility(View.VISIBLE);
    }
}

public void populatelistview() {
        adapter = new MyListAdapter();
        listView = (ListView) gView.findViewById(R.id.Shopping_listView);
        listView.setAdapter(adapter);
        adapter.notifyDataSetChanged();
    }

private class MyListAdapter extends ArrayAdapter<ListItems> {
    public MyListAdapter() {
        super(getActivity(), R.layout.custom_row, myItems);
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        //Ensures we have a view to work with (may have been given null)
        View itemView = convertView;
        if (itemView == null) {
            itemView = getActivity().getLayoutInflater().inflate(R.layout.custom_row, parent, false);
        }

        //Finds the item to work with
        ListItems currentitem = myItems.get(position);

        //Fills the view
        ImageView imageView = (ImageView) itemView.findViewById(R.id.Thrash);
        imageView.setImageResource(currentitem.getIconID());

        //Item Name
        TextView name = (TextView) itemView.findViewById(R.id.ItemName);
        name.setText(currentitem.getItemName());

        //Item Qty
        TextView qty = (TextView) itemView.findViewById(R.id.QuantityValue);
        qty.setText(""+currentitem.getQty());

        //Before Tax
        TextView btax = (TextView) itemView.findViewById(R.id.BeforeTaxValue);
        btax.setText(decimalFormat.format(currentitem.getBeforeTax()));

        //After Tax
        TextView atax = (TextView) itemView.findViewById(R.id.AfterTaxValue);
        atax.setText(decimalFormat.format(currentitem.getAfterTax()));

        imageView.setTag(currentitem);

        imageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ListItems item = myItems.get(position);
                myItems.remove(item);
                newbTotal = newbTotal - item.getBeforeTax();
                newaTotal = newaTotal - item.getAfterTax();
                count = count - 1;
                Btotal.setText(decimalFormat.format(newbTotal));
                Atotal.setText(decimalFormat.format(newaTotal));
                if(count == 1)
                {
                    itemCount.setText("Item("+count+")");
                }
                else
                {
                    itemCount.setText("Items("+count+")");
                }
                if(count == 0){
                    cart.setVisibility(View.VISIBLE);
                    top.setVisibility(View.GONE);
                    bottom.setVisibility(View.GONE);
                    listView.setVisibility(View.GONE);
                }
                notifyDataSetChanged();
            }
        });
        return itemView;
    }
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {

    super.onOptionsItemSelected(item);

    switch(item.getItemId()){
        case R.id.Receipt:
            try {
                FileOutputStream fos = getContext().openFileOutput("Receipt.txt", Context.MODE_PRIVATE);
                OutputStreamWriter osw = new OutputStreamWriter(fos);
                try{
                    //osw.write();
                    osw.flush();
                    osw.close();
                    Toast.makeText(getContext(), "Receipt Saved Successfully", Toast.LENGTH_SHORT).show();
                }
                catch (IOException e){
                    e.printStackTrace();
                }
            }
            catch (IOException e){
                e.printStackTrace();
            }
            break;
    }
    return true;
}
}

0 个答案:

没有答案