如何在此脚本中的列表视图上进行长按事件后创建上下文菜单?
我需要添加一个OnLongClickListener
,并在listView中的其中一个项目上长按一下,显示一个上下文类型菜单。
public class MainActivity extends ListActivity {
ArrayList<HashMap<String, String>> contactList;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
contactList = new ArrayList<>();
ListView lv = getListView();
// Listview on item click listener
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// getting values from selected ListItem
String nomer = ((TextView) view.findViewById(R.id.nomer))
.getText().toString();
Intent callIntent = new Intent(Intent.ACTION_CALL, Uri.parse("tel:" + nomer));
if (ActivityCompat.checkSelfPermission(MainActivity.this,
Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED) {
return;
}
startActivity(callIntent);
}
});
}
class GetContacts extends AsyncTask<Void, Void, Void> {
@Override
protected void onPreExecute() {
super.onPreExecute();
// Showing progress dialog
pDialog = new ProgressDialog(MainActivity.this);
pDialog.setMessage("Please wait...");
pDialog.setCancelable(false);
pDialog.show();
}
@Override
protected Void doInBackground(Void... arg0) {
// Creating service handler class instance
ServiceHandler sh = new ServiceHandler();
// Making a request to url and getting response
String jsonStr = sh.makeServiceCall(url.get(), ServiceHandler.GET);
Log.d("Response: ", "> " + jsonStr);
if (jsonStr != null) {
try {
JSONObject jsonObj = new JSONObject(jsonStr);
// Getting JSON Array node
contacts = jsonObj.getJSONArray(TAG_CONTACTS);
// looping through All Contacts
for (int i = 0; i < contacts.length(); i++) {
JSONObject c = contacts.getJSONObject(i);
String id = c.getString(TAG_ID);
String name = c.getString(TAG_NAME);
String nomer = c.getString(TAG_NOMER);
String date = c.getString(TAG_DATE);
String url = c.getString(TAG_URL);
// tmp hashmap for single contact
HashMap<String, String> contact = new HashMap<>();
// adding each child node to HashMap key => value
contact.put(TAG_NAME, name);
contact.put(TAG_NOMER, nomer);
contact.put(TAG_URL, url);
contact.put(TAG_DATE, date);
// adding contact to contact list
contactList.add(contact);
}
} catch (JSONException e) {
e.printStackTrace();
}
} else {
Log.e("ServiceHandler", "Couldn't get any data from the url");
}
return null;
}
@Override
protected void onPostExecute(Void result) {
super.onPostExecute(result);
// Dismiss the progress dialog
if (pDialog.isShowing())
pDialog.dismiss();
/**
* Updating parsed JSON data into ListView
* */
ListAdapter adapter = new SimpleAdapter(
MainActivity.this, contactList,
R.layout.list_item, new String[]{TAG_NAME, TAG_NOMER, TAG_DATE,
TAG_URL}, new int[]{R.id.name, R.id.nomer,
R.id.date });
setListAdapter(adapter);
}
}
答案 0 :(得分:0)
在ListView中注册forcontext菜单:
registerForContextMenu(lv);
添加重写方法:
@Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
if (v.getId()==R.id.list_view) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu_list, menu);
}
}
@Override
public boolean onContextItemSelected(MenuItem item) {
AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();
switch(item.getItemId()) {
case R.id.add:
// add stuff here
return true;
case R.id.edit:
// edit stuff here
return true;
case R.id.delete:
// remove stuff here
return true;
default:
return super.onContextItemSelected(item);
}
}
最后,添加如下所示的菜单文件(根据您的要求自定义)
<?xml version="1.0" encoding="utf-8"?>
<menu
xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@+id/add"
android:icon="@android:drawable/ic_menu_add"
android:title="@string/menu_add" />
<item android:id="@+id/edit"
android:icon="@android:drawable/ic_menu_edit"
android:title="@string/menu_edit" />
<item android:id="@+id/delete"
android:icon="@android:drawable/my_icon_delete"
android:title="@string/menu_delete" />
</menu>
res / menu文件夹中的