android-将动态子级添加到ExpandableListView(添加记录时)
作者:互联网
我需要将子级动态添加到可扩展列表视图中.
在将记录添加到addshare活动之后,我转到另一个活动(dynamic1),该活动传递一个String对象.
在这里,我以String对象作为参数添加新记录,并将意图传递回MainActivity.
investment_summary_main.xml包含可扩展列表视图.
动态代码1:
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);
}
}
广告代码:
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>
最终我在记录时遇到的错误:
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
解决方法:
好了,检查完整个代码后,我发现您可以通过以下方式初始化自定义适配器:
// notice that it's an empty constructor
MyCustomAdapter c = new MyCustomAdapter();
查看MyCustomAdapter.java,我发现该类有两个构造函数.一个是空的构造函数,另一个是这个:
// You should use this one instead
public MyCustomAdapter(Context context, ArrayList<parent> parent){
mParent = parent;
c = context;
inflater = LayoutInflater.from(context);
}
在您的dynamic1活动中,使用不接收父项和上下文的空构造函数初始化c,因此在调用时:
int i = c.getChildrenCount(0);
您将获得NPE,因为父级为null.为了解决这个问题,请使用正确的构造函数实例化c:
MyCustomAdapter c = new MyCustomAdapter(YourActivityContext, ParentArrayList);
标签:expandablelistview,android 来源: https://codeday.me/bug/20191030/1970238.html