【问题标题】:Android Expandable list view help when add new row in list在列表中添加新行时的Android可扩展列表视图帮助
【发布时间】:2012-12-27 00:10:19
【问题描述】:

我做了一个只有一组的可扩展视图,点击一个按钮,一个新的

该组中添加的行,这些行包含文本框中的一些数据,当输入时

从行中,我发现当我添加 4 行时我得到 7 行输入,总是这样

定量,当添加 5 行时,我得到 8 行输入之类的,不明白尝试做很多

技巧。

你能帮我从这些行中获取输入的最佳方式吗? actlist 是接受输入的数组列表。 在下面有代码活动

    public class AddActActivity extends ExpandableListActivity implements 

OnGroupClickListener,OnClickListener {
        /** Called when the activity is first created. */
        public TextView ShowTitle;
        public TextView Date;
        public static long cuesheetnimber;
        public static ExpandableListView expandableListView;
        public static ActAdapter expListAdapter;
        static ArrayList<String> groupNames = new ArrayList<String>();
        static ArrayList<ArrayList<Acts>> obsList = new ArrayList<ArrayList<Acts>>

();

        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.addact);

            ShowTitle = (TextView)findViewById(R.id.showtitleact);
            ShowTitle.setText(getIntent().getStringExtra("ShowTitle"));

            Date = (TextView)findViewById(R.id.showdateact);
            Date.setText(DateSpinner.date);

            cuesheetnimber=getIntent().getLongExtra("_ID", 0);



            expandableListView =getExpandableListView();
            expandableListView.setOnGroupClickListener(this);

            expListAdapter = new ActAdapter( this,groupNames, obsList );
            setListAdapter( expListAdapter );

            expListAdapter.groups.add("a");
            //AUDITDATA.HCWlist[AUDITDATA.row]=AUDITDATA.HCWNAME;

            ArrayList<Acts> obs = new ArrayList<Acts>();
            obs.add(new Acts());
            //AUDITDATA.row++;
            expListAdapter.childs.add(obs);
            expandableListView.expandGroup(0);
            expListAdapter.notifyDataSetChanged();

            Button add=(Button)findViewById(R.id.button_add_act);
            add.setOnClickListener(this);

            Button SaveandExit=(Button)findViewById(R.id.SaveandExit);
            SaveandExit.setOnClickListener(this);
        }


        @Override
        public boolean onGroupClick(ExpandableListView parent, View v,
                int groupPosition, long id) {
            // TODO Auto-generated method stub
            return false;
        }


        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            switch (v.getId()) {
            case R.id.button_add_act:
                Button b=(Button)v;
                b.setText("Add Another Act");
                 expListAdapter.addHcwGroup(expandableListView);
              //   expListAdapter.notifyDataSetChanged();   
                 break;
            case R.id.SaveandExit:
                // expListAdapter.notifyDataSetChanged();
                 saveandexit();
                 clear();
                 finish();
                 break;
            default:
                break;
            }
        }


        private void clear() {
            // TODO Auto-generated method stub
            ActAdapter.actlist.clear();
            ActAdapter.groups.clear();
            ActAdapter.childs.clear();

        }

    @Override
    public void onBackPressed() {
        ActAdapter.actlist.clear();
        ActAdapter.groups.clear();
        ActAdapter.childs.clear();
        // TODO Auto-generated method stub
        super.onBackPressed();
    }
        private void saveandexit() {
            // TODO Auto-generated method stub
            for (Acts act :ActAdapter.actlist) {
                Init.setActs(this, act);

            }
        }
    }



And here is adapter code 

   ;

    import java.util.ArrayList;

    import android.content.Context;
    import android.graphics.Color;
    import android.util.Log;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.BaseExpandableListAdapter;
    import android.widget.EditText;
    import android.widget.ExpandableListView;
    import android.widget.TimePicker;


    public class ActAdapter extends BaseExpandableListAdapter  {
        public static int staticindex = 0;
        private Context context;
        static public ArrayList<String> groups;
        static public ArrayList<ArrayList<Acts>> childs;
        private LayoutInflater inflater;
        public boolean flag = false;

        public ActAdapter(Context context, ArrayList<String> groups,
                ArrayList<ArrayList<Acts>> childs) {
            this.context = context;
            this.groups = groups;
            this.childs = childs;
            inflater = LayoutInflater.from(context);
        }

        public Object getChild(int groupPosition, int childPosition) {
            return childs.get(groupPosition).get(childPosition);
        }

        public long getChildId(int groupPosition, int childPosition) {
            return (long) (groupPosition * 1024 + childPosition); // Max 1024
            // children per
            // group
        }

        public void setChilds(ArrayList<ArrayList<Acts>> childs) {
            this.childs = childs;
        }


        public View getChildView(int groupPosition, int childPosition,
                boolean isLastChild, View convertView, ViewGroup parent) {
            View v = null;
        long ShowID=AddActActivity.cuesheetnimber;

            EditText ACTNUMBER;
            EditText  ACTTITLE;
            TimePicker STARTTIME;
            TimePicker ENDTIME;
            EditText ACTIONDATA;
            EditText NOTE;

                if (convertView != null) {
                    v = convertView;

                    Log.d("dfg", groupPosition+"act set"+childPosition);

                ACTNUMBER = (EditText) v.findViewById(R.id.ActionNumber);
                ACTTITLE = (EditText) v.findViewById(R.id.ActionTitle);
                STARTTIME = (TimePicker) v.findViewById(R.id.starttime);

                ENDTIME = (TimePicker) v.findViewById(R.id.endttime);

                ACTIONDATA = (EditText) v.findViewById(R.id.Actionname);

                NOTE = (EditText) v.findViewById(R.id.Noteact);

                Acts acts = new Acts();
                acts.setShowID(ShowID + "");
                acts.setACTIONDATA(ACTIONDATA.getText().toString());
                acts.setACTNUMBER(ACTNUMBER.getText().toString());
                acts.setNOTE(NOTE.getText().toString());
                acts.setACTTITLE(ACTTITLE.getText().toString());
                acts.setENDTIME(ENDTIME.getCurrentHour().toString() + "x"
                        + ENDTIME.getCurrentMinute().toString());
                acts.setSTARTTIME(STARTTIME.getCurrentHour().toString() + 

"x"
                        + STARTTIME.getCurrentMinute().toString());
                actlist.add(acts);



                } else {Log.d("dfg", "chil null");
                actlist.clear();
                    v = inflater.inflate(R.layout.act_child_row, parent, 

false);
                }





            return v;
        }

        public int getChildrenCount(int groupPosition) {
            if (groupPosition > (groups.size() - 1))
                return 0;
            return childs.get(groupPosition).size();
        }

        public Object getGroup(int groupPosition) {
            return groups.get(groupPosition);
        }

        public int getGroupCount() {
            return groups.size();
        }

        public long getGroupId(int groupPosition) {
            return (long) (groupPosition * 1024); // To be consistent with
            // getChildId
        }

        static int po = 0;
        public static ArrayList<Acts> actlist = new ArrayList<Acts>();
        public View getGroupView(int groupPosition, boolean isExpanded,
                View convertView, ViewGroup parent) {

            po++;
            View v = null;


        if (convertView != null) {
                v = convertView;

    Log.d("dfg", "grp reloaded");
            } else {

                v = inflater.inflate(R.layout.act_group_row, parent, false);

                Log.d("dfg", "grp created");    
            }


    ;

            return v;
        }
     public static void save()
     {}
        public boolean hasStableIds() {
            return true;
        }

        public boolean isChildSelectable(int groupPosition, int childPosition) {
            return true;
        }

        public void addHcwGroup(ExpandableListView expandableListView) {


                childs.get(0).add(new Acts());
                notifyDataSetChanged();


        }

        public void onGroupCollapsed(int groupPosition) {
        }

        public void onGroupExpanded(int groupPosition) {
        }

    }

感谢您的帮助

【问题讨论】:

  • 尝试在增量过程的每个步骤记录内容,以查看出现问题的具体步骤。
  • 你好,芒果,我做同样的事情,但找不到任何不规则的数据。你想让我把代码发给你吗?
  • dropbox.com/sh/7qcvp6uxzl0cs4u/TAzYrEfMi3 这里是代码生成路径是 create->next->add act button
  • next 按钮在哪里?这就是我所看到的:imageshack.us/photo/my-images/194/capturearj.png
  • 我在 android tab 10 中测试,所以对于你的模拟器,视图被剪切,你可以调整 xml 布局。对不起

标签: java android listview expandablelistview


【解决方案1】:

抱歉,耽搁了这么久。我已经测试了代码,似乎与这个问题有关,没有什么是真正错误的。我的系统上发生的事情是 ExapandableListActivity 加载,然后我单击添加按钮,首先加载 2 个子行,然后加载 1 个子行。 但是您的适配器启动时准备好加载一行。 AND您必须意识到您的ExpandableListView 初始化折叠。您没有调用 .expandGroup(); 方法,直到单击的按钮 AFTER 您将第二行添加到已经存在的那一行。我希望这是有道理的。也许它不是那么明显,因为您的 groupView 什么也没显示。

基本上,我的建议是在调用setListAdapter(); 之后立即调用.expandGroup(); 命令以使事情更加明显。那么应该很明显你需要调整什么才能得到你想要的结果。


我完全道歉,似乎我完全误解了这个问题。我相信您希望能够自动将您在 editTexts 中选择的所有值保存在可扩展列表中。如果是这样,我会告诉你,你真的应该重新考虑你的设计。为此使用列表视图的问题是,当一行离开视图时,它就永远消失了,您需要重新制作它——因此视图中的所有值都将消失。当然有convertView,但这并不可靠,因为它绝对不会将最近删除的视图放回相同的位置,无论如何都不是故意的。一旦它们不在视线范围内,它会继续对您的子行进行洗牌,我敢肯定您不想要。您的方法肯定会给您带来问题,因为您一直在清除 getView 中的集合,而且您还没有考虑在用户“完成”输入数据时如何捕获数据。

但是,如果这就是您想要的方式,您可以尝试将 textwatchers 应用于所有内容,并让它们不断调整集合中的相应值。该技术在Yami's 的答案中突出显示:How to get values from grid view

但是我会觉得这太费力了。您可能会发现制作 TableLayout 更有益,因为它将所有内容都保存在内存中,您可以将所有视图添加到布局中,然后将这些视图添加到相应的集合中,然后您可以简单地循环遍历 tablelayout 的内容来检索您的价值观。这是一种选择,尽管如果不加以管理,您的 heapsize 可能会受到影响。

更好的性能是在ExpandableListView 的每个子行上合并一个按钮,这样只有在单击该按钮时,才能将相应行的内容保存到集合中。

到目前为止,我只是给你我能想到的解决方案,但如果你亲自问我会怎么做,我不会为此使用 ListView 类型。我会将此组件视为一种形式,我会尝试将其作为Dialog。然后,如果我选择,我可以简单地将一整套详细信息显示为单独的列表,但只有一个 xml 膨胀用于输入详细信息。

【讨论】:

  • 我添加了 exapndgroup ,请参阅 AddActActivity 类但没有任何好处
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-06-21
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多