1

我有一個列表視圖與sublistView項目,我參考Using lists in Android(15.2。ExpandableListView示例)。在onChildClickListener中使用意圖找不到方法

我可以通過Toast和顯示獲得每個列表項的點擊。

convertView.setOnClickListener(new OnClickListener() { 
    @Override 
    public void onClick(View v) { 
    Toast.makeText(activity, children, 
     Toast.LENGTH_SHORT).show(); 
    } 

但是,當我想在childClick中使用意圖時,我收到一個錯誤:cannot find symbol method startActivity(Intent)

下面是我的課:

public class MyExpandableListAdapter extends BaseExpandableListAdapter { 
//Some sort of class code in here... 

@Override 
public View getChildView(int groupPosition, final int childPosition, 
boolean isLastChild, View convertView, ViewGroup parent) { 

final String children = (String) getChild(groupPosition, childPosition); 
TextView text = null; 

if (convertView == null) { 
    convertView = inflater.inflate(R.layout.listrow_details, null); 
} 
text = (TextView) convertView.findViewById(R.id.textView1); 
text.setText(children); 

convertView.setOnClickListener(new OnClickListener() { 

    @Override 
    public void onClick(View v) { 
     //11 group position exist, each has 5 child 
     switch ((groupPosition)){ 
     case 0: 
     if (childPosition == 0) { 

      Intent intent = new Intent(Intent.ACTION_VIEW); 
      intent.setData(Uri.parse("market://details?id=editors_choice")); 
      startActivity(intent); //cannot find symbol method startActivity(Intent) 


     } else if (childPosition == 1) { 
      Toast.makeText(activity, "2", 
        Toast.LENGTH_SHORT).show(); 

     } else if (childPosition == 2) { 
      Toast.makeText(activity, "3", 
        Toast.LENGTH_SHORT).show(); 


     } else if (childPosition == 3) { 
      Toast.makeText(activity, "4", 
        Toast.LENGTH_SHORT).show(); 

     } else if (childPosition == 4) { 
      Toast.makeText(activity, "5", 
        Toast.LENGTH_SHORT).show(); 

     } 
     break; 

     case 1: 
     ... 
    } 
}); 
return convertView; 
    } 
} 

感謝您的幫助。

+0

您可以在您的活動中調用此類MyExpandableListAdapter時顯示嗎? – 2015-04-04 09:26:17

+0

如果你可以檢查我上面提到的鏈接可能會更好地爲您獲得更廣泛的視圖,否則我可以添加我的代碼也沒有問題。順便說一句,我在我的主要活動中擴展了ActionBarActivity。 – Umitk 2015-04-04 09:29:31

+0

我的問題是你不能在你的列表適配器中調用startActivity()。它是您的應用程序上下文類的方法。 – 2015-04-04 09:31:05

回答

3

error: cannot find symbol method startActivity(Intent).

由於startActivity方法從BaseExpandableListAdapter不是那麼使用上下文來訪問startActivity方法。如:

v.getContext().startActivity(intent); 
+0

我可以運行應用程序,但當我點擊「未發現處理意圖{act = android.intent.action.VIEW dat = market:// details?id = editors_choice}」時,它崩潰。你有什麼主意嗎? – Umitk 2015-04-04 09:34:28

+0

@Umitk:'editors_choice'是包名? – 2015-04-04 09:35:39

+0

我改變它爲包和錯誤是:沒有活動發現處理意圖{act = android.intent.action.VIEW dat = market:// details?id = com.myapp.mapcards} – Umitk 2015-04-04 09:47:37

1

試試這個,

定義一個基準爲您MyExpandableListAdapter類語境,

private Context context; 

您MyExpandableListAdapter類創建一個構造函數和傳遞從那裏您初始化它的背景下,

public MyExpandableListAdapter(Context con) { 
    context = con; 
} 

開始你的活動,

context.startActivity(intent) 
相關問題