2017-08-14 51 views
0

我有一個可擴展的列表視圖,我也可以添加。我如何保存每個添加的項目。我已經嘗試使用與gson的共享首選json,但是這似乎並沒有工作。這裏是我的可擴展列表視圖適配器:如何在android中保存可展開的listview?

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) {} 
} 

我也有兩個對象類叫Group和Child。如果你需要他們,我可以展示給你。提前致謝!

+1

「如何保存一個可擴展的ListView android的?」 - 你沒有。您保存用於填充'ExpandableListView'的模型數據。在你的情況下,這是保存'Group'和'Child'對象。 「我曾嘗試將json的共享前綴與gson一起使用,但這似乎並不奏效」 - 我們無法幫助您處理我們看不到的代碼。 'SharedPreferences'似乎是一個奇怪的選擇。您也可以將數據保存爲純JSON文件(例如,在'getFilesDir()')中。 – CommonsWare

+0

你能指定爲什麼你不能使用gson和sharedpreferences保存它嗎? – ravindu1024

+0

@CommonsWare我將如何去保存組和子對象,然後檢索它們並再次填充可展開列表? –

回答

0

讓烏爾模型類實現Serializable

保存最新的ArrayList到一個文件

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(); 
    } 
} 

從文件中檢索數據

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

這不起作用,我沒有得到任何錯誤,它只是不這樣做。 –

相關問題