可扩展的onchild方法

时间:2014-10-09 20:32:59

标签: android string listview android-intent expandablelistview

我的应用有效,但我遇到了问题。当我点击第一个标题儿童其开放谷歌。但当我点击第二个标题时它再次开启谷歌!当我点击第二个标题时,它必须打开雅虎?我写了,但它每次都在工作:(我该怎么解决这个问题?

public class osym extends Activity {

ExpandableListAdapter listAdapter;
ExpandableListView expListView;
ExpandableListView expListView2;
List<String> listDataHeader;
HashMap<String, List<String>> listDataChild;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.expand);

    // get the listview
    expListView = (ExpandableListView) findViewById(R.id.lvExp);
    expListView2 = (ExpandableListView) findViewById(R.id.lvExp);
    // preparing list data
    prepareListData();

    listAdapter = new ExpandableListAdapter(this, listDataHeader, listDataChild);

    // setting list adapter
    expListView.setAdapter(listAdapter);

    expListView2.setAdapter(listAdapter);




    final ArrayList<String> links = new ArrayList<String>();         
    links.add("http://www.google.com"); // i got problem here.

    final ArrayList<String> links2 = new ArrayList<String>();         
    links2.add("http://www.yahoo.com"); // when i click second list of child.it must open yahoo but it opening google :( ? 


    expListView.setOnChildClickListener(new OnChildClickListener() {

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

             if(childPosition == 0)
            {

                 Intent int0 = new Intent(getApplicationContext(),webplayer.class);
                    int0.putExtra("url", links.get(childPosition));  // send the url here
                    startActivity(int0);
                    finish();
            }
         if(childPosition == 1)
        {

             Intent int0 = new Intent(getApplicationContext(),webplayer.class);
                int0.putExtra("url", links.get(childPosition));  // send the url here
                startActivity(int0);
                finish();
        }

         if(childPosition == 2)
        {

             Intent int0 = new Intent(getApplicationContext(),webplayer.class);
                int0.putExtra("url", links.get(childPosition));  // send the url here
                startActivity(int0);
                finish();
        }
         if(childPosition == 3)
        {

             Intent int0 = new Intent(getApplicationContext(),webplayer.class);
                int0.putExtra("url", links.get(childPosition));  // send the url here
                startActivity(int0);
                finish();
        }
     if(childPosition == 4)
        {

             Intent int0 = new Intent(getApplicationContext(),webplayer.class);
                int0.putExtra("url", links.get(childPosition));  // send the url here
                startActivity(int0);
                finish();
        }

     if(childPosition == 5)
        {

             Intent int0 = new Intent(getApplicationContext(),webplayer.class);
                int0.putExtra("url", links.get(childPosition));  // send the url here
                startActivity(int0);
                finish();
        }

    return false;

        }
    });
}


private void prepareListData() {
    listDataHeader = new ArrayList<String>();
    listDataChild = new HashMap<String, List<String>>();

    // Adding child data
    listDataHeader.add("Geometri Çıkmış Sorular");
    listDataHeader.add("Matematik Çıkmış Sorular");


    // Adding child data
    List<String> geo = new ArrayList<String>();
    geo.add("2009 Öss Geometri Çözümler");



    List<String> mat1 = new ArrayList<String>();
    mat1.add("Polinomlar");






    listDataChild.put(listDataHeader.get(0), geo); // Header, Child data
    listDataChild.put(listDataHeader.get(1), mat1);

}

1 个答案:

答案 0 :(得分:0)

正如我在这里看到的,你没有将yahoo url附加到你的第一个Array List名称链接,你正在做的是你将yahoo传递给名为links2的第二个数组列表而不使用那个arraylist。所以像这样改变

final ArrayList<String> links = new ArrayList<String>();         
    links.add("http://www.google.com"); 
    links.add("http://www.yahoo.com");


expListView.setOnChildClickListener(new OnChildClickListener() {

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

             if(childPosition == 0)
            {

            Intent int0 = new Intent(getApplicationContext(),webplayer.class);
             int0.putExtra("url", links.get(childPosition));  // refernce for google
                    startActivity(int0);
                    finish();
            }
         if(childPosition == 1)
        {

             Intent int0 = new Intent(getApplicationContext(),webplayer.class);
                int0.putExtra("url", links.get(childPosition));  // refernce for yahho
                startActivity(int0);
                finish();
        }

修改:如果您想将父级移到父级列表项,请使用此项。

  if(childPosition == 0 && groupPosition==0)
        {

             Intent int0 = new Intent(getApplicationContext(),webplayer.class);
                int0.putExtra("url", links.get(childPosition));
                startActivity(int0);
                finish();
        }