ListViewWithDialog

In Android, ListView let you arranges components in a vertical scrollable list. The display of elements in a list is a very common pattern in mobile applications. The user sees a list of items and can scroll through them
In this tutorialsee.

1. MainActivity.java

package com.tutorialsee.listviewwithdialog;

import java.util.ArrayList;
import java.util.Arrays;
import com.tutorialsee.listviewwithdialog.R;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;

public class MainActivity extends Activity implements OnItemLongClickListener,MyDialogFragment.Communicator {

ListView listview;
ArrayList<String> list_items;
int item_position_clicked;
ArrayAdapter<String> adapter;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_main);

listview = (ListView) findViewById(R.id.mylist);
list_items = new ArrayList<String>(Arrays.asList(getResources().getStringArray(R.array.list_items)));
prepareList();
}

private void prepareList() {

adapter = new ArrayAdapter<String>(this,android.R.layout.simple_expandable_list_item_1, list_items);
listview.setAdapter(adapter);
adapter.notifyDataSetChanged();
listview.setOnItemLongClickListener(this);

}

@SuppressLint("NewApi")
@Override
public boolean onItemLongClick(AdapterView<?> parent, View view,int position, long id) {

item_position_clicked = position;
android.app.FragmentManager manager = getFragmentManager();
MyDialogFragment dialog = new MyDialogFragment();
dialog.show(manager, "dialog");
return true;
}

@Override
public void message(int data) {

switch (data) {

case 0:
// Show Item Position
ToastMsg("Name :" + item_position_clicked);
break;
case 1:
// Show Item Name
ToastMsg("Name :" + list_items.get(item_position_clicked));
break;
case 2:
// Delete Item
showAlertConfirm();
break;
}
}

void ToastMsg(String msg) {
Toast.makeText(getApplicationContext(), msg, Toast.LENGTH_SHORT).show();
}

void showAlertConfirm() {
final String item_name = list_items.get(item_position_clicked);
AlertDialog alertDialog = new AlertDialog.Builder(this).create();

// Setting Dialog Title
alertDialog.setTitle("Confirm Delete...");

// Setting Dialog Message
alertDialog.setMessage("Are you sure you want delete " + item_name + "?");

// Setting Positive "Yes" Button
alertDialog.setButton(DialogInterface.BUTTON_POSITIVE, "YES", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {

list_items.remove(item_position_clicked);
adapter.notifyDataSetChanged();
ToastMsg(item_name + "Deleted");

}
});

// Setting Negative "Cancel" Button
alertDialog.setButton(DialogInterface.BUTTON_NEGATIVE, "Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});

alertDialog.show();

}
}

2. MyDialogFragment.java

package com.tutorialsee.listviewwithdialog;

import com.tutorialsee.listviewwithdialog.R;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.DialogFragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;

@SuppressLint("NewApi")
public class MyDialogFragment extends DialogFragment implements OnItemClickListener {

ListView mylist;
Communicator communicator;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

View view = inflater.inflate(R.layout.dialog_fragment, null, false);
mylist = (ListView) view.findViewById(R.id.dialog_list);
getDialog().getWindow().requestFeature(Window.FEATURE_NO_TITLE);
return view;
}

@Override
public void onAttach(Activity activity) {

super.onAttach(activity);

if (activity instanceof Communicator) {
communicator = (Communicator) getActivity();
} else {
throw new ClassCastException(activity.toString() + " must implemenet MyListFragment.communicator");
}

}

@Override
public void onActivityCreated(Bundle savedInstanceState) {

super.onActivityCreated(savedInstanceState);

ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
getActivity(), R.array.dialog_items,
android.R.layout.simple_list_item_1);

mylist.setAdapter(adapter);
mylist.setOnItemClickListener(this);

}

public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
dismiss();
communicator.message(position);

}

public interface Communicator {
public void message(int data);
}
}

3. activity_main.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="vertical" >

    <ListView
        android:id="@+id/mylist"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >
    </ListView>

</LinearLayout>

4. dialog_fragment.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_marginLeft="5dp"
    android:layout_marginRight="5dp"
    >

    <ListView
        android:id="@+id/dialog_list"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginLeft="10dp"
        android:layout_marginRight="10dp" >
    </ListView>

</LinearLayout>