Android ListView long press popup menu two implementation examples
- 2020-05-19 05:51:52
- OfStack
/**
* knowledge 1 : ListView item: Two long press popup menus
* knowledge 2 : ListView SimpleAdapter The use of
* knowledge 3 In: java Create in code 1 a ListView
*/
public class ListOnLongClickActivity extends Activity {
private LinearLayout myListViewlayout;
private ListView mListView;
SimpleAdapter adapter;
public int MID;
// create 1 a List Object to hold each of the list items 1 line map information
List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
myListViewlayout = (LinearLayout) findViewById(R.id.myListViewlayout);
mListView = new ListView(this);
// Create layout parameters
LinearLayout.LayoutParams listviewParams = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.FILL_PARENT,
LinearLayout.LayoutParams.FILL_PARENT);
// When sliding on the list, the default is displayed in black
mListView.setCacheColorHint(Color.WHITE);
// Adds the list to the streaming layout myListViewlayout In the
myListViewlayout.addView(mListView, listviewParams);
FillListData();
// The column represents a stand-alone event
mListView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1,
int position, long id) {
/*
* Triggered when a list item is clicked onItemClick Method, 4 The meanings of the parameters are respectively
* arg0 : of the click event AdapterView
* arg1 : AdapterView Is clicked on View
* position : the currently clicked line is in adapter The subscript
* id : the currently clicked row id
*/
Toast.makeText(ListOnLongClickActivity.this,
" You chose yes " + list.get(position).get("name").toString(),
Toast.LENGTH_SHORT).show();
}
});
/**
* Item Long press mode to pop up multiple menu options 1
* Item Long press mode to pop up multiple menu options 2
* ItemOnLongClick1() with ItemOnLongClick2() Do not coexist, according to the actual need to choose
*/
// ItemOnLongClick1();
ItemOnLongClick2();
}
// fill ListView resources
private void FillListData() {
adapter = new SimpleAdapter(ListOnLongClickActivity.this,
getListData(), R.layout.listviewrow, new String[] { "name",
"price" }, new int[] { R.id.tv_name, R.id.tv_price });
mListView.setAdapter(adapter);
}
private List<Map<String, Object>> getListData() {
Map<String, Object> _map = new HashMap<String, Object>();
_map.put("name", " millet ");
_map.put("price", "2350 yuan ");
list.add(_map);
_map = new HashMap<String, Object>();
_map.put("name", "HTC G18");
_map.put("price", "3340 yuan ");
list.add(_map);
_map = new HashMap<String, Object>();
_map.put("name", "iphone 5");
_map.put("price", "5450 yuan ");
list.add(_map);
_map = new HashMap<String, Object>();
_map.put("name", "iPhone 4S");
_map.put("price", "4650 yuan ");
list.add(_map);
_map = new HashMap<String, Object>();
_map.put("name", "MOTO ME525");
_map.put("price", "1345 yuan ");
list.add(_map);
return list;
}
private void ItemOnLongClick1() {
// Note: setOnCreateContextMenuListener With the following onContextItemSelected Matched for use
mListView
.setOnCreateContextMenuListener(new OnCreateContextMenuListener() {
public void onCreateContextMenu(ContextMenu menu, View v,
ContextMenuInfo menuInfo) {
menu.add(0, 0, 0, " buy ");
menu.add(0, 1, 0, " collection ");
menu.add(0, 2, 0, " contrast ");
}
});
}
// Long menu response function
public boolean onContextItemSelected(MenuItem item) {
AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) item
.getMenuInfo();
MID = (int) info.id;// Here, info.id The corresponding is in the database _id The value of the
switch (item.getItemId()) {
case 0:
// Add operation
Toast.makeText(ListOnLongClickActivity.this,
" add ",
Toast.LENGTH_SHORT).show();
break;
case 1:
// Delete operation
break;
case 2:
// delete ALL operation
break;
default:
break;
}
return super.onContextItemSelected(item);
}
private void ItemOnLongClick2() {
mListView.setOnItemLongClickListener(new OnItemLongClickListener() {
@Override
public boolean onItemLongClick(AdapterView<?> arg0, View arg1,
final int arg2, long arg3) {
list.remove(arg2);
new AlertDialog.Builder(ListOnLongClickActivity.this)
.setTitle(" right Item operate ")
.setItems(R.array.arrcontent,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int which) {
String[] PK = getResources()
.getStringArray(
R.array.arrcontent);
Toast.makeText(
ListOnLongClickActivity.this,
PK[which], Toast.LENGTH_LONG)
.show();
if (PK[which].equals(" delete ")) {
// So let's do the delete this way, this if Inside the code has bug , as needed in the actual code
list.remove(arg2);
adapter = (SimpleAdapter) mListView
.getAdapter();
if (!adapter.isEmpty()) {
adapter.notifyDataSetChanged(); // Realize real-time data refresh
}
}
}
})
.setNegativeButton(" cancel ",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int which) {
// TODO Auto-generated method stub
}
}).show();
return true;
}
});
}
}
-----------
listviewrow.xml
Code snippets, <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/listviewbg"
android:orientation="vertical" >
<TextView
android:id="@+id/tv_name"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textColor="@android:color/black" />
<TextView
android:id="@+id/tv_price"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textColor="@android:color/black" />
</LinearLayout>