2016-04-29 16 views
8

「追加」ボタンをクリックすると、「add」ボタンとGridLayoutに6スロットがあり、view1がgridlayoutに追加されます。 'ボタンをもう一度押してください。ビューが特定の順序でビューを追加し、その順序を格納する

if (prefs.getString("text4", null) != null) { 
    Grid.addView(theLayout4); 
} 

if (prefs.getString("text5", null) != null) { 
    Grid.addView(theLayout5); 
} 
// each view has one EditText 

私の問題がある活動を再作成するとき、私は追加しVIEW1を削除した場合、彼らは自動的に、追加することができるように、そのテキストがすでにsharedPrefsに添加した場合、私がチェック追加されます後

if (!theLayout1.isShown()) { 
     Grid.addView(theLayout1); 
} else if (!theLayout2.isShown()) { 
     Grid.addView(theLayout2); 
} else if (!theLayout3.isShown()) { 
     Grid.addView(theLayout3); 
} ..... // this goes on 

それは私が望むように最後のスロットに配置されますが、アクティビティを再作成すると、最初の位置に戻ります。なぜなら、コードはその順序で読み込まれるため、最初の順序でビューが追加されるからです。

アクティビティを終了する前の順序でアクティビティを再作成するときにビューを追加したい場合、これは単純な論理的解決策を持っているかもしれません。あるいは、私はちょうどその問題に非常に間違っています。 !

答えて

1

問題は(少なくとも私があなたの質問から理解したところから)、親のビューの位置に関する何らかの種類のインジケータを環境設定に保存しないということです。今すぐ最初に追加することと、ビューを削除した後に再び追加することとを比較することと、ビューをスロット1とスロット5(例えば)に追加することとの間には違いはない。例えば

:これを処理する1つの方法は、これらのスロットごとに好みのエントリを持っているし、それらのスロット設定で表示インジケータを保存することです

// the base of the slot keys, there will be 6 of them from 1 to 6 inclusive 
private static final String KEY_SLOT = "key_slot"; 

// when adding the view for slot 2 you'll save in preferences text2 it's indicator 
prefEditor.putString(KEY_SLOT + position, text2); // position will be 2, the indicator of the slot in your layout 

// when you delete the view from a slot, nullify it's slot preferences 
// for example deleting the slot 3(child 3 in the layout) 
prefEditor.putString(KEY_SLOT + position, null); position will be 3 

アクティビティを再作成しますときすべてのSLOT設定を見て、どの値が保持されているかを確認してください。あなたのレイアウトが穴を持つことを許している場合(スロット2のビューを持たず、スロット1と3のビューを持っている場合など)、またはそれらのスロットに配置するビューを示すテキスト*変数の1つを保持できる場合、

// in the activity: 
// iterate over all the SLOT preferences 
for (int i = 1; i <= 6; i++) { 
    String text = prefs.getString(KEY_SLOT + i, null); 
    // if text is null there is no view stored for this slot 
    // keep in mind that you need to add some sort of empty widget to act as a child 
    // or modify the LayoutParams of the other children to take in consideration the empty space 
    // OR 
    // if text is one of your values, like text1, text2 etc 
    // then you know that at slot i you need to place the view corresponding to the text value 
} 
0

あなたの名前+レイアウトの名前を保存する必要があります。例: Layout1、LAYOUT2、layout3 ...

あなたが順序でそれらを取得したい場合は、あなたが取得するために何を持っていないまで、あなただけループする必要があります:

 int i = 1; 
     String valueStored = null; 
     do { 
      String key = "layout" + i; // layout1, layout2, layout3... 
      SharedPreferences preferences = activity.getSharedPreferences(key, 0); 
      valueStored = preferences.getString(key, null); 

      // View v = getViewByName(valueStored) get the view by the name retrieved from the shared pref 
      //Grid.addView(view); 
     } while (valueStored != null); // stop when there is nothing more to get 

はそれが役に立てば幸い!

+0

(int)iの順番でレイアウトを追加しませんか? (layout1、次にlayout2、次にlayout3、layout4 ...) –

+0

あなたが望む順序でレイアウトを追加することができます! 重要な部分は、順番に検索することだけです。 – GuillaumeAgis

1

これは、ソートされた配列に項目を挿入することになります。メソッドaddView(View child, int index)があり、使用したい位置にビューを追加します。現在追加されている子を反復処理し、新しい子を挿入する正しいインデックスを見つける必要があります。追加する前に各ビューにその値をタグ付けすると、タグを見て、このインデックスの位置を特定できます。あなたはレイアウトを挿入する必要がある場合

private void insertView(View theLayoutN, int n) { 
    // tag the new view with its value 
    theLayoutN.setTag(Integer.valueOf(n)); 

    // iterate over the views currently in the grid, keeping track 
    // of the desired insertion index 
    int insertionIndex = 0; 
    final int childCount = grid.getChildCount(); 
    for (int i = 0; i < childCount; i++) { 
     // get the child currently at index i 
     View child = grid.getChildAt(i); 

     // retrieve its tag, which is its value 
     int childN = ((Integer) child.getTag()).intValue(); 

     // if the child's value is greater than the value of the 
     // one we're inserting, then we have found the insertionIndex 
     if (childN > n) { 
      break; 
     } 

     // increment the insertionIndex 
     insertionIndex++; 
    } 

    grid.addView(theLayoutN, insertionIndex); 
} 

その後、あなたはこれを行うことができます。

if (!theLayout1.isShown()) { 
    insertView(theLayout1, 1); 
} else if (!theLayout2.isShown()) { 
    insertView(theLayout2, 2); 
} else if (!theLayout3.isShown()) { 
    insertView(theLayout3, 3); 
} 

これは、あなたの意見が正しい順序で常にあることを確認する必要があります。バイナリ検索を実装することでさらに最適化することができますが、多くのビューを持つ場合は、ListViewを使用する必要があります。

ただし、ループやデータ構造で単純化できる冗長なコードがたくさんあるので、このアーキテクチャ全体を再検討することをお勧めします。また、いつでもすべてのビューを追加することができます。表示したいときにはtheLayout.setVisibility(View.GONE)と非表示にする場合はtheLayout.setVisibility(View.VISIBLE)を呼び出してください。

+0

ビューを削除した後に追加しようとするとNullPointerが発生する: 'java.lang.NullPointerException:仮想メソッドを呼び出そうとしている 'int null java.lang.Integer.intValue()'ヌルオブジェクトリファレンスで' –

+0

ビューにタグセットがないことを意味します。これはおそらく、 'insertView'を使わずにそのビューをコンテナに直接追加したからです。 – Jschools

+0

私は 'insertView'を使用しました –

1

私は、ビューを追加JSON配列でそのテキストを追加し、文字列として共有好みに保管するたびにJsonArray

を使用してこのような事を処理します。

jsonarray.toString() 

あなたの意見は、削除または追加取得するとあなたは簡単にアイテムを追加および削除することができます。また、これにより、共有プリファレンスを1つしか持たず、異なる共有プリファレンスで作業できなくなります。

SharedPreferences sf = ..... 
String txt=sf.getString("YOUR_KEY",""); 
JsonArray myViewArray=new JsonArray(txt); 
for(int i=0;i<myViewArray.length();i++){ 
    // ADD VIEW HERE 
} 
2

まず第一には、あなたが「あなたがそれらをマークするものは何でもまたは」ビュータグを格納することができ、タグやビューがであるという立場を持つデータ構造で、その後、SharedPreferencesのコードを格納します。

public void savePreferences() { 
     SharedPreferences mPrefs = PreferenceManager.getDefaultSharedPreferences(activity); 
     SharedPreferences.Editor prefsEditor = mPrefs.edit(); 
     Gson gson = new Gson(); 
     String json = gson.toJson(dataStructure, new TypeToken<DataStructure>() { 
     }.getType()); 
     prefsEditor.putString("ordering", json); 
     prefsEditor.commit(); 
    } 

    public static DataStructure loadPreferences() { 
     SharedPreferences mPrefs = PreferenceManager.getDefaultSharedPreferences(activity); 
     Gson gson = new Gson(); 
     String json = mPrefs.getString("ordering", ""); 
     if (!json.equals("")) { 
      return (DataStructure) gson.fromJson(json, new TypeToken<DataStructure>() { 
      }.getType()); 
     } else { 
      return new DataStructure(); 
     } 
    } 

前述したように、あなたがデータ構造を取得するときに、次のコードを使用して、それらの位置に基づいて結果を並べ替えることができます:

Arrays.sort(dataStructure , new Comparator<DataStructureElement>() { 
    @Override 
    public int compare(DataStructureElement o1, DataStructureElement o2) { 
     return Integer.compare(o1.position, o2.position); 
    } 
}); 

そして、データ構造にソート結果を反復処理し、追加します通常はGridLayoutへのビューを順番に並べ替えます。

関連する問題