MY ADAPTER
public class ExpandableListAdapter extends BaseExpandableListAdapter {
private Context _context;
private List<String> _listDataHeader; // header titles
// child data in format of header title, child title
private HashMap<String, List<String>> _listDataChild;
public ExpandableListAdapter(Context context, List<String> listDataHeader,
HashMap<String, List<String>> listChildData) {
this._context = context;
this._listDataHeader = listDataHeader;
this._listDataChild = listChildData;
}
@Override
public Object getChild(int groupPosition, int childPosititon) {
return this._listDataChild.get(this._listDataHeader.get(groupPosition))
.get(childPosititon);
}
@Override
public long getChildId(int groupPosition, int childPosition) {
return childPosition;
}
@Override
public View getChildView(int groupPosition, final int childPosition,
boolean isLastChild, View convertView, ViewGroup parent) {
final String childText = (String) getChild(groupPosition, childPosition);
if (convertView == null) {
LayoutInflater infalInflater = (LayoutInflater) this._context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = infalInflater.inflate(R.layout.list_item, null);
}
TextView txtListChild = (TextView) convertView
.findViewById(R.id.lblListItem);
txtListChild.setText(childText);
return convertView;
}
@Override
public int getChildrenCount(int groupPosition) {
return this._listDataChild.get(this._listDataHeader.get(groupPosition))
.size();
}
@Override
public Object getGroup(int groupPosition) {
return this._listDataHeader.get(groupPosition);
}
@Override
public int getGroupCount() {
return this._listDataHeader.size();
}
@Override
public long getGroupId(int groupPosition) {
return groupPosition;
}
@Override
public View getGroupView(final int groupPosition, boolean isExpanded,
View convertView, ViewGroup parent) {
String headerTitle = (String) getGroup(groupPosition);
if (convertView == null) {
LayoutInflater infalInflater = (LayoutInflater) this._context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = infalInflater.inflate(R.layout.list_group, null);
}
TextView lblListHeader = (TextView) convertView
.findViewById(R.id.lblListHeader);
lblListHeader.setTypeface(null, Typeface.BOLD);
lblListHeader.setText(headerTitle);
return convertView;
}
@Override
public boolean hasStableIds() {
return false;
}
@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
}
}
MY ACTIVITY CODE
public class MainActivity extends Activity {
ExpandableListAdapter listAdapter;
ExpandableListView expListView;
List<String> listDataHeader;
HashMap<String, List<String>> listDataChild;
PopupWindow popUp;
Dialog picker;
Button edit;
Button set;
TimePicker timep;
Integer hour;
TextView time;
Context context;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.expendable);
// get the listview
expListView = (ExpandableListView) findViewById(R.id.lvExp);
// preparing list data
prepareListData();
listAdapter = new ExpandableListAdapter(this, listDataHeader, listDataChild);
// setting list adapter
expListView.setAdapter(listAdapter);
// Listview Group click listener
expListView.setOnGroupClickListener(new OnGroupClickListener() {
@Override
public boolean onGroupClick(ExpandableListView parent, View v,
int groupPosition, long id) {
// Toast.makeText(getApplicationContext(),
// "Group Clicked " + listDataHeader.get(groupPosition),
// Toast.LENGTH_SHORT).show();
return false;
}
});
// Listview Group expanded listener
expListView.setOnGroupExpandListener(new OnGroupExpandListener() {
@Override
public void onGroupExpand(int groupPosition) {
Toast.makeText(getApplicationContext(),
listDataHeader.get(groupPosition) + " Expanded",
Toast.LENGTH_SHORT).show();
}
});
// Listview Group collasped listener
expListView.setOnGroupCollapseListener(new OnGroupCollapseListener() {
@Override
public void onGroupCollapse(int groupPosition) {
Toast.makeText(getApplicationContext(),
listDataHeader.get(groupPosition) + " Collapsed",
Toast.LENGTH_SHORT).show();
}
});
// Listview on child click listener
expListView.setOnChildClickListener(new OnChildClickListener() {
@Override
public boolean onChildClick(ExpandableListView parent, View v,
int groupPosition, int childPosition, long id) {
// TODO Auto-generated method stub
Toast.makeText(
getApplicationContext(),
listDataHeader.get(groupPosition)
+ " : "
+ listDataChild.get(
listDataHeader.get(groupPosition)).get(
childPosition), Toast.LENGTH_SHORT)
.show();
Intent child0Intent = new Intent(getApplication(), Picktime.class);
startActivity(child0Intent);
return false;
}
});
}
/*
* Preparing the list data
*/
private void prepareListData() {
listDataHeader = new ArrayList<String>();
listDataChild = new HashMap<String, List<String>>();
// Adding child data
listDataHeader.add("Light");
listDataHeader.add("Television");
listDataHeader.add("Computer");
listDataHeader.add("Fan");
listDataHeader.add("Aircondition");
listDataHeader.add("Oven");
listDataHeader.add("Refridgerator");
listDataHeader.add("WaterHeater");
// Adding child data
List<String> Light = new ArrayList<String>();
Light.add("Power:14w/hr");
List<String> Television = new ArrayList<String>();
Television.add("Power:0.03w/hr");
List<String> Computer = new ArrayList<String>();
Computer.add("Power:100w/hr");
List<String> Fan = new ArrayList<String>();
Fan.add("Power:75w/hr");
List<String> Aircondition = new ArrayList<String>();
Aircondition.add("Power:1000w/hr");
List<String> Oven = new ArrayList<String>();
Oven.add("Power:2400w/hr");
List<String> Refridgerator = new ArrayList<String>();
Refridgerator.add("Power:180w/hr");
List<String> WaterHeater = new ArrayList<String>();
WaterHeater.add("Power:4000w/hr");
listDataChild.put(listDataHeader.get(0), Light); // Header, Child data
listDataChild.put(listDataHeader.get(1), Television);
listDataChild.put(listDataHeader.get(2), Computer);
listDataChild.put(listDataHeader.get(3), Fan);
listDataChild.put(listDataHeader.get(5), Aircondition);
listDataChild.put(listDataHeader.get(6), Oven);
listDataChild.put(listDataHeader.get(7), Refridgerator);
listDataChild.put(listDataHeader.get(7), WaterHeater);
}
}
MY NumerPicker CODE
public class Picktime extends MainActivity {
NumberPicker np;
TextView tv1;
Button btn;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.picker_frag);
np = (NumberPicker) findViewById(R.id.timePicker);
tv1 = (TextView) findViewById(R.id.editText);
btn = (Button) findViewById(R.id.btnSet);
np.setMinValue(1);
np.setMaxValue(100);
np.setWrapSelectorWheel(false);
np.setOnValueChangedListener(new OnValueChangeListener(){
@Override
public void onValueChange(NumberPicker picker, int oldVal, int newVal) {
// TODO Auto-generated method stub
tv1.setText("Hr Used:" + ( np.getValue()));
}
});
I TRIED TO DO A GET INTENT BUT I HAVE NO IDEA WHERE TO DO I INPUT IT IN
btn.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
NumberPicker numPicker = (NumberPicker)findViewById(R.id.timePicker);
int x = numPicker.getValue();
Intent i = new Intent( Picktime.this,
MainActivity.class);
i.putExtra("Value", x);
startActivity(i);
}
});
}
}
答案 0 :(得分:0)
在onCreate方法之后将其插入MainActivity Class:
首先,使用getIntent()方法获取已开始活动的意图:
Intent intent = getIntent();
如果您的额外数据表示为字符串,则可以使用intent.getStringExtra(String name)方法。 在你的情况下:它是int,所以使用
Integer in = intent.getIntExtra("Value",defaultValue);
defaultvalue是整数。