0

リスト項目がクリックされたときにカスタム ダイアログを開きたい。コードに実装するにはどうすればよいですかonClickListener:

public class Fragment4 extends Fragment{
    private View parentLayout;
    private ListView listView;
    private ArrayList<String> myList;
    private AboutUsAdapter adapter; //custom adapter

    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
        parentLayout = inflater.inflate(R.layout.fragment_4, null);
        listView = (ListView)parentLayout.findViewById(R.id.listView4);
        myList = new ArrayList<String>();

        myList.add("Item1");
        myList.add("Item2");
        myList.add("Item2");
        adapter = new AboutUsAdapter(getActivity().getBaseContext(), myList);
        listView.setAdapter(adapter);
        return parentLayout;
            }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
          listView.setOnItemClickListener(new OnItemClickListener() {
              public void onItemClick(AdapterView<?> parent, View view,
                  int position, long id) {            
    }

}); } }
4

2 に答える 2

4

こんな使い方ができる...

listView.setOnItemClickListener(new OnItemClickListener() {
          public void onItemClick(AdapterView<?> parent, View view,
              int position, long id) {  

    AlertDialog.Builder alert = new AlertDialog.Builder(getActivity());
    alert.setTitle("Your title here");
    alert.setMessage(myList.get(position).toString());
    alert.setPositiveButton(R.string.OK,
            new DialogInterface.OnClickListener() {

                @Override
                public void onClick(DialogInterface dialog, int which) {
                    // TODO Auto-generated method stub

                    dialog.dismiss();

                }
            });

    alert.show();          
}});

同じ方法で、カスタムダイアログを使用できます

回線を変更する

parentLayout = inflater.inflate(R.layout.fragment_4, null);

parentLayout = inflater.inflate(R.layout.fragment_4, container,false);
于 2013-08-15T09:39:12.920 に答える
0

次のように、リスト項目に clickListener を設定できます。

    listView.setOnItemClickListener(new OnItemClickListener()
    {
        @Override 
        public void onItemClick(AdapterView<?> arg0, View arg1,int position, long arg3)
        { 
            // show your dialog here
            Toast.makeText(yourActivity.this, "" + position, Toast.LENGTH_SHORT).show();
        }
    });
于 2013-08-15T09:31:48.453 に答える