列表视图项按钮点击

时间:2016-07-25 18:40:56

标签: android listview android-fragments android-alertdialog

我有一个应用程序基于数据库查询加载listview。在每个listview项中都有一个文本视图和几个按钮。所以我创建了一个自定义适配器。我的问题是,当我点击列表视图中的按钮但我不知道如何从listview适配器调用片段管理器时,我试图显示一个对话框。

DanceAdapter.java

@Override
    public View getView(int position, View convertView, ViewGroup parent) {
        // Get the data item for this position
        final OneDancer oneDancer = getItem(position);
        // Check if an existing view is being reused, otherwise inflate the view
        if (convertView == null) {
                //Sign-In Activity
            if(type==1) {


            }else if (type==2)
            {

                Log.v("DancerAdapter","item_dj_dancer Loaded");

            }
            //VIP/Dances Adapter
            else if(type==3) {

                convertView = LayoutInflater.from(getContext()).inflate(R.layout.item_services, parent, false);

                final Button button4 = (Button)convertView.findViewById(R.id.button4);

                final TextView counterTextView = (TextView)convertView.findViewById(R.id.textView10);

                final Button button6 = (Button)convertView.findViewById(R.id.button6);



                button4.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {

                        DialogFragment newFragment = new SignInActivity.ConfirmDancerDialog();
                        newFragment.show(new getFragmentManager(), "Dancer Confirmed");

                        button4.setVisibility(View.INVISIBLE);
                        button6.setVisibility(View.VISIBLE);
                        counterTextView.setVisibility(View.VISIBLE);

                    }
                });




            }

        }

以下是填充listview的活动中方法的代码。

服务。的java

// Parsing of dancers

public void parseDancerList(String response) {

Log.v("SignInActivity","parseDancerList");
XMLParser parser = new XMLParser();

Document doc = parser.getDomElement(response); // getting DOM element

NodeList nl = doc.getElementsByTagName("DANCERS");

DancerAdapter adapter = new DancerAdapter(this,oneDancerArrayList,3);

Log.v("response ", "Dancer Count " + nl.getLength());
// looping through all item nodes <item>
for (int i = 0; i < nl.getLength(); i++) {
    // creating new HashMap

    Element e = (Element) nl.item(i);
    Log.v("response ", "Dancers  " + parser.getValue(e, "NAME"));

    OneDancer newDancer = new OneDancer(parser.getValue(e,"POSITION"),
            parser.getValue(e,"NAME"),
            parser.getValue(e,"AVAILABLE"),
            null,
            null,
            null,
            null,
            null,
            null);

    adapter.addAll(newDancer);
    listView.setAdapter(adapter);
    adapter.notifyDataSetChanged();




}

}

1 个答案:

答案 0 :(得分:0)

好吧,我想这只是因为你已经厌倦了,你没有意识到如何做到这一点,因为我真的很简单,至少如果我没有错过任何东西, xd,我自己并不累。这是代码:

活动:

DancerAdapter adapter = new DancerAdapter(this,oneDancerArrayList,3, getSupportFragmentManager());

在您的DancerAdapter中:

private static FragmentManager fragmentManager;

public DancerAdapter(...,FragmentManager fragmentManager) {
    ...
    this.fragmentManager = fragmentManager;//now you have a reference to it
}