2017-02-28 73 views
0

我在我的GridView中有一個問題,我在我的警報箱中插入了我的GridView,但是我的setOnitemClicklistener的函數不在我的警報箱中工作。如何在gridview的alertbox中實現setOnitemClicklistener。任何建議對這個問題Android Gridview setOnitemClicklisterner在警報對話框中沒有功能

LayoutInflater li = LayoutInflater.from(getActivity()); 
      View promptsView = li.inflate(R.layout.prompt_quantity, null); 
      final AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(getActivity()); 
      // set prompts.xml to alertdialog builder 
      alertDialogBuilder.setView(promptsView); 
      final GridView modifiergridview = (GridView) promptsView.findViewById(R.id.modifiergridview); 
      modifierList = db.getModifierName(pName); 
      modifiergridview.setAdapter(new Modifierlist()); 

      modifiergridview.setOnItemClickListener(new AdapterView.OnItemClickListener() { 
      @Override 
       public void onItemClick(AdapterView<?> parent, View view, int position, long id) { 
         Toast.makeText(getActivity(), "test", Toast.LENGTH_SHORT).show(); 
       } 
       }); 


       alertDialog.show(); 
       alertDialog.getWindow().setLayout(2000, 2000); 



     } 
}); 

Modifierlist.java

public class Modifierlist extends BaseAdapter { 

    LayoutInflater mInflater; 

    private ArrayList<Integer> positions = new ArrayList<Integer>(); 

    public ArrayList<Integer> getPositions() { 
     return positions; 
    } 

    public Modifierlist() { 
     mInflater = LayoutInflater.from(getActivity()); 
    } 

    @Override 
    public int getCount() { 
     return modifierList.size(); 
    } 

    @Override 
    public Object getItem(int position) { 
     return null; 
    } 

    @Override 
    public long getItemId(int position) { 
     return position; 
    } 

    @Override 
    public View getView(final int position, View convertView, ViewGroup parent) { 

     if (convertView == null) { 
      convertView = mInflater.inflate(R.layout.modifierlistview, null); 
     } 
     modifiername = (Button) convertView.findViewById(R.id.modifierbutton); 

     Modifier myObj = modifierList.get(position); 
     modifiername.setText("" + myObj.getModifierName()); 


     return convertView; 
    } 
} 
+0

,請複製粘貼Modifierlist()類代碼? –

回答

0

設置onclicklistener上modifiername適配器類

modifiername.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      Toast.makeText(getActivity(), "test", Toast.LENGTH_SHORT).show(); 
     } 
    }); 
+0

謝謝你,它的工作 –

+0

,但基本上我使用setOnitemClicklistener爲我的gridview它工作得很好 –

+0

爲什麼有alertbox中的gridview問題? –

0

使用對話,而不是 AlertDialog.Builder。

+0

感謝,我使用AlertDialog.Builder,因爲我想提示我的xml設計佈局 –

+0

你也可以使用你在對話框中的xml設計只需添加** dialog.setView(R.layout.dialog_layout); ** –