修改ExpandableListView项/刷新数组?

时间:2017-09-28 15:24:56

标签: android arrays string android-studio expandablelistview

嘿伙计们我是androidm的新手,我坚持了一下。

我想创建一个带有可扩展列表视图的时间表,默认项目(例如,Subject1,Subject2,Room1,Room2 ......)。我想编辑'长按的可扩展名单小孩。

这是我的活动

public class TuesdayActivity extends AppCompatActivity {

String[] myArray = new String[]{"Subject1", "Subject2", "Subject3", "Subject4", "Subject5", "Subject6"};

public void fillData()
{
    times = new ArrayList<>();
    info = new HashMap<String, List<String>>();

    times.add("8:00-10:00");
    times.add("10:00-12:00");
    times.add("12:00-14:00");
    times.add("14:00-16:00");
    times.add("16:00-18:00");
    times.add("18:00-20:00");


    List<String> from_8 = new ArrayList<>();
    List<String> from_10 = new ArrayList<>();
    List<String> from_12 = new ArrayList<>();
    List<String> from_14 = new ArrayList<>();
    List<String> from_16 = new ArrayList<>();
    List<String> from_18 = new ArrayList<>();

    from_8.add(myArray[0]);
    from_8.add("Room1");

    from_10.add("Subject2");
    from_10.add("Room2");

    from_12.add("Subject3");
    from_12.add("Room3");

    from_14.add("Subject4");
    from_14.add("Room4");

    from_16.add("Subject5");
    from_16.add("Room5");

    from_18.add("Subject6");
    from_18.add("Room7");

    info.put(times.get(0),from_8);
    info.put(times.get(1),from_10);
    info.put(times.get(2),from_12);
    info.put(times.get(3),from_14);
    info.put(times.get(4),from_16);
    info.put(times.get(5),from_18);


}




ExpandableListView expandableListView;

List<String > times;
Map<String,List<String>> info;
ExpandableListAdapter listAdapter;

@Override
protected void onCreate(Bundle savedInstanceState) {


    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_tuesday);

    expandableListView = (ExpandableListView) findViewById(R.id.exList);
    fillData();

    listAdapter = new MyExListAdapter(this,times,info);


    expandableListView.setAdapter(listAdapter);
    expandableListView.setOnGroupExpandListener(new ExpandableListView.OnGroupExpandListener() {
        @Override
        public void onGroupExpand(int groupPosition) {

            Toast.makeText(getBaseContext(),times.get(groupPosition)+ " is expanded",Toast.LENGTH_SHORT).show();

        }
    });

    expandableListView.setOnGroupCollapseListener(new ExpandableListView.OnGroupCollapseListener() {
        @Override
        public void onGroupCollapse(int groupPosition) {
            Toast.makeText(getBaseContext(),times.get(groupPosition)+ " is collapsed",Toast.LENGTH_SHORT).show();
        }
    });

    expandableListView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
        @Override
        public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
            if (ExpandableListView.getPackedPositionType(id) == ExpandableListView.PACKED_POSITION_TYPE_CHILD) {
                int groupPosition = ExpandableListView.getPackedPositionGroup(id);
                int childPosition = ExpandableListView.getPackedPositionChild(id);

                if(childPosition  == 0)
                {
                  myArray[0] = "Trying to change text";
                }
                return true;
            }
            return false;
        }
    });
}

}

这是我的适配器

public class MyExListAdapter extends BaseExpandableListAdapter {

Context context;
List<String> times;
Map<String,List<String>> info;

public MyExListAdapter(Context context, List<String> times, Map<String, List<String>> info) {
    this.context = context;
    this.times = times;
    this.info = info;
}

@Override
public int getGroupCount() {

    return times.size();
}

@Override
public int getChildrenCount(int groupPosition) {
    return info.get(times.get(groupPosition)).size();
}

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

@Override
public Object getChild(int groupPosition, int childPosition) {
    return info.get(times.get(groupPosition)).get(childPosition);
}

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

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

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

@Override
public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {

    String times = (String ) getGroup(groupPosition);
    if(convertView == null)
    {
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(context.LAYOUT_INFLATER_SERVICE);
        convertView = inflater.inflate(R.layout.list_parent,null);
    }
    TextView txtParent = (TextView) convertView.findViewById(R.id.txtParent);
    txtParent.setText(times);

    return convertView;
}

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

    String info = (String) getChild(groupPosition,childPosition);

    if(convertView == null)
    {
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(context.LAYOUT_INFLATER_SERVICE);
        convertView = inflater.inflate(R.layout.list_child,null);
    }
    TextView txtChild = (TextView) convertView.findViewById(R.id.txtChild);
    txtChild.setText(info);
    notifyDataSetChanged();

    return convertView;
}
@Override
public boolean isChildSelectable(int i, int i1) {
    return true;
}

}

首先,我想长时间编辑资源/字符串,但经过简短的阅读后,我知道在运行时不可能。

我的问题是:

如果我更改数组的值,如何更新列表项? (我无法理解notifyDataSetChanged();)

还有其他办法吗?

如果您知道解决方案,请帮助我。

谢谢,  M. Tailor

1 个答案:

答案 0 :(得分:0)

我通常如何使用自定义方法更新listViews。因此,不要直接更新信息,而是使用也为listView设置适配器的方法更新信息

void updateMyListView(foo, bar){
    updateInfoMap();
    expandableListView.setAdapter(listAdapter);