2015-10-12 34 views
-3

我是Android新手。我想在可展開的listview子項被點擊時開始一個活動。我已經尋找了幾個小時試圖找到一個解決方案,但我沒有找到任何適當的解決方案。有人請爲此問題提供代碼。如何在可展開列表視圖(手風琴)子項單擊時啓動活動

 package com.demo.spry.acordion1; 

import java.util.ArrayList; 

import android.app.ExpandableListActivity; 
import android.content.Context; 
import android.os.Bundle; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.widget.ExpandableListView; 
import android.widget.ExpandableListView.OnChildClickListener; 
import android.widget.Toast; 

public class MainActivity extends ExpandableListActivity implements 
     OnChildClickListener { 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     ExpandableListView expandbleLis = getExpandableListView(); 
     expandbleLis.setDividerHeight(2); 
     expandbleLis.setGroupIndicator(null); 
     expandbleLis.setClickable(true); 

     setGroupData(); 
     setChildGroupData(); 


adapter.setInflater((LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE), this); 
expandableList.setAdapter(adapter); 
adapter.notifyDataSetChanged(); 
expandableList.setOnChildClickListener(new ExpandableListView.OnChildClickListener() { 
    @Override 
    public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) { 


     Intent myIntent = new Intent(MainActivity.this, Main5Activity.class); 
     MainActivity.this.startActivity(myIntent); 

    return false; 


    } 
}); 
    public void setGroupData() { 
     groupItem.add("Car Details"); 
     groupItem.add("Appointment Details"); 
     /* groupItem.add("Manufacturer"); 
     groupItem.add("Extras");*/ 
    } 

    ArrayList<String> groupItem = new ArrayList<String>(); 
    ArrayList<Object> childItem = new ArrayList<Object>(); 

    public void setChildGroupData() { 
     /** 
     * Add Data For TecthNology 
     */ 
     ArrayList<String> child = new ArrayList<String>(); 
     child.add("Java"); 
     child.add("Drupal"); 
     child.add(".Net Framework"); 
     child.add("PHP"); 
     childItem.add(child); 

     /** 
     * Add Data For Mobile 
     */ 
     child = new ArrayList<String>(); 
     child.add("Android"); 
     child.add("Window Mobile"); 
     child.add("iPHone"); 
     child.add("Blackberry"); 
     childItem.add(child); 

    } 

    @Override 
    public boolean onChildClick(ExpandableListView parent, View v, 
           int groupPosition, int childPosition, long id) { 
     Toast.makeText(MainActivity.this, "Clicked On Child", 
       Toast.LENGTH_SHORT).show(); 
     return true; 
    } 
} 
+1

你的代碼在哪裏??? –

+0

你可以在谷歌找到它 –

+0

你必須發佈你的代碼才能得到正確的答案。 – Firefog

回答

0

使用此:

ExpandableListView.setOnChildClickListener(new ExpandableListView.OnChildClickListener() { 

          @Override 
          public boolean onChildClick(ExpandableListView arg0, 
            View arg1, int arg2, int arg3, long arg4) { 
           Intent myIntent = new Intent(CurrentActivity.this, NextActivity.class); 
          CurrentActivity.this.startActivity(myIntent); 
           return false; 
          } 
         }); 
1

下面是一個例子,但我不知道你懂不懂

如果您的列表從活動都

list.setOnItemClickListener(new AdapterView.onItemClickListener() { 
    @Override 
    public void onItemClick(AdapterView<?> adapter, View view, int position, long arg) { 

    Intent intent = new Intent(YourActivity.this, SecondActivity.class); 
      startActivity(intent); 
     } 
    }); 

如果您的列表來自片段使用此

list.setOnItemClickListener(new AdapterView.onItemClickListener() { 
@Override 
public void onItemClick(AdapterView<?> adapter, View view, int position, long arg) { 
Intent intent = new Intent(getActivity(), SecondActivity.class); 
     startActivity(intent); 
    } 
}); 
+0

我已經試過這個,但它不工作 –

+0

你在哪裏使用這段代碼,在一個片段?或活動? – Firefog

+0

@SumitraDhaker我已經更新我的答案,請檢查 – Firefog

相關問題