我正在为我的学校创建一个应用程序。它应该包含该机构提供的每门课程的讲义。现在该机构有三所不同的学校,每所学校至少有四个部门,所以我的主要活动有一个可扩展的列表视图,显示了点击学校时不同的部门。
我希望能够在单击特定子项(部门)时打开一个新活动,该活动在列表视图中填充该部门提供的课程。此外,当打开课程的新列表活动时,用户应该能够在另一个活动中查看该课程的讲义,而无需为每个部门和课程创建活动和布局。
这是我的主要活动
public class MainActivity extends Activity {
ExpandableListAdapter listAdapter;
ExpandableListView expListView;
List<String> listDataHeader;
HashMap<String, List<String>> listDataChild;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// 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();
return false;
}
});
}
/*
* Preparing the list data
*/
private void prepareListData() {
listDataHeader = new ArrayList<String>();
listDataChild = new HashMap<String, List<String>>();
// Adding child data
listDataHeader.add("School Of Applied Science");
listDataHeader.add("School Of Business And Management Studies");
listDataHeader.add("School Of Engineering");
// Adding child data
List<String> schoolOfAppliedScience = new ArrayList<String>();
schoolOfAppliedScience.add("Computer Science");
schoolOfAppliedScience.add("Statistics");
schoolOfAppliedScience.add("Science Laboratory Technology");
schoolOfAppliedScience.add("Hotel Catering And Institutional Management");
schoolOfAppliedScience.add("Fashion Design And Textiles");
List<String> BAMS = new ArrayList<String>();
BAMS.add("Secretaryship And Management Studies");
BAMS.add("Marketing");
BAMS.add("Accounting");
BAMS.add("Purchasing And Supply");
List<String> schOfEngineering = new ArrayList<String>();
schOfEngineering.add("Electrical Engineering");
schOfEngineering.add("Automotive Engineering");
schOfEngineering.add("Building Technology");
schOfEngineering.add("Civil Engineering");
listDataChild.put(listDataHeader.get(0), schoolOfAppliedScience); // Header, Child data
listDataChild.put(listDataHeader.get(1), BAMS);
listDataChild.put(listDataHeader.get(2), schOfEngineering);
}
}
这是我的适配器类
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(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;
}
}
答案 0 :(得分:1)
// 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
if (groupPosition == 0) {
if (childPosition == 0)
{
Intent c = new Intent(getApplicationContext(),ComputerScience.class);
startActivity(c);
}
if (childPosition == 1) {
Intent c = new Intent(getApplicationContext(), Statistics.class);
startActivity(c);
}
if (childPosition == 2) {
Intent c = new Intent(getApplicationContext(), ScienceLaboratoryTechnology.class);
startActivity(c);
}
if (childPosition == 3) {
Intent c = new Intent(getApplicationContext(),HotelCateringAndInstitutionalManagement.class);
startActivity(c);
}
if (childPosition == 4) {
Intent c = new Intent(getApplicationContext(),FashionDesignAndTextiles.class);
startActivity(c);
}
}
if (groupPosition == 1) {
if (childPosition == 0)
{
Intent c = new Intent(getApplicationContext(),SecretaryshipAndManagementStudies.class);
startActivity(c);
}
if (childPosition == 1) {
Intent c = new Intent(getApplicationContext(), Marketing.class);
startActivity(c);
}
if (childPosition == 2) {
Intent c = new Intent(getApplicationContext(), PurchasingAndSupply.class);
startActivity(c);
}
if (childPosition == 3) {
Intent c = new Intent(getApplicationContext(),HotelCateringAndInstitutionalManagement.class);
startActivity(c);
}
}
if (groupPosition == 2) {
if (childPosition == 0) {
Intent c = new Intent(getApplicationContext(), AutomotiveEngineering.class);
startActivity(c);
}
if (childPosition == 1) {
Intent c = new Intent(getApplicationContext(), BuildingTechnology.class);
startActivity(c);
}
if (childPosition == 2) {
Intent c = new Intent(getApplicationContext(), CivilEngineering.class);
startActivity(c);
}
}
return false;
}
});
答案 1 :(得分:0)
尝试android.widget.AdapterView.OnItemClickListener。
expListView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long arg3) {
StartIntent();
}
});
答案 2 :(得分:0)
您只需修改getChildView方法,添加 OnClickListener ,如下所示:
@Override
public View getChildView(final 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);
//-----------------------------------------------------------------------------------
txtListChild.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//put your groupPosition and childPosition to the intent and start the activity
startActivity(YOUR INTENT);
}
}//-----------------------------------------------------------------------------------
return convertView;
}
或者,您可以为子项布局设置setOnClickListener:
convertView.setOnClickListener(...)
答案 3 :(得分:0)
mExpandableList.setOnChildClickListener(new OnChildClickListener() {
@Override
public boolean onChildClick(ExpandableListView parent, View v,int groupPosition, int childPosition, long id) {
/*Additional info: You can use the view variable(v) here to get the params from the view you are clicking just in case you want to pass any data of that view to new activity(use casting to casting point to a particular view type). then pass that data as extra along with intent */
Intent intent=new Intent(CurrentClass.this,NewActivity.class)
startActivity(intent);
return true;
}
});
注意:不要忘记在清单中声明NewActivity
calss
答案 4 :(得分:0)
public class MyExpandableAdapter extends BaseExpandableListAdapter
{
private Activity activity;
private ArrayList<Object> childtems;
private LayoutInflater inflater;
private ArrayList<String> parentItems, child;
private Context context;
// constructor
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;
}
// method getChildView is called automatically for each child view.
// Implement this method as per your requirement
@Override
public View getChildView(final int groupPosition, final int childPosition, boolean isLastChild, View convertView, ViewGroup parent)
{
child = (ArrayList<String>) childtems.get(groupPosition);
TextView textView = null;
ImageView img = null;
if (convertView == null) {
convertView = inflater.inflate(R.layout.child_view, null);
}
// get the textView reference and set the value
textView = (TextView) convertView.findViewById(R.id.textViewChild);
textView.setText(child.get(childPosition));
img = (ImageView) convertView.findViewById(R.id.childImage);
if (childPosition == 0 && groupPosition==0) {
img.setImageResource(R.drawable.slika1);
}
if (childPosition == 1 && groupPosition==0) {
img.setImageResource(R.drawable.slika2);
}
if (childPosition == 2 && groupPosition==0) {
img.setImageResource(R.drawable.slika1);
}
if (childPosition == 3 && groupPosition==0) {
img.setImageResource(R.drawable.slika2);
}
if (childPosition == 4 && groupPosition==0) {
img.setImageResource(R.drawable.slika1);
}
if (childPosition == 5 && groupPosition==0) {
img.setImageResource(R.drawable.slika2);
}
if (childPosition == 6 && groupPosition==0) {
img.setImageResource(R.drawable.slika1);
}
if (childPosition == 7 && groupPosition==0) {
img.setImageResource(R.drawable.slika2);
}
if (childPosition == 8 && groupPosition==0) {
img.setImageResource(R.drawable.slika1);
}
if (childPosition == 0 && groupPosition==1) {
img.setImageResource(R.drawable.slika2);
}
if (childPosition == 1 && groupPosition==1) {
img.setImageResource(R.drawable.slika1);
}
if (childPosition == 2 && groupPosition==1) {
img.setImageResource(R.drawable.slika2);
}
if (childPosition == 3 && groupPosition==1) {
img.setImageResource(R.drawable.slika1);
}
if (childPosition == 0 && groupPosition==2) {
img.setImageResource(R.drawable.slika2);
}
if (childPosition == 1 && groupPosition==2) {
img.setImageResource(R.drawable.slika1);
}
if (childPosition == 2 && groupPosition==2 ) {
img.setImageResource(R.drawable.slika2);
}
if (childPosition == 3 && groupPosition==2) {
img.setImageResource(R.drawable.slika1);
}
if (childPosition == 0 && groupPosition==3) {
img.setImageResource(R.drawable.slika2);
}
if (childPosition == 1 && groupPosition==3) {
img.setImageResource(R.drawable.slika1);
}
if (childPosition == 2 && groupPosition==3) {
img.setImageResource(R.drawable.slika2);
}
if (childPosition == 3 && groupPosition==3) {
img.setImageResource(R.drawable.slika1);
}
// set the ClickListener to handle the click event on child item
//convertView.setOnClickListener(new View.OnClickListener() {
textView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Toast.makeText(activity, child.get(childPosition),
Toast.LENGTH_SHORT).show();
}
});
return convertView;
}
// method getGroupView is called automatically for each parent item
// Implement this method as per your requirement
@Override
public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent)
{
if (convertView == null) {
convertView = inflater.inflate(R.layout.parent_view, null);
}
((CheckedTextView) convertView).setText(parentItems.get(groupPosition));
((CheckedTextView) convertView).setChecked(isExpanded);
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;
}
}