Android Listview dialog box

Source: Internet
Author: User

MainActivity. java

[Java]
Package com. example. test;
 
Import java. util. ArrayList;
Import java. util. HashMap;
Import java. util. List;
Import java. util. Map;
 
Import android. OS. Bundle;
Import android. app. Activity;
Import android. app. AlertDialog;
Import android. app. Dialog;
Import android. content. DialogInterface;
Import android. view. Menu;
Import android. view. View;
Import android. view. WindowManager;
Import android. widget. Button;
Import android. widget. ListView;
 
Public class MainActivity extends Activity {
 
@ Override
Protected void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. activity_main );

Button btn = (Button) findViewById (R. id. button1 );
Btn. setOnClickListener (new View. OnClickListener (){

@ Override
Public void onClick (View v ){
// TODO Auto-generated method stub
List <Map <String, Object> lst_data = new ArrayList <Map <String, Object> ();
 
For (int I = 0; I <20; I ++ ){
Map <String, Object> map = new HashMap <String, Object> ();
Map. put ("Name", "content" + I );
Lst_data.add (map );
}


ListView lst_category = new ListView (MainActivity. this );

DeleteCategoryAdapter adapter = new DeleteCategoryAdapter (
MainActivity. this, lst_data );
Lst_category.setAdapter (adapter );
 
AlertDialog alertDialog = new AlertDialog. Builder (
MainActivity. this). create ();
AlertDialog. setTitle ("modify ");
AlertDialog. setIcon (android. R. drawable. ic_dialog_info );
AlertDialog. setView (lst_category );
AlertDialog. setButton (Dialog. BUTTON_POSITIVE, "correct ",
New DialogInterface. OnClickListener (){
 
@ Override
Public void onClick (DialogInterface dialog,
Int which ){
// TODO Auto-generated method stub
 
}
});
AlertDialog. setButton (Dialog. BUTTON_NEGATIVE, "cancel ",
New DialogInterface. OnClickListener (){
 
@ Override
Public void onClick (DialogInterface dialog,
Int which ){
// TODO Auto-generated method stub
 
}
});
AlertDialog. show ();
AlertDialog. getWindow (). clearFlags (
WindowManager. LayoutParams. FLAG_ALT_FOCUSABLE_IM );
}
});
}
 
@ Override
Public boolean onCreateOptionsMenu (Menu menu ){
// Inflate the menu; this adds items to the action bar if it is present.
GetMenuInflater (). inflate (R. menu. activity_main, menu );
Return true;
}
 
}

Package com. example. test;

Import java. util. ArrayList;
Import java. util. HashMap;
Import java. util. List;
Import java. util. Map;

Import android. OS. Bundle;
Import android. app. Activity;
Import android. app. AlertDialog;
Import android. app. Dialog;
Import android. content. DialogInterface;
Import android. view. Menu;
Import android. view. View;
Import android. view. WindowManager;
Import android. widget. Button;
Import android. widget. ListView;

Public class MainActivity extends Activity {

@ Override
Protected void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. activity_main );

Button btn = (Button) findViewById (R. id. button1 );
Btn. setOnClickListener (new View. OnClickListener (){

@ Override
Public void onClick (View v ){
// TODO Auto-generated method stub
List <Map <String, Object> lst_data = new ArrayList <Map <String, Object> ();

For (int I = 0; I <20; I ++ ){
Map <String, Object> map = new HashMap <String, Object> ();
Map. put ("Name", "content" + I );
Lst_data.add (map );
}


ListView lst_category = new ListView (MainActivity. this );

DeleteCategoryAdapter adapter = new DeleteCategoryAdapter (
MainActivity. this, lst_data );
Lst_category.setAdapter (adapter );

AlertDialog alertDialog = new AlertDialog. Builder (
MainActivity. this). create ();
AlertDialog. setTitle ("modify ");
AlertDialog. setIcon (android. R. drawable. ic_dialog_info );
AlertDialog. setView (lst_category );
AlertDialog. setButton (Dialog. BUTTON_POSITIVE, "correct ",
New DialogInterface. OnClickListener (){

@ Override
Public void onClick (DialogInterface dialog,
Int which ){
// TODO Auto-generated method stub

}
});
AlertDialog. setButton (Dialog. BUTTON_NEGATIVE, "cancel ",
New DialogInterface. OnClickListener (){

@ Override
Public void onClick (DialogInterface dialog,
Int which ){
// TODO Auto-generated method stub

}
});
AlertDialog. show ();
AlertDialog. getWindow (). clearFlags (
WindowManager. LayoutParams. FLAG_ALT_FOCUSABLE_IM );
}
});
}

@ Override
Public boolean onCreateOptionsMenu (Menu menu ){
// Inflate the menu; this adds items to the action bar if it is present.
GetMenuInflater (). inflate (R. menu. activity_main, menu );
Return true;
}

}

DeleteCategoryAdapter. java

[Java]
Package com. example. test;
 
Import java. util. List;
Import java. util. Map;
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. EditText;
Import android. widget. TextView;
 
Public class DeleteCategoryAdapter extends BaseAdapter {
 
Private Context context;
Private List <Map <String, Object> listItems;
Private LayoutInflater listContainer;
 
Public DeleteCategoryAdapter (Context context,
List <Map <String, Object> listItems ){
This. context = context;
// Create view and settings context
ListContainer = LayoutInflater. from (context );
This. listItems = listItems;
}
 
// Custom widget
Public final class ListItemView {
Public TextView txtV_category;
Public CheckBox ckb_category;
}
 
@ Override
Public int getCount (){
// TODO Auto-generated method stub
Return listItems. size ();
}
 
@ Override
Public Object getItem (int position ){
// TODO Auto-generated method stub
If (position <listItems. size ()){
Return listItems. get (position );
}
Return null;
}
 
@ Override
Public long getItemId (int position ){
// TODO Auto-generated method stub
Return position;
}
 
@ Override
Public View getView (int position, View convertView, ViewGroup parent ){
// TODO Auto-generated method stub
ListItemView listItemView = new ListItemView ();
If (convertView = null ){
ConvertView = listContainer. inflate (R. layout. show_deletecategory,
Null );
ListItemView.txt V_category = (TextView) convertView
. FindViewById (R. id. deletecategory_txtV_category );
ListItemView. ckb_category = (CheckBox) convertView
. FindViewById (R. id. deletecategory_ckb_select );
 
ConvertView. setTag (listItemView );
} Else {
ListItemView = (ListItemView) convertView. getTag ();
}
ListItemView.txt V_category.setText (String) listItems. get (position)
. Get ("Name "));
ListItemView. ckb_category.setTag (String) listItems. get (position). get (
"ID "));
Return convertView;
}
 
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.