Android实验3:学习使用ListView

学习使用ListView

1.  实现一个列表,其中显示班级学号姓名,提供添加功能,如需要删去某一项,长按该项,通过弹出菜单显示删除功能。


MainActivity.java

import android.app.Activity;
import android.os.Bundle;
import android.widget.AdapterView;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.EditText;
import android.widget.Button;
import android.widget.PopupMenu;
import android.view.MenuItem;
import java.util.ArrayList;
import java.util.List;

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        final Button bt1=(Button)findViewById(R.id.bt1);
        final ListView lv1=(ListView)findViewById(R.id.lv1);

        List list=new ArrayList();
        list.add("班级"+"                   "+"学号"+"                   "+"姓名");
        ArrayAdapter adapter=new ArrayAdapter(this,android.R.layout.simple_list_item_1,list);
        lv1.setAdapter(adapter);

        bt1.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v)
            {
                EditText et_c,et_a,et_n;
                et_c=(EditText)findViewById(R.id.et_c);
                et_a=(EditText)findViewById(R.id.et_a);
                et_n=(EditText)findViewById(R.id.et_n);
                ArrayAdapter temp_adp=(ArrayAdapter) lv1.getAdapter();
                temp_adp.add(et_c.getText().toString()+"   "+et_a.getText().toString()+"     "+et_n.getText().toString());
            }
        });

        lv1.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener(){
            @Override
            public boolean onItemLongClick(AdapterView parent, View view, final int position, long id) {
                if (id > 0)
                {
                    PopupMenu popup = new PopupMenu(MainActivity.this, view);
                    popup.getMenuInflater().inflate(R.menu.simple_menu1, popup.getMenu());
                    popup.show();
                    popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
                        @Override
                        public boolean onMenuItemClick(MenuItem item) {
                            switch (item.getItemId()) {
                                case R.id.menu1:
                                ArrayAdapter temp_adp = (ArrayAdapter) lv1.getAdapter();
                                    temp_adp.remove(temp_adp.getItem(position));
                                    return true;
                                default:
                                    return false;
                            }
                        }
                    });
                }
        return true;
        }
        });
    }
}


activity_main.xml



    

        

            

            
        

        

            

            
        

        

            

            
        

        

simple_main1.xml


    
    


你可能感兴趣的:(Android实验3:学习使用ListView)