3

I need to dynamically add children to expandable list view. AFter i add my record in addshare activity, i go to another activity(dynamic1) passing a String object. Over here i add the new record with the String object as parameter, and pass intent back to MainActivity. investment_summary_main.xml contains the expandable list view.

CODE FOR DYNAMIC1:

public class dynamic1 extends Activity{
MyCustomAdapter c=new MyCustomAdapter();
String s;
 private ExpandableListView mExpandableList;
public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.investment_summary_main);
      if(getIntent().getExtras()!=null)
      { Intent intent = getIntent();
       s = intent.getExtras().getString("k");
      }
      mExpandableList= (ExpandableListView) this.findViewById(R.id.expandable_list);
      ExpandableListAdapter adapter =  (ExpandableListAdapter) mExpandableList.getExpandableListAdapter();
((parent)adapter.getGroup(0)).getArrayChildren().add(s);
((BaseExpandableListAdapter) adapter).notifyDataSetChanged();
Intent i = new Intent(this, MainActivity.class);
startActivity(i);
}
}

CODE FOR ADDSHARE:

public class addshare extends Activity{
DBAdapter db = new DBAdapter(this);
MyCustomAdapter c=new MyCustomAdapter();
 private ExpandableListView mExpandableList;
  public void addshare(){}

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

    public void addingshare(View v)
    {
        Log.d("test", "adding");
        //get data from form
        //EditText intypeid = (EditText)findViewById(R.id.intypeid);
        EditText sharename= (EditText)findViewById(R.id.sharename);
        EditText shareid = (EditText)findViewById(R.id.shareid);
        EditText purpri = (EditText)findViewById(R.id.purpri);
        EditText sharepri = (EditText)findViewById(R.id.sharepri);
        EditText shareno = (EditText)findViewById(R.id.shareno);
        EditText purdate = (EditText)findViewById(R.id.purdate);
        EditText purplace = (EditText)findViewById(R.id.purplace);
        EditText purcon = (EditText)findViewById(R.id.purcon);
        Float totalinvestment=0F,currmarketvalue=0F;


           String sname=sharename.getText().toString();

         /*  Intent i = new Intent(this, dynamicchild.class);
           i.putExtra("k", sname);
           startActivity(i); 
          */

           String s1=sharepri.getText().toString();
           Float n1=Float.parseFloat(s1);

           String s2=purpri.getText().toString();
           Float n2=Float.parseFloat(s2);

         //  shares_equities s=new shares_equities( sname);
           String s3=shareno.getText().toString();
           Integer i1=Integer.parseInt(s3);
           db.open();        
           long id = db.addRecord(sname, shareid.getText().toString(),
                 n2,n1,i1, purdate.getText().toString(),purplace.getText().toString(),purcon.getText().toString()
                 ,totalinvestment,currmarketvalue);  

           db.close();

        sharename.setText("");
        shareid.setText("");
        purpri.setText("");
        sharepri.setText("");
        shareno.setText("");
        purdate.setText("");
        purplace.setText("");
        purcon.setText("");


        Toast.makeText(addshare.this,"share record Added", Toast.LENGTH_LONG).show();  


        Intent i = new Intent(this, dynamic1.class);
           i.putExtra("k", sname);
           startActivity(i);

    }
     public void viewAssignments(View v)
    {
    Intent i = new Intent(this, MainActivity.class);
    startActivity(i);
    }

 }

CODE FOR investment_summary_main.xml

  <?xml version="1.0" encoding="utf-8"?>
  <ScrollView xmlns:android="http://schemas.android.com/apk/res/android"

          android:layout_width="fill_parent"
          android:layout_height="fill_parent"
          android:id="@+id/slist"
           >

   <LinearLayout
       android:layout_width="fill_parent"
       android:layout_height="wrap_content"
       android:orientation="vertical" >


       <ExpandableListView
           android:id="@+id/expandable_list"
           android:layout_width="fill_parent"
           android:layout_height="400dp"
           android:layout_weight="2900208.75"
           android:cacheColorHint="#00000000"
           android:listSelector="@android:color/transparent"
           android:transcriptMode="disabled" />

       <Button
           android:id="@+id/addnew"
           android:layout_width="wrap_content"
           android:layout_height="wrap_content"
           android:layout_gravity="center"
           android:layout_margin="5dp"
           android:text="ADD NEW"
           android:onClick="sendMessage_addrecord" />
   </LinearLayout>

FINALLY THE ERROR I GET WHEN ADDING RECORD:

06-23 01:40:40.997: E/AndroidRuntime(484): FATAL EXCEPTION: main
06-23 01:40:40.997: E/AndroidRuntime(484): java.lang.RuntimeException: Unable to start activity      ComponentInfo{com.example.moolah/com.example.moolah.dynamic1}: java.lang.NullPointerException
06-23 01:40:40.997: E/AndroidRuntime(484):  at    android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1955)
06-23 01:40:40.997: E/AndroidRuntime(484):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1980)
06-23 01:40:40.997: E/AndroidRuntime(484):  at android.app.ActivityThread.access$600(ActivityThread.java:122)
06-23 01:40:40.997: E/AndroidRuntime(484):  at    android.app.ActivityThread$H.handleMessage(ActivityThread.java:1146)
06-23 01:40:40.997: E/AndroidRuntime(484):  at android.os.Handler.dispatchMessage(Handler.java:99)
06-23 01:40:40.997: E/AndroidRuntime(484):  at android.os.Looper.loop(Looper.java:137)
06-23 01:40:40.997: E/AndroidRuntime(484):  at android.app.ActivityThread.main(ActivityThread.java:4340)
06-23 01:40:40.997: E/AndroidRuntime(484):  at java.lang.reflect.Method.invokeNative(Native Method)
06-23 01:40:40.997: E/AndroidRuntime(484):  at java.lang.reflect.Method.invoke(Method.java:511)
06-23 01:40:40.997: E/AndroidRuntime(484):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
06-23 01:40:40.997: E/AndroidRuntime(484):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
06-23 01:40:40.997: E/AndroidRuntime(484):  at dalvik.system.NativeStart.main(Native Method)
06-23 01:40:40.997: E/AndroidRuntime(484): Caused by: java.lang.NullPointerException
06-23 01:40:40.997: E/AndroidRuntime(484):  at com.example.moolah.dynamic1.onCreate(dynamic1.java:31)
06-23 01:40:40.997: E/AndroidRuntime(484):  at android.app.Activity.performCreate(Activity.java:4465)
06-23 01:40:40.997: E/AndroidRuntime(484):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
06-23 01:40:40.997: E/AndroidRuntime(484):  at     android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1919)
06-23 01:40:40.997: E/AndroidRuntime(484):  ... 11 more
user2468835
  • 79
  • 1
  • 7

2 Answers2

3

Alright, after checking your entire code I've seen that you initialize your custom adapter this way:

// notice that it's an empty constructor
MyCustomAdapter c = new MyCustomAdapter();

Looking at your MyCustomAdapter.java I've found there are two constructors for this class. One is the empty constructor and the other is this:

// You should use this one instead
public MyCustomAdapter(Context context, ArrayList<parent> parent){
         mParent = parent;
         c = context;
         inflater = LayoutInflater.from(context);
}

In your dynamic1 activity you initialize the c with the empty constructor which doesn't receive parent and context, hence when you call:

int  i = c.getChildrenCount(0);

You'll get NPE because the parent is null. To sort it out, instantiate the c with correct constructor:

MyCustomAdapter c = new MyCustomAdapter(YourActivityContext, ParentArrayList);
Sam R.
  • 16,027
  • 12
  • 69
  • 122
  • THanks. that made sense.:) – user2468835 Jun 24 '13 at 06:42
  • But now i get index out of bounds exception for getGroup :( – user2468835 Jun 24 '13 at 06:44
  • @user2468835, Debug your code. Obviously you are referring to an array index which is not there. – Sam R. Jun 24 '13 at 09:59
  • True that. it gets into the getGroup() function of custom adapter. but fails there according to log. if i initialize list items before hand, it turns into a null pointer exception :/ – user2468835 Jun 24 '13 at 10:06
  • i get why the exceptions occur actually. makes sense, but I can't find a way around them. – user2468835 Jun 24 '13 at 10:09
  • @SamRad thanks for ur look here...do u have any idea how user2468835 supporting list view within scrollview container..or have any suggestion in this direction? – CoDe Dec 07 '13 at 11:58
  • @Shubh, have you seen [`How can I put a ListView into a ScrollView`](http://stackoverflow.com/q/3495890/1693859)? – Sam R. Dec 07 '13 at 18:19
  • Yes...solution is not up to the mark...as most of time it not draw last item...because of error in total height calculation. http://stackoverflow.com/a/3495908/2624806 – CoDe Dec 09 '13 at 04:39
1

It appears that you never assigned an Adapter so the list doesn't have one to return to you. Look at this tutorial on how to implement a SimpleExpandableListAdapter.

JRomero
  • 4,878
  • 1
  • 27
  • 49
  • But i've defined an expandable list adapter and not a simple expandable list adapter. Both have different implementations right? – user2468835 Jun 23 '13 at 07:41
  • I tried that, and a lot of meddling. wasted hours :( seems there's some fundamental trouble with the declaration of adapter. – user2468835 Jun 23 '13 at 08:45