2011-06-03 130 views
0
public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) { 
    View v = null; 
    if(convertView != null) 
     v = convertView; 
    else 
     v = inflater.inflate(R.layout.child_row, parent, false); 
    Color c = (Color)getChild(groupPosition, childPosition); 
    TextView color = (TextView)v.findViewById(R.id.childname); 
    if(color != null) 
     color.setText(c.getColor()); 
    TextView rgb = (TextView)v.findViewById(R.id.rgb); 
    if(rgb != null) 
     rgb.setText(c.getRgb()); 
    // CheckBox cb = (CheckBox)v.findViewById(R.id.check1); 
    // cb.setChecked(c.getState()); 
    Button btnPlay=(Button)v.findViewById(R.id.Play); 
    Button btnDelete=(Button)v.findViewById(R.id.Delete); 
    Button btnEmail=(Button)v.findViewById(R.id.Email); 
    btnPlay.setOnClickListener(new View.OnClickListener() { 

     @Override 
     public void onClick(View arg0) { 
      Log.i("TEST ", "play btn cliked"); 

      Intent i=new Intent(this,FindFilesByType.class);//THIS IS NOT ALLOWED IN MY CODE 

     } 
    }); 

    return v; 
} 

在上面的代碼中,我寫了我的adapterclass。 作爲孩子,我的可解釋列表中有3個按鈕。 我想呼籲按鈕的單擊事件另一個Java類, 所以,當我使用:android擴展列表需要幫助

Intent i=new Intent(this,FindFilesByType.class); 

我得到這個:

The constructor Intent(new View.OnClickListener(){}, Class<FindFilesByType>) is undefined 
Remove arguments to match 'Intent' 

什麼是錯的我在做什麼?

回答

0
public View getChildView(int groupPosition, int childPosition, 
     boolean isLastChild, View convertView, ViewGroup parent) { 
    View v = null; 
    if (convertView != null) 
     v = convertView; 
    else 
     v = inflater.inflate(R.layout.child_row, parent, false); 
    Rington c = (Rington) getChild(groupPosition, childPosition); 
    TextView color = (TextView) v.findViewById(R.id.childname); 
    if (color != null) 
     color.setText(c.getColor()); 
    TextView rgb = (TextView) v.findViewById(R.id.rgb); 
    if (rgb != null) 
     rgb.setText(c.getRgb()); 

    Button btnPlay = (Button) v.findViewById(R.id.Play); 
    Button btnDelete = (Button) v.findViewById(R.id.Delete); 
    Button btnEmail = (Button) v.findViewById(R.id.Email); 
    btnPlay.setOnClickListener(new View.OnClickListener() { 

     @Override 
     public void onClick(View arg0) { 
      //code 

     } 
    }); 
    btnDelete.setOnClickListener(new View.OnClickListener() { 

     @Override 
     public void onClick(View arg0) { 
      //code 

     } 
    }); 
    btnEmail.setOnClickListener(new View.OnClickListener() { 

     @Override 
     public void onClick(View arg0) { 
      //code 
     } 
    }); 

    return v; 
} 

這上面methos到適配器類,這是完全正常工作的代碼我:)

1

this對象在你的意圖構造函數是匿名OnClickListener類實例,而不是當前的上下文。你需要做的就是用當前上下文在構造函數中,如:

Intent i = new Itent(getContext(), FindFilesByType.class); 
+0

不,這不是爲我工作的感謝:) – Android 2011-06-03 04:36:48

+0

最後我得到了我所用btnPlay.setOnClickListener解決方案(新景觀.OnClickListener(){ \t \t \t \t \t \t @覆蓋 \t \t \t公共無效的onClick(查看爲arg0){ \t \t \t \t Log.i(「TEST」,「play btn cliked」); \t \t \t \t \t \t \t意圖I =新意圖(上下文,PlayingActivity.class); \t \t \t context.startActivity(i); \t \t \t \t \t \t \t} \t \t});這是工作 – Android 2011-06-03 04:36:56

1

在開始時,的onCreate之前創建intance變量()方法類似

public static InstanceTest instance; 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    instance=this; 
} 

之稱的孩子變量點擊事件

public void onClick(View arg0) { 
    Intent i=new Intent(instance,FindFilesByType.class);//This time it will be allowed 
} 

希望這可以幫助。