我正在开发一个必须ExpandableListView
在运行时添加新组的 Android 应用程序。我正在做的是,我有一个EditText
和一个Button
。当用户写入EditText
并单击时Button
,应将其添加ExpandableListView
为新组(父组)。这是我的代码。需要做什么?
ExpandableDemo.java
public class ExpandableDemo extends Activity implements OnChildClickListener{
private ArrayList<String> parentItems = new ArrayList<String>();
private ArrayList<Object> childItems = new ArrayList<Object>();
ExpandableListView expandableList;
String s[]=new String[]{"New List"};
public static Session friendSession;
final private static int DIALOG_LOGIN = 1;
String listData;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// this is not really necessary as ExpandableListActivity contains an ExpandableList
setContentView(R.layout.expandable);
expandableList = (ExpandableListView)findViewById(R.id.listex); // you can use (ExpandableListView) findViewById(R.id.list)
expandableList.setDividerHeight(2);
expandableList.setGroupIndicator(null);
expandableList.setClickable(true);
setGroupParents();
setChildData();
MyExpandableAdapter adapter = new MyExpandableAdapter(parentItems, childItems);
adapter.setInflater((LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE), this);
expandableList.setAdapter(adapter);
expandableList.setOnChildClickListener(this);
expandableList.setOnGroupClickListener(new OnGroupClickListener()
{
public boolean onGroupClick(ExpandableListView arg0, View arg1, int groupPosition, long arg3)
{
// Toast.makeText(getBaseContext(), "" + groupPosition, 1000).show();
if(groupPosition == 0)
{
showDialog(DIALOG_LOGIN);
}
else
{
}
return false;
}
});
}
public void setGroupParents() {
parentItems.add("New Friend List");
}
public void setChildData() {
ArrayList<String> child = new ArrayList<String>();
//child.add("");
childItems.add(child);
}
@Override
public boolean onChildClick(ExpandableListView parent, View v,
int groupPosition, int childPosition, long id) {
// TODO Auto-generated method stub
return false;
}
@Override
protected Dialog onCreateDialog(int id)
{
AlertDialog dialogDetails = null;
switch (id)
{
case DIALOG_LOGIN:
LayoutInflater inflater = LayoutInflater.from(this);
View dialogview = inflater.inflate(R.layout.dialog, null);
AlertDialog.Builder dialogbuilder = new AlertDialog.Builder(this);
dialogbuilder.setView(dialogview);
dialogDetails = dialogbuilder.create();
break;
}
return dialogDetails;
}
@Override
protected void onPrepareDialog(int id, Dialog dialog) {
switch (id) {
case DIALOG_LOGIN:
final AlertDialog alertDialog = (AlertDialog) dialog;
Button btnOk = (Button) alertDialog.findViewById(R.id.btnOk);
final EditText etList = (EditText) alertDialog.findViewById(R.id.etList);
btnOk.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if(etList.length() <= 0)
{
etList.requestFocus();
Toast.makeText(getBaseContext(), "Enter Name of List", 1000).show();
}
else
{
listData = etList.getText().toString();// This should be name of new group
dismissDialog(DIALOG_LOGIN);
etList.setText("");
}
}
});
break;
}
}
}
MyExpandableAdapter.java
public class MyExpandableAdapter extends BaseExpandableListAdapter {
private Activity activity;
private ArrayList<Object> childtems;
private LayoutInflater inflater;
private ArrayList<String> parentItems, child;
public MyExpandableAdapter(ArrayList<String> parents, ArrayList<Object> childern) {
this.parentItems = parents;
this.childtems = childern;
}
public void setInflater(LayoutInflater inflater, Activity activity) {
this.inflater = inflater;
this.activity = activity;
}
@Override
public View getChildView(final int groupPosition, final int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
child = (ArrayList<String>) childtems.get(groupPosition);
child.get(groupPosition);
TextView textView = null;
if (convertView == null) {
convertView = inflater.inflate(R.layout.group, null);
}
textView = (TextView) convertView.findViewById(R.id.textView1);
textView.setText(child.get(childPosition));
convertView.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
Toast.makeText(activity, child.get(childPosition),
Toast.LENGTH_SHORT).show();
}
});
return convertView;
}
@Override
public View getGroupView(final int groupPosition, boolean isExpanded, View convertView, final ViewGroup parent) {
if (convertView == null) {
convertView = inflater.inflate(R.layout.row, null);
}
if(groupPosition == 0)
{
((CheckedTextView) convertView).setCompoundDrawablesWithIntrinsicBounds(R.drawable.add, 0, 0, 0);
((CheckedTextView) convertView).setPadding(30, 0, 0, 0);
((CheckedTextView) convertView).setText(parentItems.get(groupPosition));
((CheckedTextView) convertView).setChecked(isExpanded);
}
else
{
((CheckedTextView) convertView).setCompoundDrawablesWithIntrinsicBounds(R.drawable.right, 0, 0, 0);
((CheckedTextView) convertView).setText(parentItems.get(groupPosition));
((CheckedTextView) convertView).setChecked(isExpanded);
((CheckedTextView) convertView).setPadding(30, 0, 0, 0);
if(((CheckedTextView) convertView).isChecked() == true)
{
((CheckedTextView) convertView).setCompoundDrawablesWithIntrinsicBounds(R.drawable.down, 0, 0, 0);
}
}
return convertView;
}
@Override
public Object getChild(int groupPosition, int childPosition) {
return null;
}
@Override
public long getChildId(int groupPosition, int childPosition) {
return 0;
}
@Override
public int getChildrenCount(int groupPosition) {
return ((ArrayList<String>) childtems.get(groupPosition)).size();
}
@Override
public Object getGroup(int groupPosition) {
return null;
}
@Override
public int getGroupCount() {
return parentItems.size();
}
@Override
public void onGroupCollapsed(int groupPosition) {
super.onGroupCollapsed(groupPosition);
}
@Override
public void onGroupExpanded(int groupPosition) {
super.onGroupExpanded(groupPosition);
}
@Override
public long getGroupId(int groupPosition) {
return 0;
}
@Override
public boolean hasStableIds() {
return false;
}
@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return false;
}
}