2013-05-31 69 views
2

我想實現我的應用程序一個功能,用戶可以添加按鈕,屏幕上,然後用長按刪除的ID。Android的 - 獲取動態生成按鈕

我能夠做到,用戶可以添加按鈕,用戶可以刪除最近添加的按鈕的第一部分。但是,如果您嘗試刪除任何其他按鈕,則會出現NullPointerException。

這裏是我的方法,它創建了一個新的按鈕:

public Button makeButton(Context context, String label, final GestureDetector gdt, int buttonId) { 



    btn = new Button(context); 

    v = new TextView(context); 
    v.setText(""); 
    v.setHeight(10); 


    ScrollView s = (ScrollView) homeView.findViewById(R.id.mainScroll); 
    s.setVerticalScrollBarEnabled(false); 

    btn.setText(label); 
    btn.setWidth(LinearLayout.LayoutParams.MATCH_PARENT); 
    btn.setHeight(70); 
    btn.setId(buttonId); 
    btn.setBackgroundColor(Color.GREEN); 
    btn.setOnTouchListener(new OnTouchListener() { 

     @Override 
     public boolean onTouch(View v, MotionEvent event) { 
      // TODO Auto-generated method stub 
      id = btn.getId(); 

      gdt.onTouchEvent(event); 



      switch(event.getAction()) { 

      case(MotionEvent.ACTION_DOWN): 

       ColorDrawable color = (ColorDrawable) btn.getBackground(); 
       int backgroundColor = color.getColor(); 

       if(backgroundColor == Color.GREEN) { 
        btn.setBackgroundColor(Color.YELLOW); 
        showToast("Running late??"); 
       } 

      } 

      return true; 

     } 

    }); 




    Ll.addView(btn); 
    Ll.addView(v); 


    return btn; 


} 

這裏是它是用來創造新的按鈕按鈕onClickListener():

addButton.setOnClickListener(new OnClickListener() { 

     @Override 
     public void onClick(View v) { 
      // TODO Auto-generated method stub 

      factory.inflate(R.layout.eventadd, null); 
      final View addEvent = factory.inflate(R.layout.eventadd, null); 
      final EditText eventTime = (EditText) addEvent.findViewById(R.id.editText1); 
      AlertDialog.Builder createEvent = new AlertDialog.Builder(context); 

      createEvent.setTitle("Add new event"); 
      createEvent.setView(addEvent); 
      (createEvent).setPositiveButton("Add Event", new DialogInterface.OnClickListener(){ 

       @Override 
       public void onClick(DialogInterface arg0, int arg1) { 
        // TODO Auto-generated method stub 
        String getTxt = eventTime.getText().toString(); 
        makeButton(context, getTxt, gestureDetector, id); 
        id++; 
       } 



      }); 
      createEvent.show(); 

     } 

    }); 

最後,這是我的GestureListener:

private class GestureListener extends SimpleOnGestureListener { 



    @Override 
    public void onLongPress(MotionEvent event) { 


     showToast("Button " + btn.getId() + " deleted"); 
     btn.setVisibility(View.GONE); 
     ((LinearLayout)btn.getParent()).removeViewAt(id); 
     ((LinearLayout)v.getParent()).removeView(v); 
    } 

} 

我很確定這個錯誤是關於如何處理id的bu ttons以及它們如何在GestureListener中被刪除。當然,我比所有幫助:)

+0

你在哪裏設置你的GestureListener到你的按鈕? GestureListener如何始終引用正確的v和id字段? – speedynomads

+0

在makeButton方法的onTouch()中(gdt.onTouchEvent(event));這是我認爲可能成爲問題的事情之一。 –

回答

4

的問題是在你的GestureListener可能wrong.Thanks更多。

你做((LinearLayout)btn.getParent()).removeViewAt(id);這消除了在特定的索引視圖。我相信你想

View view = ((LinearLayout)btn.getParent()).findViewById(id)返回你看,你應該再傳遞以

((LinearLayout)btn.getParent()).removeView(view);

編輯

它看起來像你保持一個私有類變量爲您Button。這意味着那裏的按鈕將永遠是您製作的最後一個按鈕。在你的onTouch()方法中,你引用了btn變量,這是你所做的最後一個按鈕。如果您將onTouch()方法中的第一行更改爲id = v.getId(),它應該爲您提供正確的按鈕。

+0

不...再次是同樣的事情。 –

+0

@DanielScott你將GestureListener附加到了什麼? – DavidAndroidDev

+0

@DanielScott另外一些我可能會推薦的而不是使用id的方法是使用視圖的setTag(Object obj)方法。這會讓你將對象綁定到視圖而不是Id。然後你可以傳遞一個常量字符串作爲標記,然後使用'findViewByTag(Object obj)'來查找基於字符串的視圖。 – DavidAndroidDev

0

使用按鈕ID的堆疊。 當過創建按鈕,保存其ID在該堆棧 ,當你按下按鈕刪除從堆棧獲得最後的禮物ID和刪除按鈕 - >然後從堆棧中刪除其ID爲好。

+0

是的我認爲沒有分配給每個按鈕並保存在那裏的ID有問題。但是,我怎樣才能解決這個問題,因爲按鈕可以隨機刪除? –