gpt4 book ai didi

java - 单击可扩展 ListView 中的子项时如何在另一个 Activity 中打开 ListView

转载 作者:行者123 更新时间:2023-11-30 01:59:06 26 4
gpt4 key购买 nike

我正在为我的学校开发一个应用程序。它应该包含该机构提供的每门类(class)的讲义。现在该机构中有三所不同的学校,每所学校至少有四个系,所以我的主要 Activity 有一个可扩展的 ListView ,当单击任何学校时它会显示不同的系。

我希望能够在单击特定子项(部门)时在 ListView 中打开一个新 Activity ,其中填充了该部门提供的类(class)。此外,当打开新的类(class)列表 Activity 时,用户应该能够在另一个 Activity 中查看该类(class)的讲义,而无需为每个部门和类(class)创建 Activity 和布局。

这是我的主要 Activity

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;
}

}

最佳答案

    // 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;
}
});

关于java - 单击可扩展 ListView 中的子项时如何在另一个 Activity 中打开 ListView ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31761976/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com