Android Listview對話方塊

來源:互聯網
上載者: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", "內容"+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; 
    } 
 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.