<span style="font-family: Arial, Helvetica, sans-serif;">package com.example.listview;</span>
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import android.os.Bundle;
import android.provider.Contacts.People;
import android.support.v4.widget.SimpleCursorAdapter;
import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.AdapterContextMenuInfo;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends Activity {
private ListView listView;
int ItemSelectedId;
List<String>list=new ArrayList<String>();
String path="/sdcard/wifikey/wifikey.txt";//路径在此设置
@Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final Button bt=(Button)findViewById(R.id.bt);
final Button bt2=(Button)findViewById(R.id.bt2);
final TextView tv = (TextView)findViewById(R.id.tv);
initListView();
listView =(ListView)findViewById(R.id.lv);
//由此网上为ListView的数组内容添加
bt.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View arg0) {
showListView();
}
});
bt2.setOnClickListener(new OnClickListener(){
public void onClick(View v){
Toast.makeText(MainActivity.this, "开发中……", Toast.LENGTH_SHORT).show();
}
});
registerForContextMenu(listView);
listView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
@Override
public boolean onItemLongClick(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
ItemSelectedId=arg2;
return false;
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public void onCreateContextMenu(ContextMenu menu, View v,
ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
MenuInflater inflater=getMenuInflater();
inflater.inflate(R.menu.menu, menu);
}
@Override
public boolean onContextItemSelected(MenuItem item) {
switch(item.getItemId()){
case R.id.deleteTeam:
removeItem(ItemSelectedId);
showListView();
Toast.makeText(MainActivity.this, "删除"+ItemSelectedId, Toast.LENGTH_SHORT).show();
// listView.setAdapter(adapter);
break;
case R.id.noteTeam:
Toast.makeText(MainActivity.this, "备注", Toast.LENGTH_SHORT).show();
return true;
}
return false;
}
public void removeItem(int i){
list.remove(i);
showListView();
}
public void showListView(){
ArrayAdapter<String>adapter=new ArrayAdapter<String>(this,
R.layout.list,list);
listView.setAdapter(adapter);
}
public void initListView(){
list.add("one");
list.add("two");
//去掉android
}
}
<span style="font-size:24px;color:#ff0000;">今天通过写这个demo,又学到很多东西。</span>
<span style="font-size:24px;color:#ff0000;">例如变量,函数名起名非常重要。</span>
<span style="font-size:24px;color:#ff0000;">然后通过onItemLongClickListener()里面的第二个参数传给全局变量ItemSelectedId</span>
<span style="font-size:24px;color:#ff0000;">再通过contextmenu操作list数组的内容,实现ListView的动态管理。</span>