Android 向ExpandableListView添加动态子项(添加记录时)
我需要动态地将子项添加到可扩展列表视图中。 在addshare活动中添加记录后,我转到另一个活动(dynamic1),传递字符串对象。 在这里,我以String对象作为参数添加新记录,并将意图传递回MainActivity。 investment_summary_main.xml包含可扩展列表视图 动态代码1:Android 向ExpandableListView添加动态子项(添加记录时),android,expandablelistview,Android,Expandablelistview,我需要动态地将子项添加到可扩展列表视图中。 在addshare活动中添加记录后,我转到另一个活动(dynamic1),传递字符串对象。 在这里,我以String对象作为参数添加新记录,并将意图传递回MainActivity。 investment_summary_main.xml包含可扩展列表视图 动态代码1: public class dynamic1 extends Activity{ MyCustomAdapter c=new MyCustomAdapter(); String s; p
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);
}
}
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);
}
}
投资代码\u summary\u 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>
您似乎从未分配适配器,因此列表中没有可返回的适配器。查看如何实现。好的,在检查了整个代码后,我发现您可以通过以下方式初始化自定义适配器:
// 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);
}
您将获得NPE,因为父项为空。要进行分类,请使用正确的构造函数实例化c
:
MyCustomAdapter c = new MyCustomAdapter(YourActivityContext, ParentArrayList);
第31行错误为:((父)适配器.getGroup(0)).getArrayChildren().add;亲爱的@user2468835,您的列表视图在scrollview容器中是如何工作的…它支持吗?但是我定义了一个可扩展列表适配器,而不是一个简单的可扩展列表适配器。两者都有不同的实现,对吗?我试过了,而且还做了很多干预。浪费的时间:(adapter.Thank的声明似乎有一些根本性的问题。这是有意义的。)但现在我得到了getGroup的索引越界异常:(@user2468835,调试代码。显然你指的是一个不存在的数组索引。这是真的。它进入getGroup()自定义适配器的函数。但根据日志,该函数失败。如果我事先初始化列表项,它将变成空指针异常:/i我了解异常实际发生的原因。有道理,但我找不到解决方法。
int i = c.getChildrenCount(0);
MyCustomAdapter c = new MyCustomAdapter(YourActivityContext, ParentArrayList);