Java 删除一行时如何使用列表中的设置更改
我正在使用可扩展listView和可扩展ListAdapter。 ChlidView显示标题,子视图显示副标题。 在“子视图”中,我有一个删除特定行的按钮。 我有两个类,一个用于将数据从数据库加载到hashMap,第二个用于ExpandableListAdapter。我在适配器类上创建了onClick方法,delete函数正在工作,但是列表没有刷新更新的数据。我知道我需要再次将数据加载到hashMap,但是适配器类在第一个类之外。我尝试使用“notifyDataSetChanged”,但也没有结果 第1类:Java 删除一行时如何使用列表中的设置更改,java,android,listview,expandablelistview,Java,Android,Listview,Expandablelistview,我正在使用可扩展listView和可扩展ListAdapter。 ChlidView显示标题,子视图显示副标题。 在“子视图”中,我有一个删除特定行的按钮。 我有两个类,一个用于将数据从数据库加载到hashMap,第二个用于ExpandableListAdapter。我在适配器类上创建了onClick方法,delete函数正在工作,但是列表没有刷新更新的数据。我知道我需要再次将数据加载到hashMap,但是适配器类在第一个类之外。我尝试使用“notifyDataSetChanged”,但也没有结
hand = new DbHandler(getActivity());
fullList = new HashMap<ArrayList<ClockModel>, ArrayList<ClockModel>>();
temp = new ArrayList<ClockModel>();
ArrayList<ClockModel> child = new ArrayList<ClockModel>();
child = hand.getDay(workName);
for (int i = 0; i < child.size(); i++) {
if(child.get(i).getDateMonth() == month && child.get(i).getDateYear()==year){
ClockModel m1 = new ClockModel(
child.get(i).getId(),
child.get(i).getWorkName(),
child.get(i).getDateDay(),
child.get(i).getDateMonth(),
);
temp.add(m1);
fullList.put(temp, temp);
}
}
adapter = new ExpandableListCustom(getActivity(), fullList,temp);
lv.setAdapter(adapter);
hand=newdbhandler(getActivity());
fullList=新的HashMap();
temp=新的ArrayList();
ArrayList子项=新的ArrayList();
child=hand.getDay(工作名);
对于(int i=0;i
适配器:
@Override
public View getChildView(final int groupPosition, final int childPosition,
boolean isLastChild, View convertView, ViewGroup parent) {
//final String childText = (String) getChild(groupPosition, childPosition);
if (convertView == null) {
LayoutInflater infalInflater = (LayoutInflater)
this.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = infalInflater.inflate(R.layout.row_child,null);
}
TextView txMyVal = (TextView) convertView.findViewById(R.id.row_myVal);
TextView txBreakTime = (TextView) convertView.findViewById(R.id.row_breakTime);
TextView txComment = (TextView) convertView.findViewById(R.id.row_comments);
TextView txNameOfDay = (TextView) convertView.findViewById(R.id.row_nameOfDay);
String comments= fullList.get(child).get(groupPosition).getComment();
nameOfDay= fullList.get(child).get(groupPosition).getNameOfDay();
shiftType= fullList.get(child).get(groupPosition).getShiftType();
int breakTime= fullList.get(child).get(groupPosition).getBreakTime();
currentPosition = fullList.get(child).get(groupPosition).getId();
btnRemove = (ImageButton) convertView.findViewById(R.id.row_btnRemove);
btnRemove.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
hand.deleteDay(currentPosition);
Toast.makeText(context, "Del : "+currentPosition, 2000).show();
}
});
public class ExpandableListCustom extends BaseExpandableListAdapter implements OnClickListener {
DbHandler hand;
private Context context;
private HashMap<ArrayList<ClockModel>,ArrayList<ClockModel> > fullList; //Headers
ArrayList<ClockModel> child; //data
MySharedPreferences preferences;
public ExpandableListCustom(Context context,
HashMap<ArrayList<ClockModel>, ArrayList<ClockModel>> fullList,
ArrayList<ClockModel> child) {
super();
this.context = context;
this.fullList = fullList;
this.child = child;
clockSet = new Clock();
preferences = new MySharedPreferences(context);
hand = new DbHandler(context);
}
@Override
public Object getChild(int groupPosition, int childPosition) {
// TODO Auto-generated method stub
return this.fullList.get(this.fullList.get(childPosition));
}
@Override
public long getChildId(int groupPosition, int childPosition) {
return childPosition;
}
@Override
public View getChildView(final int groupPosition, final int childPosition,
boolean isLastChild, View convertView, ViewGroup parent) {
//final String childText = (String) getChild(groupPosition, childPosition);
if (convertView == null) {
LayoutInflater infalInflater = (LayoutInflater)
this.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = infalInflater.inflate(R.layout.row_child,null);
}
TextView txMyVal = (TextView) convertView.findViewById(R.id.row_myVal);
TextView txBreakTime = (TextView) convertView.findViewById(R.id.row_breakTime);
TextView txComment = (TextView) convertView.findViewById(R.id.row_comments);
TextView txNameOfDay = (TextView) convertView.findViewById(R.id.row_nameOfDay);
enterHour= (int) fullList.get(child).get(groupPosition).getEnterHour();
exitHour= (int) fullList.get(child).get(groupPosition).getExitHour();
enterMin= (int) fullList.get(child).get(groupPosition).getEnterMin();
exitMin= (int) fullList.get(child).get(groupPosition).getExitMin();
currentPosition = fullList.get(child).get(groupPosition).getId();
btnRemove = (ImageButton) convertView.findViewById(R.id.row_btnRemove);
btnRemove.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
hand.deleteDay(currentPosition);
Toast.makeText(context, "Del : "+currentPosition, 2000).show();
}
});
return convertView;
}
@Override
public int getChildrenCount(int groupPosition) {
// TODO Auto-generated method stub //////////////////////////////////////////
// return this.fullList.get(groupPosition).size();
if(fullList != null){
return 1;
}else{
return 0;
}
}
@Override
public Object getGroup(int groupPosition) {
// TODO Auto-generated method stub
return this.fullList.get(child).get(groupPosition);
}
@Override
public int getGroupCount() {
if(fullList != null){
return this.fullList.size();
}else{
return 0;
}
}
@Override
public long getGroupId(int groupPosition) {
return groupPosition;
}
@Override
public View getGroupView(int groupPosition, boolean isExpanded,
View convertView, ViewGroup parent) {
int dateDay= fullList.get(child).get(groupPosition).getDateDay();
int dateMonth= fullList.get(child).get(groupPosition).getDateMonth();
int dateYear= fullList.get(child).get(groupPosition).getDateYear();
if (convertView == null) {
LayoutInflater infalInflater = (LayoutInflater) this.context.getSystemService
(Context.LAYOUT_INFLATER_SERVICE);
convertView = infalInflater.inflate(R.layout.row_header, null);
}
return convertView;
}
@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
// TODO Auto-generated method stub
return true;
}
@Override
public boolean hasStableIds() {
// TODO Auto-generated method stub
return false;
}
}
@覆盖
公共视图getChildView(最终int groupPosition,最终int childPosition,
布尔值isLastChild、视图转换视图、视图组父级){
//最终字符串childText=(字符串)getChild(groupPosition,childPosition);
if(convertView==null){
LayoutInflater infalInflater=(LayoutInflater)
this.context.getSystemService(context.LAYOUT\u充气机\u服务);
convertView=infalInflater.充气(R.layout.row_child,null);
}
TextView txMyVal=(TextView)convertView.findViewById(R.id.row_myVal);
TextView txBreakTime=(TextView)convertView.findViewById(R.id.row_breakTime);
TextView txComment=(TextView)convertView.findViewById(R.id.row_注释);
TextView txNameOfDay=(TextView)convertView.findViewById(R.id.row\u nameOfDay);
String comments=fullList.get(child.get(groupPosition.getComment());
nameOfDay=fullList.get(child.get(groupPosition.getNameOfDay();
shiftType=fullList.get(child.get(groupPosition.getShiftType();
int breakTime=fullList.get(child.get(groupPosition.getBreakTime();
currentPosition=fullList.get(child.get(groupPosition.getId));
btnRemove=(ImageButton)convertView.findViewById(R.id.row\u btnRemove);
setOnClickListener(新的OnClickListener(){
@凌驾
公共void onClick(视图v){
hand.deleteDay(当前位置);
makeText(上下文,“Del:+currentPosition,2000).show();
}
});
公共类ExpandableListCustom扩展BaseExpandableListAdapter实现OnClickListener{
手;
私人语境;
私有HashMap fullList;//头
ArrayList子项;//数据
我的共享偏好;
public ExpandableListCustom(上下文,
HashMap完整列表,
ArrayList(子项){
超级();
this.context=上下文;
this.fullList=完整列表;
这个孩子=孩子;
时钟组=新时钟();
首选项=新的MySharedPreferences(上下文);
hand=newdbhandler(上下文);
}
@凌驾
公共对象getChild(int-groupPosition,int-childPosition){
//TODO自动生成的方法存根
返回this.fullList.get(this.fullList.get(childPosition));
}
@凌驾
公共长getChildId(int-groupPosition,int-childPosition){
返回子位置;
}
@凌驾
公共视图getChildView(最终int groupPosition,最终int childPosition,
布尔值isLastChild、视图转换视图、视图组父级){
//最终字符串childText=(字符串)getChild(groupPosition,childPosition);
if(convertView==null){
LayoutInflater infalInflater=(LayoutInflater)
this.context.getSystemService(context.LAYOUT\u充气机\u服务);
convertView=infalInflater.充气(R.layout.row_child,null);
}
TextView txMyVal=(TextView)convertView.findViewById(R.id.row_myVal);
TextView txBreakTime=(TextView)convertView.findViewById(R.id.row_breakTime);
TextView txComment=(TextView)convertView.findViewById(R.id.row_注释);
TextView txNameOfDay=(TextView)convertView.findViewById(R.id.row\u nameOfDay);
enterHour=(int)fullList.get(child.get(groupPosition.getEnterHour();
exitHour=(int)fullList.get(child.get(groupPosition.getExitHour();
enterMin=(int)fullList.get(child.get(groupPosition.getEnterMin();
exitMin=(int)fullList.get(child.get(groupPosition.getExitMin();
currentPosition=fullList.get(child.get(groupPosition.getId));
btnRemove=(ImageButton)convertView.findViewById(R.id.row\u btnRemove);
setOnClickListener(新的OnClickListener(){
@凌驾
公共void onClick(视图v){
hand.deleteDay(当前位置);
makeText(上下文,“Del:+currentPosition,2000).show();
}
});
返回视图;
}
@凌驾
公共整数getChildrenCount(整数组位置){
//TODO自动生成的方法存根//////////////////////////////////////////
//返回此.fullList.get(groupPosition.size();
如果(完整列表!=null){
返回1;
}否则{
返回0;
}
}
@凌驾
公共对象getGroup(int-groupPosition){
//TODO自动生成
@Override
public void onClick(View v) {
hand.deleteDay(currentPosition);
fullList.remove(currentPosition);
notifyDataSetChanged();
}
notifyDataSetChanged();