如何显示在一个ListView行单击一个按钮的AlertDialog? [英] How to show an AlertDialog from a Button Clicked in a ListView row?

查看:134
本文介绍了如何显示在一个ListView行单击一个按钮的AlertDialog?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我填充用底座适配器一个ListView以这样的方式,所有的除了最后一个项目将复选框和最后一个项目将是一个按钮一个TextView

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.

下面是XML文件。

最后一个项目:

<?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>

复选框:

<?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;
}
}

有是一种活动调用,填充这个基础适配器将String数组列表。我试图显示一个对话框,用户单击Add按钮时。但我不能表现出来。我想:

There is an Activity calls which Populates this base adapter will an Array list of 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:


  • 添加机器人:的onClick =方法在XML文件中,写在相应的主要活动文件的方法,但是Eclipse找不到的功能。我认为这是寻找在基适配器类的功能。但问题是,我不能写code,显示在底座适配器类AlertBox因为的 getSupportFragmentManager 的不能从那里访问。

使用findViewById添加到onClickListener巴顿,但是Eclipse给我的的NullPointerException 的位置。我想这是因为按钮被直接放置在ListView和不活动。

Adding onClickListener to Button using findViewById, but Eclipse gives me NullPointerException here. I think this is because the button is placed in the ListView and not the Activity directly.

有人可以帮助我在这里?

Can someone help me here?

谢谢!

推荐答案

这,下面的例子来实现在活动的接口,并把它传递给您的适配器,当您创建它。

Check this, follow the examples to implement an interface in the activity and pass it to your adapter when you create it.

所有你需要的是放置在活动界面方法打开的对话​​框code和调用它的适配器时,单击该按钮。

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)

getView 方法在你的 TYPE_NEW_ACCOUNT 补充一点:

Add this under your TYPE_NEW_ACCOUNT in the getView method:

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();
    }
});

这篇关于如何显示在一个ListView行单击一个按钮的AlertDialog?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆