2016-04-09 24 views
0

我爲我的listview創建了一個自定義arrayadapter。它有一個玩家的名字和得分,我也有一個單元格上的添加按鈕,當點擊該按鈕時,我想要一個彈出屏幕出現,這裏是用戶可以添加玩家的得分的地方。這個彈出窗口有6個按鈕「+1」,「+2」「+10」..等和完成按鈕。當完成按鈕被點擊時,分數得到更新。ListView上的彈出窗口Android

我正在處理我的customArraryAdapter類中的添加按鈕單擊事件,因此我也必須在這裏創建彈出窗口。我已經搜索並試圖做​​到這一點,但沒有成功。

我試過到目前爲止: 我有一個View = convertViewviewHolder = holder的問題是我不那麼確定什麼作爲參數傳遞給創建一個彈出窗口。下面的代碼是myCustomArrayAdapter類。我也讀過彈出窗口不能處理觸摸事件,但有人說它可以。由於我的彈出窗口有很多按鈕,所以這可能是一個很好的解決方案。

這是@Override 公共查看getView(最終詮釋的立場,觀點convertView,父母的ViewGroup)方法CustomArrayAdapter類

//Handle add button click 
holder.add.setOnClickListener(new View.OnClickListener() { 

    @Override 
    public void onClick(View arg0) { 

     addScores(convertView); 

     //list gets updated 
     notifyDataSetChanged(); 


    } 

}); 

我addScores方法看起來像這樣 enter image description here

private void addScores(View v){ 
     PopupWindow pw; 
     LayoutInflater inflater = (LayoutInflater)v.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
     View layout = inflater.inflate(R.layout.weight_popup, (ViewGroup)v.findViewById(R.id.linlay_weight_popup)); 
     pw = new PopupWindow(layout, ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT, true); 
     pw.setBackgroundDrawable(new BitmapDrawable()); 
     pw.setOutsideTouchable(true); 
     pw.showAsDropDown(btnSelectWeight); 

    } 
+0

您可以從適配器傳遞上下文。從那裏你可以使用LayoutInflator.from(context)方法創建LayoutInflator對象。 – Krish

+0

你能提供一個帶有代碼的解決方案嗎?我現在嘗試傳遞上下文,但即時通訊仍在getSystemService上獲取紅色文本,並且它是否真的無法處理彈出窗口上的觸摸/單擊事件? –

+0

檢查此鏈接https://androidresearch.wordpress.com/2012/05/06/how-to-create-popups-in-android/ – Krish

回答

0

您可能傳遞的視圖將以彈出方式顯示做。

考慮一下:

View layout = inflater.inflate(R.layout.weight_popup, (ViewGroup)v.findViewById(R.id.linlay_weight_popup)); 

你weight_popup佈局應包含6個按鈕,這將對那裏的onClick更新分數。 類似於:

Button btn1 = (Button)layout.findViewById(R.id.btn1); 
btn1.setOnClickListener(new View.OnClickListener() { 
       public void onClick(View v) { 
        //update your score here. 
       } 
      }); 

//other buttons.. 

pw = new PopupWindow(layout, ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT, true);