Kishor datta gupta
Kishor datta gupta

Reputation: 1103

Android Expandable list view help when add new row in list

i have made a expandable view with only one group, upon click a one button, A new

row in that group added, these row contain some data in textbox, when input taking

from rows, i find i get 7 rows input when i add add 4 rows , always like this

ration, when add 5 rows i get 8rows input like, did not understand tried to do many

tricks.

Can u help me what is the best way to take input from these rows. actlist is the array list which taking the inputs. in below there is the code activity

    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) {
        }

    }

Thank u for trying to help

Upvotes: 0

Views: 1404

Answers (1)

mango
mango

Reputation: 5636

I'm sorry for a long delay. I've tested the code and it seems to be that pertaining to this question, nothing much is really erroneous. What is happening on my system is that the ExapandableListActivity loads, then i click the add button and 2 child rows will load at first and then 1 child row after that. BUT your adapter initiated with one row ready to be loaded. AND what you must realize is that your ExpandableListView initializes collapsed. you didn't call the .expandGroup(); method till the button what clicked which is AFTER you added a second row to the one already there. I hope that made sense. perhaps it wasn't so obvious because your groupView shows nothing.

Basically my advice would be to call on the .expandGroup(); command immediately after you call on setListAdapter(); to make things more apparent. then it should be apparent what you need to adjust in order to get the results you want.


i completely apologize, it seems that i completely misunderstood the question. i believe that you want to be able to automatically save all the values selected in your editTexts in the expandableList. If so, i will tell you that you should really re-think your design. The problem with using a listview for this is that when a row goes out of view it is gone for good, you need to remake it - so all the values within the views would be gone. Of course there's convertView but it is unreliable for this because it absolutely will not put the recently removed view back in the same position, not on purpose anyway. It will keep shuffling your child rows once they're out of view, which i'm sure you don't want. your method would surely give you issues because you kept clearing the collection in getView and further more you haven't considered how to capture data when the user has "finished" inputting it.

But if that's how you want it anyway, you could try applying textwatchers to everything and have them constantly adjust corresponding values in collections. The technique was highlighted in Yami's answer here: How to get values from grid view.

However this i would find far too laborious. You might find making a TableLayout more beneficial as it would keep everything in memory and you could add all the views to the layout and then add these views into respective collections and then you could simply loop through the content of the tablelayout to retrieve your values. This is one alternative, though your heapsize could suffer if not managed somewhat.

Better performance-wise would be to incorporate a button on each childrow of your ExpandableListView, so that only upon click of the button, you save the contents of the corresponding row to a collection.

Up till now, i'm only giving you the solutions that i can think of but if you were to ask me personally how i'd go about this, I wouldn't use a type of ListView for this. I would perceive this component as a form and i would try to have this as a Dialog instead. Then i could simply show an entire set of details as a separate list if i chose, but only have the one xml inflation for inputting details.

Upvotes: 1

Related Questions