2017-08-14 16 views
0

私は追加可能な拡張可能なリストビューを持っています。どのように私は追加されたすべてのアイテムを保存することができます。 gsonとjsonのsharedpreferencesを使ってみましたが、うまくいかないようです。ここでアンドロイドに展開可能なリストビューを保存するには?

public class ExpandListAdapter extends BaseExpandableListAdapter { 
    private Context context; 
    private ArrayList<Group> groups; 

    public ExpandListAdapter(Context context, ArrayList<Group> groups) { 
     this.context = context; 
     this.groups = groups; 
    } 

    @Override 
    public Object getChild(int groupPosition, int childPosition) { 
     ArrayList<Child> chList = groups.get(groupPosition).getItems(); 
     return chList.get(childPosition); 
    } 

    @Override 
    public long getChildId(int groupPosition, int childPosition) { 
     return childPosition; 
    } 

    @Override 
    public View getChildView(int groupPosition, int childPosition, 
         boolean isLastChild, View convertView, ViewGroup parent) { 

     Child child = (Child) getChild(groupPosition, 
        childPosition); 
     if (convertView == null) { 
      LayoutInflater infalInflater = (LayoutInflater) context 
        .getSystemService(context.LAYOUT_INFLATER_SERVICE); 
      convertView = infalInflater.inflate(R.layout.child_row, null); 
     } 
     TextView tv = (TextView) convertView.findViewById(R.id.country_name); 
     tv.setText(child.getName().toString()); 

     return convertView; 

    } 

    @Override 
    public int getChildrenCount(int groupPosition) { 
     ArrayList<Child> chList = groups.get(groupPosition) 
       .getItems(); 

     return chList.size(); 
    } 

    @Override 
    public Object getGroup(int groupPosition) { 
     return groups.get(groupPosition); 
    } 

    @Override 
    public int getGroupCount() { 
     return groups.size(); 
    } 

    @Override 
    public long getGroupId(int groupPosition) { 
     return groupPosition; 
    } 

    @Override 
    public View getGroupView(int groupPosition, boolean isExpanded, 
          View convertView, ViewGroup parent) { 
     Group group = (Group) getGroup(groupPosition); 
     if (convertView == null) { 
      LayoutInflater inf = (LayoutInflater) context 
        .getSystemService(context.LAYOUT_INFLATER_SERVICE); 
      convertView = inf.inflate(R.layout.group_header, null); 
     } 
     TextView tv = (TextView) convertView.findViewById(R.id.group_name); 
     tv.setText(group.getName()); 
     return convertView; 
    } 

    @Override 
    public boolean hasStableIds() { 
     return true; 
    } 

    @Override 
    public boolean isChildSelectable(int groupPosition, int childPosition) { 
     return true; 
    } 

拡張可能なリストビューを使用するクラスは、私はAlertDialogとグループを追加することができますされ、それはまた、単にテストの理由から、「ヨ」の名前で子を追加します。ここに私の拡張可能なリストビュー・アダプタは、次のとおりです。

public class classesScreen extends Activity implements AdapterView.OnItemSelectedListener { 
private ExpandListAdapter ExpAdapter; 
private ExpandableListView ExpandList; 
private Button newHeaderBut; 
static ArrayList<Group> group_list; 
ArrayList<Child> child_list; 
String m_text; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_classes); 

    group_list = new ArrayList<Group>(); 

    ExpandList = (ExpandableListView) findViewById(R.id.classesListView); 
    ExpAdapter = new ExpandListAdapter(this, group_list); 
    ExpandList.setAdapter(ExpAdapter); 


    newHeaderBut = (Button) findViewById(R.id.newClassBut); 

    newHeaderBut.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 

      AlertDialog.Builder builder = new AlertDialog.Builder(classesScreen.this); 
      builder.setTitle("Add a New Class:"); 

      final EditText input = new EditText(classesScreen.this); 

      builder.setView(input); 

      builder.setPositiveButton("OK", new DialogInterface.OnClickListener() { 
       @Override 
       public void onClick(DialogInterface dialog, int which) { 

        m_text = input.getText().toString(); 

        Group gru1 = new Group(); 
        gru1.setName(m_text); 

        child_list = new ArrayList<Child>(); 

        Child ch1 = new Child(); 
        ch1.setName("Yo"); 

        child_list.add(ch1); 

        gru1.setItems(child_list); 

        group_list.add(gru1); 
        ExpAdapter.notifyDataSetChanged(); 
       } 
      }); 

      builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() { 
       @Override 
       public void onClick(DialogInterface dialog, int which) { 
        dialog.cancel(); 
       } 
      }); 
      builder.show(); 
     }); 
    } 

    public void onItemSelected(AdapterView<?> parent, View view, int position, long id){} 

    public void onNothingSelected(AdapterView<?> arg0) {} 
} 

また、グループと子という2つのオブジェクトクラスがあります。私はあなたがそれらを必要とする場合、それらを表示することができます。前もって感謝します!

+1

からデータを取得し、ファイルに

private void saveDataToFile() { FileOutputStream fileOutputStream = null; try { fileOutputStream = getContext().openFileOutput("fileName", Context.MODE_PRIVATE); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (NullPointerException e) { } ObjectOutputStream objectOutputStream = null; try { objectOutputStream = new ObjectOutputStream(fileOutputStream); } catch (IOException e) { e.printStackTrace(); } catch (NullPointerException e) { } try { if (objectOutputStream != null) { objectOutputStream.writeObject(yourArrayList); //which data u want to save } } catch (IOException e) { e.printStackTrace(); } try { if (objectOutputStream != null) { objectOutputStream.close(); } } catch (IOException e) { e.printStackTrace(); } } 

を最新のArrayListを保存ウルモデルクラスがSerializableを

を実装してください "のAndroidに拡張可能なリストビューを保存する方法は?" - あなたはそうしない。 'ExpandableListView'を設定するためのモデルデータを保存します。あなたのケースでは、それは 'Group'と' Child'オブジェクトを保存しています。 「私はgsonとjsonのsharedpreferencesを使ってみましたが、うまくいかないようです」 - 私たちが見ることのできないコードを手助けすることはできません。 'SharedPreferences'は奇妙な選択です。普通のJSONファイル(例: 'getFilesDir()')にデータを保存することもできます。 – CommonsWare

+0

gsonとsharedpreferencesを使用して保存できなかった理由を指定できますか? – ravindu1024

+0

@CommonsWareでは、グループオブジェクトと子オブジェクトを保存してからそれらを取得し、展開可能なリストに再度データを設定する方法を教えてください。 –

答えて

0

はファイル

private void getDataFromFile() { 

    FileInputStream fileInputStream = null; 
    try { 
     fileInputStream = getContext().openFileInput("fileName"); 
    } catch (FileNotFoundException e) { 
     e.printStackTrace(); 
     return; 
    } 
    ObjectInputStream objectInputStream = null; 
    try { 
     objectInputStream = new ObjectInputStream(fileInputStream); 
    } catch (IOException |NullPointerException e) { 
     e.printStackTrace(); 
    } 
    try { 
     yourArrayList = (ArrayList<YourClassName>) objectInputStream.readObject(); 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } catch (ClassNotFoundException e) { 
     e.printStackTrace(); 
    } 
    try { 
     objectInputStream.close(); 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } 

} 
+0

どうすればこれらのメソッドを呼び出すことができますか? –

+0

あなたがリスト呼び出しsaveDataToFile()を保存する必要があるとき。あなたがデータ呼び出しを取得する必要があるときgetDataFromFile(); –

+0

これは動作しません、私はそれをしないだけのエラーは発生しません。 –

関連する問題