【问题标题】:Modify an ExpandableListView item / refresh array?修改 ExpandableListView 项/刷新数组?
【发布时间】:2017-09-28 15:24:55
【问题描述】:

大家好,我是 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.裁缝

【问题讨论】:

    标签: android arrays string android-studio expandablelistview


    【解决方案1】:

    我通常如何更新 listViews 是使用自定义方法。所以不要直接更新信息,而是用一个同时为listView设置适配器的方法来更新它

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

    【讨论】:

    • 你能解释一下吗?我听不懂,把它翻译成我的案例。
    猜你喜欢
    • 2016-07-23
    • 2012-02-04
    • 2011-02-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-12-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多