Friday, August 31, 2012

Simple ListView 2 - with Dialog (android)


public class HelloListViewActivity extends ListActivity {

    String[] data;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        // storing string resources into Array
        data = getResources().getStringArray(R.array.data);
        // Binding resources Array to ListAdapter
        this.setListAdapter(new ArrayAdapter<String>(this, R.layout.list_cell, R.id.label, data));

        ListView lv = getListView();
        lv.setOnItemClickListener(new OnItemClickListener() {
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                createDialog(data[position]);
            }
        });
    }

    private void createDialog(String s)
    {
        new AlertDialog.Builder(this)
        .setTitle("Message")
        .setMessage("Go to " + s + " page ?")
        .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {

            }
        })
        .setNegativeButton("No", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {

            }
        })
        .show();    
    }
}


No comments:

Post a Comment