简体   繁体   中英

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.

Here are the XML files.

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

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

Thanks!

You need to implement

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:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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