0

I'm looking for a way to implement a dialog which asks for confirmation when clicking on the delete button of my ListView row. I tried to do it inside my custom ArrayAdapter, but as it is no Activity I don't know how to do it.

When I put the whole onClick-Listener inside the MainActivity, I have no clou how to find out which position the button was clicked so that I can remove it afterwards.

public class ServiceAdapter extends ArrayAdapter<Service> {

    private final Singleton singleton = Singleton.getInstance();
    private ArrayList<Service> services;

    public ServiceAdapter(Context context, ArrayList<Service> services) {
        super(context, 0, services);

        this.services = services;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        // Get the data item for this position
        Service service = getItem(position);
        // Check if an existing view is being reused, otherwise inflate the view
        if (convertView == null) {
            convertView = LayoutInflater.from(getContext()).inflate(
                    R.layout.listview_row, parent, false);
        }
        // Lookup view for data population
        TextView quantity = (TextView) convertView
                .findViewById(R.id.QUANTITY_CELL);
        TextView description = (TextView) convertView
                .findViewById(R.id.DESCRIPTION_CELL);
        Button delete = (Button) convertView.findViewById(R.id.BUTTON_DELETE);

        // Populate the data into the template view using the data object
        quantity.setText(String.valueOf(service.getQuantity()));
        description.setText(service.getDescription());

        // Set up the listener for the delete button.
        final View view = convertView;
        view.setTag(Integer.valueOf(position));
        delete.setOnClickListener(new Button.OnClickListener() {
            @Override
            public void onClick(View v) {
                Integer index = (Integer) view.getTag();
                services.remove(index.intValue());
                notifyDataSetChanged();
            }
        });

        // Return the completed view to render on screen
        return convertView;
    }

}

public class MainActivity extends Activity {

    private ListView serviceList;
    private ArrayList<Service> services;
    private ServiceAdapter adapter;
    private final Singleton singleton = Singleton.getInstance();

    @Override
    public void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        serviceList = (ListView) findViewById(R.id.service_list);
        adapter = new ServiceAdapter(this, services);
        serviceList.setAdapter(adapter);

        serviceList.setOnItemClickListener(new OnItemClickListener() {

            public void onItemClick(AdapterView<?> parent, View view,
                    final int position, long id) {

                Service temp = services.get(position);
                singleton.setQuantity(temp.getQuantity());
                singleton.setDescription(temp.getDescription());
                setPosition(position);

                openDetailedEntry();

            }
        });
    }

    public void openDetailedEntry() {
        Intent i = new Intent(this, DetailedEntryActivity.class);

        // Check if the meant Activity is actually resolvable
        if (i.resolveActivity(getPackageManager()) != null)
            startActivity(i);
    }
}

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:paddingLeft="16dp"
    android:paddingRight="16dp" >

    <TableLayout 
            android:orientation="horizontal"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="0.1"
            android:paddingTop="8dp"
            >   

            <ListView 
                android:id="@+id/service_list"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:dividerHeight="2dp" />

        </TableLayout>

</LinearLayout>

<LinearLayout
     xmlns:android="http://schemas.android.com/apk/res/android"
     android:id="@+id/listview_row"
     android:layout_width="fill_parent"
     android:layout_height="wrap_content"
     android:paddingTop="4dip"
     android:paddingBottom="4dip"
     android:paddingLeft="4dip"
     android:paddingRight="4dip"
     android:orientation="horizontal">


     <TextView android:id="@+id/QUANTITY_CELL"
         android:layout_width="0dp"
         android:layout_height="wrap_content"
         android:layout_weight="2"
         android:textSize="20sp"
         />

     <TextView android:id="@+id/DESCRIPTION_CELL"
         android:layout_width="0dip"
         android:layout_height="wrap_content" 
         android:textSize="20sp"
         android:layout_weight="6" />

     <Button
         android:id="@+id/BUTTON_DELETE"
         android:layout_width="0dp"
         android:layout_height="35dp"
         android:layout_weight="1"
         android:textSize="12sp"
         android:focusable="false"
         android:text="@string/delete" />

</LinearLayout>

Let me know if you need something.

2
  • Dialog or AlertDialog? Commented Jul 8, 2014 at 10:43
  • AlertDialog, sorry. Commented Jul 8, 2014 at 10:46

1 Answer 1

2

Construct AlertDialog in ServiceAdapter Like this,

private AlertDialog mDialog; 
private int mListRowPosition;
public ServiceAdapter(Context context, ArrayList<Service> services) {
    super(context, 0, services);
    this.services = services;

    //Create AlertDialog here
    AlertDialog.Builder builder = new AlertDialog.Builder(context); 
    builder.setMessage("Your Message")
           .setPositiveButton(R.string.fire, new DialogInterface.OnClickListener() {
               public void onClick(DialogInterface dialog, int id) {
                   // Use mListRowPosition for clicked list row...
               }
           })
           .setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
               public void onClick(DialogInterface dialog, int id) {
                   // User cancelled the dialog
               }
           });
    // Create the AlertDialog object 

    mDialog = builder.create();
}

Create method in ServiceAdapter Like,

private void showDialog(int position)
{
 mListRowPosition = position;
 if(mDialog != null)
 mDialog.show();
}

Now in onClick() Just call

showDialog(position); // But make position of getView() as final...
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.