2012-01-26 73 views
1

我創建了一個類型爲BaseAdapter的類,該類使用按鈕填充 - 當您單擊按鈕時,我想加載新的意圖。這在兩個層面證明有困難:Android:使用GridView中的按鈕(帶有導航按鈕的GridView)創建新的意圖

  1. 您不能將該事件與適配器內部的按鈕(創建新的意圖的按鈕)相關聯。這就是爲什麼我將按鈕作爲數組發送到我的適配器(此解決方案有效,但它很凌亂)

  2. 即使我的按鈕是在同一活動內創建的 - 他們無法從該活動創建新的意圖。免除是如此之大,我甚至沒有得到一個嘗試...趕上聲明工作。

我曾嘗試思考,創建按鈕活動中並使它們通過,傳遞上下文(調用context.startIntent(...))

我的問題:有人可以告訴我怎麼創建一個ButtonAdapter,其中每個按鈕創建一個新的Intent - 即使是與原始Activity相同的類型?

UPDATE:這裏是代碼,因爲我收到來自誰認爲我的人回答正與onClickListeners掙扎:

public class ButtonAdapter extends BaseAdapter 
{ 
    private Context _context; 
    private Button[] _button; 

    public ButtonAdapter(Context c, Button[] buttons) 
    { 
     _context = c; 
     _button = buttons; 
    } 

    // Total number of things contained within the adapter 
    public int getCount() 
    { 
     return _button.length; 
    } 

    // Require for structure, not really used in my code. 
    public Object getItem(int position) 
    { 
     return _button[position]; 
    } 

    // Require for structure, not really used in my code. Can 
    // be used to get the id of an item in the adapter for 
    // manual control. 
    public long getItemId(int position) 
    { 
     return position; 
    } 

    public View getView(int position, View convertView, ViewGroup parent) 
    {   
     _button[position].setId(position); 
     return _button[position]; 
    } 
} 


---------------
的活動:

public class MainActivity extends Activity 
{ 
    private GridView _gv; 
    private TextView _heading;  
    private ButtonAdapter _adapter; 

    public void LoadActivity(String heading) 
    { 
     try 
     { 
      Itent intent = new Intent(getApplicationContext(), MainActivity.class); 
      intent.putExtra("Level", "NextPage"); 
      intent.putExtra("Heading", heading); 
      startActivity(intent); 
     } 
     catch (Exception ex) 
     { 
      Toast.makeText(getApplicationContext(), String.format("Error LoadActivity: %s", ex.getMessage()), Toast.LENGTH_SHORT).show(); 
     } 
    }  

    private void createButtonsAdapter(Button _button[]) 
    { 
     _buttonadapter = new ButtonAdapter(getApplicationContext(), _button); 
     _gv.setAdapter(_adapter); 
    }   

    private void setupButtons() 
    { 
     Button[] _buttons = new Button[2]; 
     String names[] = new String[]{"Button1","Button2"}; 

     for (int i = 0; i < 2; i++) 
     { 
      _buttons[i] = new Button(this); 
      _buttons[i].setText(names[i]); 
      _buttons[i].setTag(names[i]); 

      _buttons[i].setOnClickListener(new View.OnClickListener() 
      { 
       public void onClick(View arg0) 
       { 
        try 
        { 
         LoadActivity(((Button)arg0).getTag().toString()); 
        } 
        catch(Exception ex) 
        { 
         Toast.makeText(getApplicationContext(), String.format("Error button.onClick: %s", ex.getMessage()), Toast.LENGTH_SHORT).show(); 
        } 
       } 
      }); 
     } 
     createButtonsAdapter(_buttons);  
    } 

    @Override 
    public void onCreate(Bundle savedInstanceState) 
    { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 

     _gv = (GridView)findViewById(R.id.gridview); 
     _heading = (TextView)findViewById(R.id.tv_heading);      

     Bundle params = getIntent().getExtras();     

     if (params == null) 
     { 
      setupButtons();  
     } 
     else if (params.containsKey("Level")) 
     { 
      _heading.setText(params.getString("Heading")); 
      if (params.getString("Level").equals("NextPage")) 
      { 
       //code not here yet 
      } 
      else if (params.getString("Level").equals("Chapters")) 
      { 
       //future code 
      } 
     } 
    } 
} 

藉口大膽和帽子,但我已經受夠傻回答warrent此:
我曾嘗試在GRIDVIEW中插入ONCLICKLISTENER,但它不起作用
即使您將上下文作爲參數傳遞,也無法從該活動外的類加載活動。這就是爲什麼我使用了這種方法,它完全炸燬了android,儘管我已經嘗試了catch語句。

請嘗試給我一個解決方案,以修正我的代碼,其他代碼或實現我想要的教程。我知道如何正確地做一個按鈕適配器,這是加載Intent的行爲,迫使我以這種方式實現它。

回答

0

我建議以下,

  1. 使用共同的onClick聽者中所有在網格視圖
  2. 設置標籤的按鈕在getView FUNC所有butons。的適配器。
  3. 使用標記對象來決定從onClick偵聽器觸發的意圖。

我希望它有助於..

+0

請參閱我對以前「解決方案」的評論。這不是一個簡單的答案。 –

0

我想你可以輕鬆地操縱在擴展底座適配器類創建您的按鈕。在getView方法....如果你有按鈕b ..然後做如下

b.setOnClickListener(new OnClickListener() 
{ 
    public void onClick() 
    { 
     // Do your stuff here ... 
    } 
}); 

,如果你想開始在點擊這個按鈕的其他活動,那麼你需要調用上下文傳遞給該適配器。

+0

請在提交答案之前嘗試您的解決方案。我知道如何設置onClickListener。我想要一個GridView中的Button來啓動一個新的活動。我知道如何創建按鈕事件。我知道如何創建GridViews和適配器。我知道如何開始與意向的新活動。我想知道如何把它們放在一起。 –