Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> Android Listview對話框

Android Listview對話框

編輯:關於Android編程

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", "內容"+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("修改"); 
                alertDialog.setIcon(android.R.drawable.ic_dialog_info); 
                alertDialog.setView(lst_category); 
                alertDialog.setButton(Dialog.BUTTON_POSITIVE, "正確", 
                        new DialogInterface.OnClickListener() { 
 
                            @Override 
                            public void onClick(DialogInterface dialog, 
                                    int which) { 
                                // TODO Auto-generated method stub  
 
                            } 
                        }); 
                alertDialog.setButton(Dialog.BUTTON_NEGATIVE, "取消", 
                        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", "內容"+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("修改");
    alertDialog.setIcon(android.R.drawable.ic_dialog_info);
    alertDialog.setView(lst_category);
    alertDialog.setButton(Dialog.BUTTON_POSITIVE, "正確",
      new DialogInterface.OnClickListener() {

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

       }
      });
    alertDialog.setButton(Dialog.BUTTON_NEGATIVE, "取消",
      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.txtV_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.txtV_category.setText((String) listItems.get(position) 
                .get("Name")); 
        listItemView.ckb_category.setTag((String) listItems.get(position).get( 
                "ID")); 
        return convertView; 
    } 
 

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved