简体   繁体   English

如何从在ListView行中单击的按钮显示AlertDialog?

[英]How to show an AlertDialog from a Button Clicked in a ListView row?

I am populating a ListView with a Base Adapter in such a way that all except the last item will be checkboxes and the last item will be a TextView with a button. 我用基本适配器填充ListView,使得除最后一项外的所有项均为复选框,最后一项为带按钮的TextView。

Here are the XML files. 这是XML文件。

Final Item: 最终产品:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<TextView 
    android:id="@+id/tv_newitem"
    android:layout_height="wrap_content"
    android:layout_width="0dp"
    android:layout_weight="3"
    android:text="@string/new_account_text"
    />
<Button 
    android:id="@+id/b_newitem"
    android:layout_height="wrap_content"
    android:layout_width="0dp"
    android:layout_weight="2"
    android:text="@string/add_button_text"
    android:onClick="showNewAccountDialog"
    />
</LinearLayout>

Checkboxes: 复选框:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 >
<CheckBox 
    android:focusable="true" 
    android:id="@+id/account_item_cb" 
    android:layout_height="wrap_content" 
    android:layout_width="match_parent" 
 ></CheckBox>       
</LinearLayout>

Here is the Class file for the base adapter: 这是基本适配器的类文件

import java.util.ArrayList;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;


public class AccountListAdapter extends BaseAdapter{

private static final int TYPE_ACCOUNT = 0;
private static final int TYPE_NEW_ACCOUNT = 1;
private static final int TYPE_MAX_COUNT = 2;


private LayoutInflater mInflator;
private ArrayList<String> mStrings;
private ArrayList<String> mSelectedStrings;


public AccountListAdapter(Context context, ArrayList<String> array)
{
    mInflator = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    mStrings = array;

    mSelectedStrings = new ArrayList<String>();
}

public void addNewAccount(final String accountName)
{
    mStrings.add(mStrings.size()-2, accountName);
    notifyDataSetChanged();
}


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

@Override
public String getItem(int position)
{
    return mStrings.get(position);
}

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

@Override
public int getViewTypeCount()
{
    return TYPE_MAX_COUNT;
}

@Override
public int getItemViewType(int position)
{

    return position == mStrings.size()-1 ? TYPE_NEW_ACCOUNT : TYPE_ACCOUNT;
}

@Override
public View getView(int position, View convertView, ViewGroup parent)
{
    int type = getItemViewType(position);
    System.out.println(position + ": " + type);
    switch (type) {
    case TYPE_ACCOUNT:
        convertView = mInflator.inflate(R.layout.account_item, null);
        CheckBox tv = (CheckBox) convertView.findViewById(R.id.account_item_cb);
        tv.setText(getItem(position));
        tv.setOnCheckedChangeListener(new OnCheckedChangeListener() {

            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                // TODO Auto-generated method stub
                if (isChecked)
                {
                    mSelectedStrings.add(buttonView.getText().toString());
                }else {
                    mSelectedStrings.remove(buttonView.getText().toString());
                }   
            }
        });
        break;
    case TYPE_NEW_ACCOUNT:
        convertView = mInflator.inflate(R.layout.list_new_item_add_button, null);
        break;
    default:
        break;
    }

    return convertView;

}


public ArrayList<String> getSelectedStrings()
{
    return mSelectedStrings;
}
}

There is an Activity calls which Populates this base adapter will an Array list of String. 有一个Activity调用,它将填充此基本适配器将为String的数组列表。 I am trying to show a dialog box to the user when the Add button is clicked. 单击添加按钮时,我试图向用户显示一个对话框。 But I am not able to show it. 但是我无法显示它。 I tried: 我试过了:

  • Adding android:onClick=method in the XML file and writing corresponding method in the main activity file, but Eclipse cannot find the function. 在XML文件中添加android:onClick = method并在主活动文件中编写相应的方法,但是Eclipse无法找到该函数。 I think it is looking for the function in the base adapter class. 我认为它正在寻找基本适配器类中的功能。 But the problem is I can't write code to show a AlertBox in the Base Adapter class because getSupportFragmentManager cannot be accessed from there. 但是问题是我无法编写代码在基本适配器类中显示AlertBox,因为无法从那里访问getSupportFragmentManager

  • Adding onClickListener to Button using findViewById, but Eclipse gives me NullPointerException here. 使用findViewById将onClickListener添加到Button,但是Eclipse在这里给了我NullPointerException I think this is because the button is placed in the ListView and not the Activity directly. 我认为这是因为按钮放置在ListView中,而不是直接放置在Activity中。

Can someone help me here? 有人可以帮我吗?

Thanks! 谢谢!

You need to implement 您需要实施

OnItemClickListener OnItemClickListener

@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3)

Check this , follow the examples to implement an interface in the activity and pass it to your adapter when you create it. 选中此项 ,按照示例在活动中实现接口,并在创建接口时将其传递给适配器。

All you need is to place the open dialog code in the interface method in the activity and call it in the adapter when you click the button. 您所需要做的就是将打开的对话框代码放在活动的接口方法中,并在单击按钮时在适配器中调用它。

Place this somewhere in your activity: (this could also be done by making the activity implement the interface) 将此放置在您的活动中的某个位置:(这也可以通过使活动实现接口来完成)

public interface DialogCreatorInterface{
    public void showDialog();
}

DialogCreatorInterface dialogCreatorInterface  = new DialogCreatorInterface() {

    @Override
    public void showDialog() {
        //Create and show the dialog code

    }
};

Change the adapter constructor to include the interface: 更改适配器构造函数以包括接口:

AccountListAdapter(Context context, ArrayList<String> array, DialogCreatorInterface dialogCreatorInterface)

Add this under your TYPE_NEW_ACCOUNT in the getView method: 将其添加到您的TYPE_NEW_ACCOUNT下的getView方法中:

Button button = (Button) convertView.findViewById(R.id.b_newitem);
button.setOnClickListener(new OnClickListener() {

    @Override
    public void onClick(View v) {
       //Call open AlerDialog in activity via the interface
       dialogCreatorInterface.showDialog();
    }
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM