我有一个可扩展的列表视图,我也可以添加。如何保存添加的每个项目。我已经尝试将共享偏好与gson一起使用到json,但这似乎不起作用。这是我的可扩展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添加组,并且还添加名为“Yo”的子项,仅用于测试原因:
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的对象类。如果你需要它我可以展示它们。提前谢谢!
答案 0 :(得分:0)
让你的模型类实现Serializable
将最新的arrayList保存到文件中
RegExp
从文件中检索数据
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();
}
}