添加动态孩子ExpandableListView(如记录被添加) [英] Adding dynamic children to ExpandableListView (as records are added)

查看:117
本文介绍了添加动态孩子ExpandableListView(如记录被添加)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要动态的孩子加入到扩展列表视图。
之后,我在addshare活动添加我的纪录,我去传递一个String对象的另一个活动(dynamic1)。
在这里我添加了新的纪录,String对象为参数,并通过意向回MainActivity。
investment_summary_main.xml包含扩展列表视图。

code对于DYNAMIC1:

 公共类dynamic1延伸活动{
MyCustomAdapter C =新MyCustomAdapter();
字符串s;
 私人ExpandableListView mExpandableList;
公共无效的onCreate(捆绑savedInstanceState){
      super.onCreate(savedInstanceState);
      的setContentView(R.layout.investment_summary_main);
      如果(getIntent()。getExtras()!= NULL)
      {意向意图= getIntent();
       S = intent.getExtras()的getString(K);
      }
      mExpandableList =(ExpandableListView)this.findViewById(R.id.expandable_list);
      ExpandableListAdapter适配器=(ExpandableListAdapter)mExpandableList.getExpandableListAdapter();
((父)adapter.getGroup(0))getArrayChildren()加(S)。
((BaseExpandableListAdapter)适配器).notifyDataSetChanged();
意图I =新意图(这一点,MainActivity.class);
startActivity(ⅰ);
}
}

code对于ADDSHARE:

 公共类addshare延伸活动{
DBAdapter DB =新DBAdapter(本);
MyCustomAdapter C =新MyCustomAdapter();
 私人ExpandableListView mExpandableList;
  公共无效addshare(){}@覆盖
 公共无效的onCreate(捆绑savedInstanceState){
      super.onCreate(savedInstanceState);
      的setContentView(R.layout.add);
}    公共无效addingshare(视图V)
    {
        Log.d(测试,添加);
        //从表格数据
        //的EditText intypeid =(EditText上)findViewById(R.id.intypeid);
        共享名的EditText =(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);
        浮法投资总额= 0F,currmarketvalue = 0F;
           。字符串SNAME = sharename.getText()的toString();         / *意向书I =新意图(这一点,dynamicchild.class);
           i.putExtra(K,SNAME);
           startActivity(ⅰ);
          * /           。字符串S1 = sharepri.getText()的toString();
           浮N1 = Float.parseFloat(S1);           。字符串s2 = purpri.getText()的toString();
           浮N2 = Float.parseFloat(S2);         // shares_equities S =新shares_equities(SNAME);
           。串S = shareno.getText()的toString();
           整数的i1 =的Integer.parseInt(S3);
           db.open();
           长ID = db.addRecord(SNAME,shareid.getText()。的toString()
                 N2,N1,I1,purdate.getText()。的toString(),purplace.getText()。的toString(),purcon.getText()。的toString()
                 ,投资总额,currmarketvalue);           db.close();        sharename.setText();
        shareid.setText();
        purpri.setText();
        sharepri.setText();
        shareno.setText();
        purdate.setText();
        purplace.setText();
        purcon.setText();
        Toast.makeText(addshare.this,共享记录增加,Toast.LENGTH_LONG).show();
        意图I =新意图(这一点,dynamic1.class);
           i.putExtra(K,SNAME);
           startActivity(ⅰ);    }
     公共无效viewAssignments(视图V)
    {
    意图I =新意图(这一点,MainActivity.class);
    startActivity(ⅰ);
    } }

code用于investment_summary_main.xml

 <?XML版本=1.0编码=UTF-8&GT?;
  <滚动型的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android          机器人:layout_width =FILL_PARENT
          机器人:layout_height =FILL_PARENT
          机器人:ID =@ + ID / SLIST
           >   <的LinearLayout
       机器人:layout_width =FILL_PARENT
       机器人:layout_height =WRAP_CONTENT
       机器人:方向=垂直>
       < ExpandableListView
           机器人:ID =@ + ID / expandable_list
           机器人:layout_width =FILL_PARENT
           机器人:layout_height =400dp
           机器人:layout_weight =2900208.75
           机器人:cacheColorHint =#00000000
           机器人:listSelector =@机器人:彩色/透明
           机器人:transcriptMode =已禁用/>       <按钮
           机器人:ID =@ + ID /的AddNew
           机器人:layout_width =WRAP_CONTENT
           机器人:layout_height =WRAP_CONTENT
           机器人:layout_gravity =中心
           机器人:layout_margin =5DP
           机器人:文字=新增
           安卓的onClick =sendMessage_addrecord/>
   < / LinearLayout中>

终于ERROR我得到当添加记录:

  1月6日至23日:40:40.997:E / AndroidRuntime(484):致命异常:主要
一月6日至23日:40:40.997:E / AndroidRuntime(484):了java.lang.RuntimeException:无法启动活动ComponentInfo {com.example.moolah / com.example.moolah.dynamic1}:显示java.lang.NullPointerException
一月6日至23日:40:40.997:E / AndroidRuntime(484):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1955)
一月6日至23日:40:40.997:E / AndroidRuntime(484):在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1980)
一月6日至23日:40:40.997:E / AndroidRuntime(484):在android.app.ActivityThread.access $ 600(ActivityThread.java:122)
一月6日至23日:40:40.997:E / AndroidRuntime(484):在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1146)
一月6日至23日:40:40.997:E / AndroidRuntime(484):在android.os.Handler.dispatchMessage(Handler.java:99)
一月6日至23日:40:40.997:E / AndroidRuntime(484):在android.os.Looper.loop(Looper.java:137)
一月6日至23日:40:40.997:E / AndroidRuntime(484):在android.app.ActivityThread.main(ActivityThread.java:4340)
一月6日至23日:40:40.997:E / AndroidRuntime(484):在java.lang.reflect.Method.invokeNative(本机方法)
一月6日至23日:40:40.997:E / AndroidRuntime(484):在java.lang.reflect.Method.invoke(Method.java:511)
一月6日至23日:40:40.997:E / AndroidRuntime(484):在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:784)
一月6日至23日:40:40.997:E / AndroidRuntime(484):在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
一月6日至23日:40:40.997:E / AndroidRuntime(484):在dalvik.system.NativeStart.main(本机方法)
一月6日至23日:40:40.997:E / AndroidRuntime(484):致:显示java.lang.NullPointerException
一月6日至23日:40:40.997:E / AndroidRuntime(484):在com.example.moolah.dynamic1.onCreate(dynamic1.java:31)
一月6日至23日:40:40.997:E / AndroidRuntime(484):在android.app.Activity.performCreate(Activity.java:4465)
一月6日至23日:40:40.997:E / AndroidRuntime(484):在android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
一月6日至23日:40:40.997:E / AndroidRuntime(484):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1919)
一月6日至23日:40:40.997:E / AndroidRuntime(484):11 ...更多


解决方案

好吧,检查你的整个code后,我已经看到了你初始化了你自定义适配器是这样的:

  //注意,这是一个空的构造
MyCustomAdapter C =新MyCustomAdapter();

看你的 MyCustomAdapter.java 我发现有两个构造这个类。一个是空的构造函数,另一个是这样的:

  //你应该用这个来代替
公共MyCustomAdapter(上下文的背景下,ArrayList的<母体GT;母公司){
         mParent =父母;
         C =背景;
         充气= LayoutInflater.from(上下文);
}

在你的 dynamic1 活动在初始化 C 与不接受空构造背景,因此,当你调用:

  INT I = c.getChildrenCount(0);

您会得到NPE因为是<大骨节病>空。要排序出来,实例化 C 以正确的构造函数:

  MyCustomAdapter C =新MyCustomAdapter(YourActivityContext,ParentArrayList);

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

解决方案

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);

这篇关于添加动态孩子ExpandableListView(如记录被添加)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆