2017-03-25 3 views
0

私の経費を数えるアプリケーションを作成したいと思います。私は旅行のための配列リストを作成しました。そして今、カテゴリーの配列リストと各カテゴリーの別の配列リストを作成したいと思います。たとえばtrip1には3つのカテゴリがあります:a、b、c。すべてのカテゴリで、私は自分の経費を持っています。どのように私はトリップ配列リストの各値の配列リストを作るのですか?アンドロイドスタジオの別の配列リストの各値の配列リストを作成する方法

+1

あなたのリストの内容についてもっと明確にしてください...あなたは例を挙げることができますか? – User9123

+0

すべての旅行のアレイリストと、カテゴリのアレイリストと、費用のアレイリストが必要です。例えば、私はリンゴを食べるとリンゴは10ドルのコストがかかるので、私はカテゴリーの食べ物とリンゴに追加したい、10。私は各旅行のカテゴリと費用の配列のリストが必要です。 –

答えて

0

データベースのような構造を作成しています。ここにいくつかのヒントがあります:

1)構造に矛盾が生じやすいので、データを重複させたくありません。
これは意味:expansesリストとcategories[cat]リスト

2の両方で、あなたの広がりを追加しないでください)たぶん、いくつかのMaps

3を使用)私が行っていたデータに

を集約するために、いくつかのクラスを使用しますこれは次のようになります:

import java.util.*; 

public class Trip { 

    /** An item in the expanses list */ 
    static class Item { 
     final String item; 
     final int cost; 
     public Item(String item, int cost) { 
      this.item = item; 
      this.cost = cost; 
     } 
     @Override public String toString() { 
      return this.item + " (" + this.cost + "$)"; 
     } 
    } 

    /** A map with category as key and the associed list of items as value */ 
    Map<String,List<Item>> expanses; 


    public Trip() { 
     this.expanses = new HashMap<String,List<Item>>(); 
     String[] categories = {"food","clothes","souvenir"}; 
     for (String cat: categories) { // init the categories with empty lists 
      this.expanses.put(cat, new ArrayList<Item>()); 
     } 
    } 



    /** Register a new expanse to the trip. */ 
    public void add(String item, int cost, String category) { 
     List<Item> list = this.expanses.get(category); 
     if (list == null) 
      throw new IllegalArgumentException("Category '"+category+"' does not exist."); 
     list.add(new Item(item, cost)); 
    } 

    /** Get the expanses, given a category. 
    * @return a fresh ArrayList containing the category elements, or null if the category does not exists 
    */ 
    public List<Item> getItems(String category) { 
     List<Item> list = this.expanses.get(category); 
     if (list == null) 
      return null; 
     return new ArrayList<Item>(list); 
    } 

    /** Get the expanses, given a category. 
    * @return a fresh ArrayList containing all the elements 
    */ 
    public List<Item> getItems() { 
     List<Item> list = new ArrayList<Item>(); 
     for (List<Item> l: this.expanses.values()) // fill with each category items 
      list.addAll(l); 
     return list; 
    } 

    /** Get the total cost, given a category. */ 
    public int getCost(String category) { 
     List<Item> list = this.expanses.get(category); 
     if (list == null) 
      return -1; 
     int cost = 0; 
     for (Item item: list) 
      cost += item.cost; 
     return cost; 
    } 

    /** Get the total cost. */ 
    public int getCost() { 
     int cost = 0; 
     for (List<Item> l: this.expanses.values()) 
      for (Item item: l) 
       cost += item.cost; 
     return cost; 
    } 


    // lets test this structure 
    public static void main(String[] args) { 

     Trip trip1 = new Trip(); 

     trip1.add("apple", 10, "food"); 
     trip1.add("cheese", 15, "food"); 
     trip1.add("hat", 30, "clothes"); 


     System.out.println(trip1.getItems("food")); 
     System.out.println(trip1.getCost("food")); 
     System.out.println(); 
     System.out.println(trip1.getItems()); 
     System.out.println(trip1.getCost()); 

    } 

} 
+0

しかし、私は最初のアクティビティで旅行をしたいが、2番目のアクティビティで経費を追加したいのですが、これをどうやって行うのですか? –

+0

と、最初のアクティビティでリストビューが必要なので、どのようにすべての旅行の配列リストを作成しますか? –

+0

アクティビティとは何ですか?カテゴリを意味しますか? – User9123

0

は旅行(それに関連したそれらの値を持つ)オブジェクトは、その後、リストの中に代わりのように多くのリスト

1

を作るあなたはいくつかのクラス

class Trip { 
    private ArrayList[Category] categories = new ArrayList(); 

    public void addCategory(Category c) { 
     categories.add(c); 
    } 
    public void getCategory(int index) { 
     categories.get(index); 
    } 
} 

class Category { 
    private ArrayList[Expense] expenses = new ArrayList(); 

    public void addExpense(Expense e) { 
     expenses.add(e); 
    } 
} 


class Expense { 
    private String name; 
    private int amount; 

    public Expense(name, amount) { 
     this.name = name; 
     this.amount = amount; 
    } 
} 

を持っている必要があることを入れて作ってみますあなたのコードの他の部分で、あなたはあなたがTrip内部カテゴリのあなたのArrayListがになりたいかもしれません

Trip africa = new Trip(); 
africa.addCategory(new Category()); 
africa.getCategory(0).addExpense(new Expense("apple", 10)); 

のようなものを行うことができますの場合、カテゴリに名前を付けることができます。

+0

は 'put'によって' add'を意味しますか? – User9123

+0

しかし、私はユーザーが名前を選ぶことを望みます。編集テキストと文字列を作成すると、文字列はetNewTrip.getText()。toString()と等しくなります。 トリップ文字列= new Trip(); しかし、私は "変数はすでにスコープ内に定義されています"というエラーが出るので、どうすれば修正できますか? –

+0

このエラーは、コード内に変数 'string'をすでに定義していることを意味します。別の名前を使用する必要があります。 – TuomasK

関連する問題