Android仿QQ好友列表分組實現(xiàn)增刪改及持久化
Android自帶的控件ExpandableListView實現(xiàn)了分組列表功能,本案例在此基礎(chǔ)上進(jìn)行優(yōu)化,為此控件添加增刪改分組及子項的功能,以及列表數(shù)據(jù)的持久化。
Demo實現(xiàn)效果:

GroupListDemo具體實現(xiàn):
①demo中將列表頁面設(shè)計為Fragment頁面,方便后期調(diào)用;在主界面MainActivity中動態(tài)添加GroupListFragment頁面;
MainActivity.java
package com.eric.grouplistdemo;
import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentManager;
import android.os.Bundle;
import android.widget.RelativeLayout;
public class MainActivity extends Activity {
public static GroupListFragment fragment;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
fragment = new GroupListFragment();
getFragmentManager().beginTransaction()
.replace(R.id.fragContainer, fragment).commit();
}
}
動態(tài)添加GroupListFragment實例到界面的fragContainer布局中;將fragment聲明為static用于在Adapter中組添加子項時進(jìn)行調(diào)用。
activity_main.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <RelativeLayout android:id="@+id/fragContainer" android:layout_width="match_parent" android:layout_height="match_parent" > </RelativeLayout> </LinearLayout>
②實現(xiàn)自定義適配器類MyAdapter,繼承自BaseExpandableListAdapter;組項布局及子項布局;
list_item_parent.xml組項布局文件,展開圖標(biāo)及名稱,增刪圖標(biāo);
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="50dp" android:background="#0099ff" android:orientation="horizontal"> <ImageView android:id="@+id/image_parent" android:layout_width="50dp" android:layout_height="50dp" android:src="@drawable/image_parent1"/> <TextView android:id="@+id/text_parent" android:layout_width="wrap_content" android:layout_height="50dp" android:textColor="#FFF" android:textSize="20sp" android:text="parent1" android:layout_toRightOf="@id/image_parent" android:gravity="center"/> <ImageView android:id="@+id/image_delete" android:layout_width="40dp" android:layout_height="40dp" android:layout_centerVertical="true" android:layout_alignParentRight="true" android:src="@drawable/delete"/> <ImageView android:id="@+id/image_add" android:layout_width="40dp" android:layout_height="40dp" android:layout_centerVertical="true" android:layout_toLeftOf="@id/image_delete" android:src="@drawable/add"/> </RelativeLayout>
list_item_child.xml子項布局文件,名稱及刪除圖標(biāo);
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="40dp" > <TextView android:id="@+id/text_child" android:layout_width="wrap_content" android:layout_height="40dp" android:layout_margin="5dp" android:textColor="#0099ff" android:text="child" android:layout_centerInParent="true" android:gravity="center"/> <ImageView android:id="@+id/image_delete" android:layout_width="40dp" android:layout_height="40dp" android:layout_alignParentRight="true" android:layout_centerVertical="true" android:src="@drawable/delete"/> </RelativeLayout>
MyAdapter.java自定義適配器
package com.eric.grouplistdemo;
import java.util.List;
import java.util.Map;
import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
public class MyAdapter extends BaseExpandableListAdapter{
private List<String> parentList;
private Map<String,List<String>> map;
private Context context;
private EditText edit_modify;
private ModifyDialog dialog;
//構(gòu)造函數(shù)
public MyAdapter(Context context, List<String> parentList, Map<String,List<String>> map) {
this.context = context;
this.parentList = parentList;
this.map = map;
}
//獲取分組數(shù)
@Override
public int getGroupCount() {
return parentList.size();
}
//獲取當(dāng)前組的子項數(shù)
@Override
public int getChildrenCount(int groupPosition) {
String groupName = parentList.get(groupPosition);
int childCount = map.get(groupName).size();
return childCount;
}
//獲取當(dāng)前組對象
@Override
public Object getGroup(int groupPosition) {
String groupName = parentList.get(groupPosition);
return groupName;
}
//獲取當(dāng)前子項對象
@Override
public Object getChild(int groupPosition, int childPosition) {
String groupName = parentList.get(groupPosition);
String chidlName = map.get(groupName).get(childPosition);
return chidlName;
}
//獲取組ID
@Override
public long getGroupId(int groupPosition) {
return groupPosition;
}
//獲取子項ID
@Override
public long getChildId(int groupPosition, int childPosition) {
return childPosition;
}
@Override
public boolean hasStableIds() {
return true;
}
//組視圖初始化
@Override
public View getGroupView(int groupPosition, boolean isExpanded,
View convertView, ViewGroup parent) {
final int groupPos = groupPosition;
if(convertView == null){
convertView = LayoutInflater.from(context).inflate(R.layout.list_item_parent, null);
}
ImageView image = (ImageView) convertView.findViewById(R.id.image_parent);
ImageView image_add = (ImageView) convertView.findViewById(R.id.image_add);
ImageView image_delete = (ImageView) convertView.findViewById(R.id.image_delete);
if(isExpanded){
image.setImageResource(R.drawable.image_parent2);
}else{
image.setImageResource(R.drawable.image_parent1);
}
image_add.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
alertAddDialog(MainActivity.fragment.getActivity(), "新增子項", groupPos);
}
});
image_delete.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
GroupListFragment.deleteGroup(groupPos);
}
});
TextView parentText = (TextView) convertView.findViewById(R.id.text_parent);
parentText.setText(parentList.get(groupPosition));
return convertView;
}
//子項視圖初始化
@Override
public View getChildView(int groupPosition, int childPosition,
boolean isLastChild, View convertView, ViewGroup parent) {
final int groupPos = groupPosition;
final int childPos = childPosition;
if(convertView == null){
convertView = LayoutInflater.from(context).inflate(R.layout.list_item_child, null);
}
TextView childText = (TextView) convertView.findViewById(R.id.text_child);
ImageView image_delete = (ImageView) convertView.findViewById(R.id.image_delete);
String parentName = parentList.get(groupPosition);
String childName = map.get(parentName).get(childPosition);
childText.setText(childName);
image_delete.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
GroupListFragment.deleteChild(groupPos, childPos);
}
});
return convertView;
}
@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
}
//彈新增子項對話框
public void alertAddDialog(Context context, String title, int currentGroup){
final int group = currentGroup;
dialog = new ModifyDialog(context, title, null);
edit_modify = dialog.getEditText();
dialog.setOnClickCommitListener(new OnClickListener() {
@Override
public void onClick(View v) {
GroupListFragment.addChild(group, edit_modify.getText().toString());
dialog.dismiss();
}
});
dialog.show();
}
}
構(gòu)造函數(shù):將傳入的parentList和map進(jìn)行數(shù)據(jù)同步,parentList保存組數(shù)據(jù),map保存對應(yīng)組及其子項list數(shù)據(jù);
獲取分組數(shù)及子項數(shù)等很簡單,就不介紹了,主要講述一下,組視圖初始化和子項視圖初始化這兩個函數(shù);
組視圖初始化getGroupView():盡量復(fù)用convertView防止內(nèi)存泄露,首先是進(jìn)行判斷,若convertView為空,則進(jìn)行組視圖初始化,加載list_item_parent子項布局;獲取到相應(yīng)的組布局控件:展開圖標(biāo)image、添加圖標(biāo)image_add、刪除圖標(biāo)image_delete;通過傳遞過來的布爾類型參數(shù)isExpanded,進(jìn)行判斷給image賦值展開圖標(biāo)或合并圖標(biāo);分別給添加圖標(biāo)和刪除圖標(biāo)添加點擊事件,分別調(diào)用GroupListFragment中的彈出添加窗口函數(shù)alertAddDialog()和刪除組函數(shù)deleteGroup();
子項視圖初始化getChildView():同樣盡量復(fù)用convertView防止內(nèi)存泄露,首先是進(jìn)行判斷,若convertView為空,則進(jìn)行子項視圖初始化,加載list_item_child子項布局;獲取到相應(yīng)的子布局控件:內(nèi)容文本ChildText和刪除圖標(biāo)Image_delete;從parentList和map中分別獲取到,當(dāng)前子項的組名parentName和子項名childName,賦值ChildText,刪除圖標(biāo)添加點擊事件,調(diào)用刪除子項函數(shù)deleteChild();
③實現(xiàn)自定義對話框類ModifyDialog,繼承自Dialog類,對輸入修改內(nèi)容,或新增項內(nèi)容進(jìn)行過渡;
no_title_dialog.xml,在values目錄下自定義Dialog的style類型xml,除去Dialog的標(biāo)題欄;
<?xml version="1.0" encoding="utf-8"?> <resources xmlns:android="http://schemas.android.com/apk/res/android"> <style name="noTitleDialog" parent="android:style/Theme.Dialog"> <item name="android:width">300dp</item> <item name="android:height">40dp</item> <item name="android:windowNoTitle">true</item> </style> </resources>
dialog_modify.xml 自定義對話框的布局文件,標(biāo)題文本,輸入框,確定按鈕;
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <TextView android:id="@+id/text_title" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" android:gravity="center" android:background="#0099ff" android:text="修改名稱" android:textColor="#FFF" android:textSize="20sp"/> <EditText android:id="@+id/edit_modify" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="組名稱"/> <Button android:id="@+id/btn_commit" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="確定" android:textColor="#FFF" android:background="#0099ff" /> </LinearLayout>
ModifyDialog.java
package com.eric.grouplistdemo;
import android.app.Dialog;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class ModifyDialog extends Dialog{
private TextView text_title;
private EditText edit_modify;
private Button btn_commit;
public ModifyDialog(Context context, String title, String name) {
super(context, R.style.noTitleDialog);
View view = LayoutInflater.from(getContext())
.inflate(R.layout.dialog_modify, null);
text_title = (TextView) view.findViewById(R.id.text_title);
edit_modify = (EditText)view.findViewById(R.id.edit_modify);
btn_commit = (Button) view.findViewById(R.id.btn_commit);
text_title.setText(title);
edit_modify.setText(name);
super.setContentView(view);
}
public EditText getEditText(){
return edit_modify;
}
public void setOnClickCommitListener(View.OnClickListener listener){
btn_commit.setOnClickListener(listener);
}
}
ModifyDialog自定義構(gòu)造函數(shù)中,通過super()加載剛剛自定義的no_title_dialog.xml,聲明View加載layout布局dialog_modify.xml;并且獲取布局中的相應(yīng)控件,將構(gòu)造函數(shù)中傳來的字符串title和name,分別賦值到標(biāo)題文本和輸入框控件中;最后調(diào)用setContentView()初始化對話框視圖;
添加一個返回輸入框控件的函數(shù)getEditText(),用于獲取輸入框輸入的內(nèi)容;
還需要一個自定義的點擊事件監(jiān)聽器,綁定在確定按鈕上;
④準(zhǔn)備工作都完成了,下面就實現(xiàn)GroupListFragment,包括數(shù)據(jù)的初始化及持久化保存,組項和子項的增刪改操作,列表子項點擊事件,列表組項和子項的長按事件;
fragment_group_list.xml 頁面的布局文件ExpandableListView列表以及一個添加組圖標(biāo);
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" > <ExpandableListView android:id="@+id/expandablelistview" android:layout_width="match_parent" android:layout_height="match_parent" android:groupIndicator="@null" > </ExpandableListView> <ImageView android:id="@+id/image_add" android:layout_width="40dp" android:layout_height="40dp" android:layout_centerHorizontal="true" android:layout_alignParentBottom="true" android:src="@drawable/add"/> </RelativeLayout>
這里需要將ExpandableListView的groupIndicator屬性設(shè)置為@null,不使用其自帶的展開圖標(biāo);
GroupListFragment.java 加載列表的頁面
package com.eric.grouplistdemo;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.R.integer;
import android.app.Fragment;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.ExpandableListView;
import android.widget.ImageView;
import android.widget.Toast;
public class GroupListFragment extends Fragment{
private View view;
private ExpandableListView expandableListView;
public static MyAdapter adapter;
public static List<String> parentList;
public static Map<String,List<String>> map;
private ModifyDialog dialog;
private EditText edit_modify;
private ImageView image_add;
private int currentGroup,currentChild;
public static SharedPreferences sp;
public static Editor editor;
public static String dataMap,dataParentList;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
view = inflater.inflate(R.layout.fragment_group_list, container, false);
expandableListView = (ExpandableListView) view.findViewById(R.id.expandablelistview);
image_add = (ImageView) view.findViewById(R.id.image_add);
image_add.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
alertAddDialog(getActivity(), "新增組");
}
});
initData();
adapter = new MyAdapter(getActivity().getApplicationContext(), parentList, map);
expandableListView.setAdapter(adapter);
//設(shè)置子項點擊事件
MyOnClickListener myListener = new MyOnClickListener();
expandableListView.setOnChildClickListener(myListener);
//設(shè)置長按點擊事件
MyOnLongClickListener myLongListener = new MyOnLongClickListener();
expandableListView.setOnItemLongClickListener(myLongListener);
return view;
}
public void initData(){
map = new HashMap<String, List<String>>();
parentList = new ArrayList<String>();
sp = getActivity().getApplicationContext().getSharedPreferences("spfile", getActivity().MODE_PRIVATE);
dataMap = sp.getString("dataMap", null);
dataParentList = sp.getString("dataParentList", null);
if(dataMap == null || dataParentList == null){
parentList = new ArrayList<String>();
parentList.add("客廳");
parentList.add("廚房");
parentList.add("臥室");
List<String> list1 = new ArrayList<String>();
list1.add("客廳空調(diào)");
list1.add("客廳電視");
list1.add("客廳電燈");
map.put("客廳", list1);
List<String> list2 = new ArrayList<String>();
list2.add("廚房油煙機");
list2.add("廚房電燈");
list2.add("廚房電器");
map.put("廚房", list2);
List<String> list3 = new ArrayList<String>();
list3.add("臥室空調(diào)");
list3.add("臥室燈光");
list3.add("臥室電視");
map.put("臥室", list3);
}else{
try {
//初始化parentList
JSONArray jsonArray = new JSONArray(dataParentList);
for (int i = 0; i < jsonArray.length(); i++) {
parentList.add(jsonArray.get(i).toString());
}
//初始化map
JSONObject jsonObject = new JSONObject(dataMap);
for (int i = 0; i < jsonObject.length(); i++) {
String key = jsonObject.getString(parentList.get(i));
JSONArray array = new JSONArray(key);
List<String> list = new ArrayList<String>();
for (int j = 0; j < array.length(); j++) {
list.add(array.get(j).toString());
}
map.put(parentList.get(i), list);
}
Log.d("eric", "①:"+map+"②:"+parentList);
} catch (JSONException e) {
e.printStackTrace();
Log.e("eric","String轉(zhuǎn)Map或List出錯"+e);
}
}
Log.e("eric", dataMap+"!&&!"+dataParentList);
saveData();
}
//自定義點擊監(jiān)聽事件
public class MyOnClickListener implements ExpandableListView.OnChildClickListener{
@Override
public boolean onChildClick(ExpandableListView parent, View v,
int groupPosition, int childPosition, long id) {
String str = "choose"+groupPosition+"-"+childPosition;
Toast.makeText(getActivity(), str, Toast.LENGTH_SHORT).show();
return false;
}
}
//自定義長按監(jiān)聽事件
public class MyOnLongClickListener implements AdapterView.OnItemLongClickListener{
@Override
public boolean onItemLongClick(AdapterView<?> parent, View view,
int position, long id) {
//長按子項
if (ExpandableListView.getPackedPositionType(id) == ExpandableListView.PACKED_POSITION_TYPE_CHILD){
long packedPos = ((ExpandableListView) parent).getExpandableListPosition(position);
int groupPosition = ExpandableListView.getPackedPositionGroup(packedPos);
int childPosition = ExpandableListView.getPackedPositionChild(packedPos);
currentGroup = groupPosition;
currentChild = childPosition;
String str = (String)adapter.getChild(groupPosition, childPosition);
alertModifyDialog("修改此項名稱",str);
Toast.makeText(getActivity(),str,Toast.LENGTH_SHORT).show();
return true;
//長按組
}else if(ExpandableListView.getPackedPositionType(id) == ExpandableListView.PACKED_POSITION_TYPE_GROUP){
long packedPos = ((ExpandableListView) parent).getExpandableListPosition(position);
int groupPosition = ExpandableListView.getPackedPositionGroup(packedPos);
int childPosition = ExpandableListView.getPackedPositionChild(packedPos);
currentGroup = groupPosition;
currentChild = childPosition;
String group = parentList.get(groupPosition);
alertModifyDialog("修改組名稱", group);
String str = (String)adapter.getGroup(groupPosition);
Toast.makeText(getActivity(),str,Toast.LENGTH_SHORT).show();
}
return false;
}
}
//新增組
public static void addGroup(String newGroupName){
parentList.add(newGroupName);
List<String> list = new ArrayList<String>();
map.put(newGroupName, list);
adapter.notifyDataSetChanged();
saveData();
}
//新增子項到指定組
public static void addChild(int groupPosition, String newChildName){
String groupName = parentList.get(groupPosition);
List<String> list = map.get(groupName);
list.add(newChildName);
adapter.notifyDataSetChanged();
saveData();
}
//刪除指定組
public static void deleteGroup(int groupPos){
String groupName = parentList.get(groupPos);
map.remove(groupName);
parentList.remove(groupPos);
adapter.notifyDataSetChanged();
saveData();
}
//刪除指定子項
public static void deleteChild(int groupPos, int childPos){
String groupName = parentList.get(groupPos);
List<String> list = map.get(groupName);
list.remove(childPos);
adapter.notifyDataSetChanged();
saveData();
}
//修改該項名稱
public void modifyName(int groupPosition, int childPosition, String modifyName){
Toast.makeText(getActivity(), String.valueOf(groupPosition)+'-'+String.valueOf(childPosition), Toast.LENGTH_SHORT).show();
if(childPosition<0){
//修改組名稱
String groupName = parentList.get(groupPosition);
if(!groupName.equals(modifyName)){
map.put(modifyName, map.get(groupName));
map.remove(groupName);
parentList.set(groupPosition, modifyName);
}
}else{
//修改子項名稱
String group = parentList.get(groupPosition);
List<String> list =map.get(group);
list.set(childPosition, modifyName);
map.put(group, list);
}
adapter.notifyDataSetChanged();
saveData();
}
//彈修改對話框
public void alertModifyDialog(String title, String name){
dialog = new ModifyDialog(getActivity(), title, name);
edit_modify = dialog.getEditText();
dialog.setOnClickCommitListener(new OnClickListener() {
@Override
public void onClick(View v) {
modifyName(currentGroup, currentChild, edit_modify.getText().toString());
dialog.dismiss();
}
});
dialog.show();
}
//彈新增組對話框
public void alertAddDialog(Context context, String title){
dialog = new ModifyDialog(context, title, null);
edit_modify = dialog.getEditText();
dialog.setOnClickCommitListener(new OnClickListener() {
@Override
public void onClick(View v) {
addGroup(edit_modify.getText().toString());
dialog.dismiss();
}
});
dialog.show();
}
//保存數(shù)據(jù)
public static void saveData(){
JSONObject jsonObject = new JSONObject(map);
dataMap = jsonObject.toString();
dataParentList = parentList.toString();
editor = sp.edit();
editor.putString("dataMap", dataMap);
editor.putString("dataParentList", dataParentList);
editor.commit();
}
}
內(nèi)容有點多,一個個來:
初始化Fragment頁面函數(shù)onCreateView():
首先,要進(jìn)行l(wèi)ayout布局fragment_group_list.xml加載,獲取相應(yīng)控件的實例,expandableListView列表控件以及添加組圖標(biāo)image_add,添加組圖標(biāo)添加點擊事件;調(diào)用initData()方法進(jìn)行組數(shù)據(jù)parentList和組對應(yīng)子項map的初始化;然后,實例化adapter傳入parentList和map數(shù)據(jù)到自定義適配器MyAdapter中,并將其綁定到expandableListView上;最后,給expandableListView添加自定義子項點擊事件監(jiān)聽器,組項和子項長按事件監(jiān)聽器;
初始化數(shù)據(jù)函數(shù)initData():該函數(shù)通過ShareReference來保存數(shù)據(jù);
首先,實例化parentList和map,從ShareReference中獲取到保存的String類型的parentList和map實際數(shù)據(jù),賦值到dataMap和dataParentList中,若當(dāng)中數(shù)據(jù)不存在,默認(rèn)返回null,第一次運行程序的時候肯定是沒有數(shù)據(jù)的,故進(jìn)行判斷,若沒獲取到數(shù)據(jù),那就給parentList和Map賦值“客廳”、“廚房”、“臥室”等一系列數(shù)據(jù);如果有數(shù)據(jù)的話,那就得進(jìn)行數(shù)據(jù)的轉(zhuǎn)換,因為之前保存的String類型數(shù)據(jù),parentList初始化:將dataParentList轉(zhuǎn)換為JSONArray類型,通過循環(huán)將數(shù)據(jù)賦值到parentList中;同理,將dataMap轉(zhuǎn)換為JSONObject類型,通過兩層for循環(huán)將數(shù)據(jù)賦值到map中。
自定義點擊子項監(jiān)聽其類MyOnClickListener:實現(xiàn)ExpandableListView.OnChildClickListener接口,這里就簡單的進(jìn)行Toast操作,讀者可以修改為跳轉(zhuǎn)等其他自定義功能;
自定義長按組項或子項監(jiān)聽器類MyOnLongClickListener:實現(xiàn)AdapterView.OnItemLongClickListener接口,通過調(diào)用ExpandableListView的getPackedPositionType()方法來判斷此時長按的是組項還是子項;這里將長按組和子項分離出來,方便根據(jù)功能修改;無論是組項還是子項長按后,都調(diào)用alertModifyDialog()彈修改對話框,傳入當(dāng)前項的名稱;
新增組addGroup函數(shù):將傳遞過來的newGroupName,添加parentList中,且定義一個空的list,綁定newGroupName,將這對string和list,添加到map中;最后,調(diào)用adapter.notifyDataSetChanged()刷新列表,調(diào)用saveData()保存數(shù)據(jù)到ShareReference;
同理,新增子項到指定組addChild(),刪除指定組deleteGroup(),刪除指定子項deleteChild(),都是對parentList和map進(jìn)行操作,最后刷新列表和保存數(shù)據(jù);
修改該項名稱modifyName():通過傳遞過來childPosition進(jìn)行判斷,當(dāng)修改項為組項時,childPosition的值為-1,以此區(qū)分組項和子項;這里遇到一個問題,當(dāng)組項提交的名稱與原名稱相同會報錯,故添加一個判斷,僅提交的名稱不同時才進(jìn)行修改操作;修改的具體實現(xiàn)還是對parentList和map的操作,以修改組項為例,同新增組,添加一個modifyName的組,其對應(yīng)的List為原來組名對應(yīng)的List數(shù)據(jù),然后再將原來的groupName組及其對應(yīng)的list刪除,實現(xiàn)修改;最后同樣要刷新列表和保存數(shù)據(jù);
彈修改對話框alertModifyDialog()函數(shù):首先對自定義的dialog進(jìn)行實例化,初始化標(biāo)題和輸入框中的數(shù)據(jù);調(diào)用getEditText()函數(shù)獲取輸入框?qū)嵗?,添加提交按鈕點擊事件,調(diào)用modifyName方法傳入當(dāng)前組和子項數(shù)據(jù),以及輸入框中提交的文本;
彈新增組對話框alertAddDialog()函數(shù):同樣,實例化dialog,獲取輸入框控件,點擊事件中調(diào)用addGroup()方法添加數(shù)據(jù);
保存數(shù)據(jù)saveData()函數(shù):將parentList和map數(shù)據(jù)轉(zhuǎn)化為String類型,分別賦值,保存至ShareReference的Editor中并提交;
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助。
相關(guān)文章
Android自動編輯文本框(AutoCompleteTextView)使用方法詳解
這篇文章主要為大家詳細(xì)介紹了Android自動編輯文本框AutoCompleteTextView的使用方法,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-02-02
Android使用httpPost向服務(wù)器發(fā)送請求的方法
這篇文章主要介紹了Android使用httpPost向服務(wù)器發(fā)送請求的方法,實例分析了Android針對HttpPost類的操作技巧,需要的朋友可以參考下2015-12-12
Android學(xué)習(xí)筆記之Shared Preference
在之前遇到有個需求是要改settings里面自動轉(zhuǎn)屏的首選項,于是就學(xué)習(xí)了下Shared Preference。Shared Preference是一種簡單的、輕量級的鍵/值對機制,用于保存原始應(yīng)用程序數(shù)據(jù),最常見的就是首選項2013-09-09
使用androidx BiometricPrompt實現(xiàn)指紋驗證功能
這篇文章主要介紹了使用androidx BiometricPrompt實現(xiàn)指紋驗證功能,對android指紋驗證相關(guān)知識感興趣的朋友跟隨小編一起看看吧2021-07-07
Android 創(chuàng)建與解析XML(四)——詳解Pull方式
本篇文章主要介紹了Android創(chuàng)建與解析XML(二)——詳解Pull方式,這里整理了詳細(xì)的代碼,有需要的小伙伴可以參考下。2016-11-11

