gpt4 book ai didi

java - onchildclick 案例声明 android

转载 作者:行者123 更新时间:2023-12-01 04:19:42 26 4
gpt4 key购买 nike

项目运行正常,案例 3 子级单击“确定”,但如果案例 2 单击,它会显示案例 3 中的 Activity 或我之前删除它的 Activity ,然后返回时它会显示我想要的 Activity (当我单击微生物学时,它显示 child dentry 学,然后返回时单击它显示微生物学)

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ExpandableListView;
import android.widget.ExpandableListView.OnChildClickListener;
import android.widget.ExpandableListView.OnGroupClickListener;
import android.widget.ExpandableListView.OnGroupCollapseListener;
import android.widget.ExpandableListView.OnGroupExpandListener;
import android.widget.Toast;
import com.freedental.blogspot.R;

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) {

switch(groupPosition) {
case 1:
switch (childPosition) {
case 0:
Intent c1= new Intent(MainActivity.this,Webv1Activity.class);
startActivity(c1);
break;

}
case 2:
switch (childPosition) {
case 0:
Intent d1= new Intent(MainActivity.this,Webv1Activity.class);
startActivity(d1);
break;
case 1:
Intent d2= new Intent(MainActivity.this,Webv2Activity.class);
startActivity(d2);
break;
case 2:
Intent d3= new Intent(MainActivity.this,Webv3Activity.class);
startActivity(d3);
break;
case 3:
Intent d4= new Intent(MainActivity.this,Webv4Activity.class);
startActivity(d4);
break;
case 4:
Intent d5= new Intent(MainActivity.this,Webv5Activity.class);
startActivity(d5);
break;
case 5:
Intent d6= new Intent(MainActivity.this,Webv6Activity.class);
startActivity(d6);
break;
case 6:
Intent d7= new Intent(MainActivity.this,Webv7Activity.class);
startActivity(d7);
break;
case 7:
Intent d8= new Intent(MainActivity.this,Webv8Activity.class);
startActivity(d8);
break;


}
case 3:
switch (childPosition) {
case 0:
Intent a1= new Intent(MainActivity.this,Webv9Activity.class);
startActivity(a1);
break;
case 1:
Intent a2= new Intent(MainActivity.this,Webv10Activity.class);
startActivity(a2);
break;
case 2:
Intent a3= new Intent(MainActivity.this,Webv11Activity.class);
startActivity(a3);
break;
case 3:
Intent a4= new Intent(MainActivity.this,Webv12Activity.class);
startActivity(a4);
break;
case 4:
Intent a5= new Intent(MainActivity.this,Webv13Activity.class);
startActivity(a5);
break;
case 5:
Intent a6= new Intent(MainActivity.this,Webv14Activity.class);
startActivity(a6);
break;
case 6:
Intent a7= new Intent(MainActivity.this,Webv15Activity.class);
startActivity(a7);
break;
case 7:
Intent a8= new Intent(MainActivity.this,Webv16Activity.class);
startActivity(a8);
break;


}
}
return false;
}
});
}

/*
* Preparing the list data
*/
private void prepareListData() {
listDataHeader = new ArrayList<String>();
listDataChild = new HashMap<String, List<String>>();

// Adding child data
listDataHeader.add("1st Grade lectures");
listDataHeader.add("2nd Grade lectures");
listDataHeader.add("3rd Grade lectures");
listDataHeader.add("4th Grade lectures");
listDataHeader.add("5th Grade lectures");

// Adding child data
List<String> first = new ArrayList<String>();
first.add("Under Construction");

List<String> second = new ArrayList<String>();
second.add("Prosthodontics");
second.add("Other under con");

List<String> third = new ArrayList<String>();
third.add("Microbiology");
third.add("Pathology");
third.add("Pharmacology");
third.add("Surgery");
third.add("Operative");
third.add("Prosthodontics");
third.add("Radiology");
third.add("Cummunity");


List<String> fourth = new ArrayList<String>();
fourth.add("periodontology");
fourth.add("pedodontics");
fourth.add("Orthodontics");
fourth.add("Prosthodontics");
fourth.add("Oral Pathology");
fourth.add("Oral Surgery");
fourth.add("General Surgery");
fourth.add("General Medicine");

List<String> fifth = new ArrayList<String>();
fifth.add("Under Construction");


listDataChild.put(listDataHeader.get(0), first); // Header, Child data
listDataChild.put(listDataHeader.get(1), second);
listDataChild.put(listDataHeader.get(2), third);
listDataChild.put(listDataHeader.get(3), fourth);
listDataChild.put(listDataHeader.get(4), fifth);
}
}

最佳答案

您在 case 1case 2 之后缺少 break 语句:

expListView.setOnChildClickListener(new OnChildClickListener() {

@Override
public boolean onChildClick(ExpandableListView parent, View v,
int groupPosition, int childPosition, long id) {

switch(groupPosition) {
case 1:
switch (childPosition) {
case 0:
Intent c1= new Intent(MainActivity.this,Webv1Activity.class);
startActivity(c1);
break;

}

=======>>>>> break;

case 2:
switch (childPosition) {
case 0:
Intent d1= new Intent(MainActivity.this,Webv1Activity.class);
startActivity(d1);
break;
case 1:
Intent d2= new Intent(MainActivity.this,Webv2Activity.class);
startActivity(d2);
break;
case 2:
Intent d3= new Intent(MainActivity.this,Webv3Activity.class);
startActivity(d3);
break;
case 3:
Intent d4= new Intent(MainActivity.this,Webv4Activity.class);
startActivity(d4);
break;
case 4:
Intent d5= new Intent(MainActivity.this,Webv5Activity.class);
startActivity(d5);
break;
case 5:
Intent d6= new Intent(MainActivity.this,Webv6Activity.class);
startActivity(d6);
break;
case 6:
Intent d7= new Intent(MainActivity.this,Webv7Activity.class);
startActivity(d7);
break;
case 7:
Intent d8= new Intent(MainActivity.this,Webv8Activity.class);
startActivity(d8);
break;


}

==========>>>>> break;

case 3:
switch (childPosition) {
case 0:
Intent a1= new Intent(MainActivity.this,Webv9Activity.class);
startActivity(a1);
break;
case 1:
Intent a2= new Intent(MainActivity.this,Webv10Activity.class);
startActivity(a2);
break;
case 2:
Intent a3= new Intent(MainActivity.this,Webv11Activity.class);
startActivity(a3);
break;
case 3:
Intent a4= new Intent(MainActivity.this,Webv12Activity.class);
startActivity(a4);
break;
case 4:
Intent a5= new Intent(MainActivity.this,Webv13Activity.class);
startActivity(a5);
break;
case 5:
Intent a6= new Intent(MainActivity.this,Webv14Activity.class);
startActivity(a6);
break;
case 6:
Intent a7= new Intent(MainActivity.this,Webv15Activity.class);
startActivity(a7);
break;
case 7:
Intent a8= new Intent(MainActivity.this,Webv16Activity.class);
startActivity(a8);
break;


}
}
return false;
}
});

关于java - onchildclick 案例声明 android,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19063904/

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