Android dialog listview for multi selection

 

We are not going to use any thing from xml file.

package com.pr.testtwo;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.res.TypedArray;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.Toast;

public class TestTwoActivity extends Activity {
/** Called when the activity is first created. */

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

final CharSequence[] digitList= { “One”, “Two”, “Three” };
AlertDialog.Builder alt_bld = new AlertDialog.Builder(this);
alt_bld.setIcon(R.drawable.icon);
alt_bld.setTitle(“Select one Digit”);

alt_bld.setMultiChoiceItems(digitList, new boolean[] { false, true,
false },
new DialogInterface.OnMultiChoiceClickListener() {
public void onClick(DialogInterface dialog,
int whichButton, boolean isChecked) {

}
});
alt_bld.setPositiveButton(“OK”, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {

ListView list = ((AlertDialog)dialog).getListView();

StringBuilder sb = new StringBuilder();
for (int i=0; i<list.getCount(); i++) {
boolean checked = list.isItemChecked(i);

if (checked) {
if (sb.length()>0) sb.append(“, “);
sb.append(list.getItemAtPosition(i));
}
}

Toast.makeText(getApplicationContext(),
“Selected digit: ” + sb.toString(),
Toast.LENGTH_SHORT).show();

}
});
alt_bld.setNegativeButton(“Cancel”,
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {

}
});

AlertDialog alert = alt_bld.create();
alert.show();

}

}

Advertisements

Leave a comment

Filed under Uncategorized

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s