在列表视图中多选时我有 2 个工具栏
Posted
技术标签:
【中文标题】在列表视图中多选时我有 2 个工具栏【英文标题】:i got 2 toolbar when multi select in listview 【发布时间】:2016-04-23 00:14:00 【问题描述】:大家好,我的应用 SMS 出现问题,当我长按某个联系人时,它会显示新工具栏,但我不知道要删除另一个工具栏:/
如果你能帮助我,谢谢..
这是我的主要活动:
package com.ducha.messavtext;
import android.annotation.TargetApi;
import android.content.ContentUris;
import android.content.ContentValues;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.provider.BaseColumns;
import android.provider.ContactsContract;
import android.provider.Telephony;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.util.SparseBooleanArray;
import android.view.ActionMode;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AbsListView;
import android.widget.AdapterView;
import android.widget.ListView;
import com.ducha.training3.R;
import java.util.ArrayList;
@TargetApi(Build.VERSION_CODES.KITKAT)
public class MainActivity extends AppCompatActivity implements BaseColumns
private Cursor cursor;
private Cursor cursorPhone;
private Cursor cursorContact;
private final Uri uriSms = Telephony.Sms.CONTENT_URI;
private final Uri uriPhone = ContactsContract.CommonDataKinds.Phone.CONTENT_URI;
private final Uri uri = Uri.parse("content://sms/inbox");
private String body;
private String numero;
private String id;
private String contact;
private final String[] selectionSms = new String[]Telephony.Sms._ID, Telephony.Sms.BODY, Telephony.Sms.THREAD_ID, Telephony.Sms.ADDRESS, Telephony.Sms.DATE, Telephony.Sms.READ,;
private final String[] selectionPhone = new String[]ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME, ContactsContract.CommonDataKinds.Phone.NUMBER,;
private final String[] selectionContact = new String[]ContactsContract.Contacts._ID, ContactsContract.Contacts.DISPLAY_NAME,;
public static ArrayList<CreateContactAdapter> listContact = new ArrayList<>();;
public static boolean runMain;
public static ContactAdapter adapter;
public static byte[] myPhoto;
private ArrayList<String> idList = new ArrayList<>();;
private Context ctx = this;
private int read;
private int messageUnread = 0;
private ListView lv;
private ArrayList<String> diffNumber;
private byte[] photo;
private Toolbar toolbar;
@Override
protected void onResume()
super.onResume();
runMain = true;
@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
clearIfNotEmpty();
isTheDefaultApp();
findId();
getInfoMain();
getMyContactPicture();
ConfigToolbar();
lv.setAdapter(adapter);
lv.setChoiceMode(AbsListView.CHOICE_MODE_MULTIPLE_MODAL);
lv.setMultiChoiceModeListener(new AbsListView.MultiChoiceModeListener()
@Override
public void onItemCheckedStateChanged(ActionMode mode, int position, long id, boolean checked)
final int selectedItem = lv.getCheckedItemCount();
mode.setTitle(selectedItem + " Item selected");
@Override
public boolean onCreateActionMode(ActionMode mode, Menu menu)
mode.getMenuInflater().inflate(R.menu.select_menu, menu);
return true;
@Override
public boolean onPrepareActionMode(ActionMode mode, Menu menu)
return false;
@Override
public boolean onActionItemClicked(ActionMode arg0, MenuItem item)
switch (item.getItemId())
case R.id.remove_item:
SparseBooleanArray select = adapter.getSelectedIds();
for (int i = (select.size() - 1); i >= 0; i--)
if (select.valueAt(i))
CreateContactAdapter contact = (CreateContactAdapter) adapter.getItem(select.keyAt(i));
adapter.remove(contact);
arg0.finish();
return true;
default:
return false;
@Override
public void onDestroyActionMode(ActionMode mode)
adapter.removeSelection();
);
lv.setOnItemClickListener(new AdapterView.OnItemClickListener()
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id)
Intent intent = new Intent(MainActivity.this, PrintMessage.class);
String persoId = listContact.get(position).id;
String numero = listContact.get(position).numero;
String contact = listContact.get(position).contact;
String body = listContact.get(position).body;
byte[] bmp = listContact.get(position).photo;
intent.putExtra("id", persoId);
intent.putExtra("numero", numero);
intent.putExtra("contact", contact);
intent.putExtra("photo", bmp);
markMessageRead(ctx, numero, body);
startActivity(intent);
);
private void clearIfNotEmpty()
if (!listContact.isEmpty())
listContact.clear();
private void getMyContactPicture()
Uri uriContact = Uri.withAppendedPath(ContactsContract.PhoneLookup.CONTENT_FILTER_URI, Uri.encode("0659641613"));
cursorContact = this.getContentResolver().query(uriContact, selectionContact, null, null, null);
cursorContact.moveToFirst();
long idContact = cursorContact.getLong(0);
myPhoto = openPhoto(idContact);
@TargetApi(Build.VERSION_CODES.KITKAT)
private void isTheDefaultApp()
final String packageName = MainActivity.this.getPackageName();
if (!Telephony.Sms.getDefaultSmsPackage(MainActivity.this).equals(packageName))
Intent intent = new Intent(Telephony.Sms.Intents.ACTION_CHANGE_DEFAULT);
intent.putExtra(Telephony.Sms.Intents.EXTRA_PACKAGE_NAME, packageName);
startActivity(intent);
private void findId()
toolbar = (Toolbar) findViewById(R.id.my_toolbar);
lv = (ListView) findViewById(R.id.ListView01);
@TargetApi(Build.VERSION_CODES.KITKAT)
public void getInfoMain()
cursor = this.getContentResolver().query(uriSms, selectionSms, null, null, "date DESC");
if (cursor.moveToFirst())
while (!cursor.isAfterLast())
id = cursor.getString(2);
if (!(idList.contains(id)))
body = cursor.getString(1);
numero = cursor.getString(3);
read = cursor.getInt(5);
if (read == 0)
messageUnread++;
diffNumber = getDifferentNumber(numero);
cursorPhone = this.getContentResolver().query(uriPhone, selectionPhone,
ContactsContract.CommonDataKinds.Phone.NUMBER + "='" + diffNumber.get(0) +"' OR " +
ContactsContract.CommonDataKinds.Phone.NUMBER + "='" + diffNumber.get(1) + "' OR " +
ContactsContract.CommonDataKinds.Phone.NUMBER + "='" + diffNumber.get(2) + "' OR " +
ContactsContract.CommonDataKinds.Phone.NUMBER + "='" + diffNumber.get(3) + "'", null, null);
if (cursorPhone.moveToFirst())
contact = cursorPhone.getString(0);
Uri uriContact = Uri.withAppendedPath(ContactsContract.PhoneLookup.CONTENT_FILTER_URI, Uri.encode(numero));
cursorContact = this.getContentResolver().query(uriContact, selectionContact, null, null, null);
cursorContact.moveToFirst();
long idContact = cursorContact.getLong(0);
photo = openPhoto(idContact);
if (photo != null)
listContact.add(new CreateContactAdapter(contact, numero, body, id, photo, read));
else
listContact.add(new CreateContactAdapter(contact, numero, body, id, null, read));
else
listContact.add(new CreateContactAdapter("no", numero, body, id, null, read));
idList.add(id);
cursor.moveToNext();
cursor.close();
adapter = new ContactAdapter(listContact, this);
private void ConfigToolbar()
setSupportActionBar(toolbar);
if (messageUnread > 0)
getSupportActionBar().setTitle("Messages (" + messageUnread + ")");
else
getSupportActionBar().setTitle("Messages");
public byte[] openPhoto(long contactId)
Uri contactUri = ContentUris.withAppendedId(ContactsContract.Contacts.CONTENT_URI, contactId);
Uri photoUri = Uri.withAppendedPath(contactUri, ContactsContract.Contacts.Photo.CONTENT_DIRECTORY);
Cursor cursor = getContentResolver().query(photoUri, new String[]ContactsContract.Contacts.Photo.PHOTO, null, null, null);
if (cursor == null)
return null;
try
if (cursor.moveToFirst())
byte[] data = cursor.getBlob(0);
if (data != null)
return data;
finally
cursor.close();
return null;
private void markMessageRead(Context context, String number, String body)
Cursor cursor = context.getContentResolver().query(uri, null, null, null, null);
try
while (cursor.moveToNext())
if ((cursor.getString(cursor.getColumnIndex("address")).equals(number)) && (cursor.getInt(cursor.getColumnIndex("read")) == 0))
if (cursor.getString(cursor.getColumnIndex("body")).startsWith(body))
String SmsMessageId = cursor.getString(cursor.getColumnIndex("_id"));
ContentValues values = new ContentValues();
values.put("read", true);
context.getContentResolver().update(Uri.parse("content://sms/inbox"), values, "_id=" + SmsMessageId, null);
return;
catch (Exception e)
Log.e("Mark Read", "Error in Read: " + e.toString());
public ArrayList<String> getDifferentNumber(String number)
ArrayList<String> num = new ArrayList<>();
if (number.matches("^[06|07]2[0-9]8"))
num.add(number.replaceAll(".2", "$0 "));
num.add(number.replaceAll("06", "+336"));
num.add(number.replaceAll(".2", "$0 ").replaceAll("0", "+33 "));
num.add(number);
else if (number.matches("[06|07]2( [0-9]2)4"))
num.add(number.replaceAll(" ", ""));
num.add(number.replaceAll("0", "+33 "));
num.add(number.replaceAll(" ", "").replaceAll("0", "+33"));
num.add(number);
else if (number.matches("\\+[3]2[0-9]8."))
num.add(number.replaceAll("\\+33", "0"));
num.add(number.replaceAll("\\+33", "0").replaceAll(".2", "$0 "));
num.add(number.replaceAll("\\+33", "0").replaceAll(".2", "$0 ").replaceAll("^0", "+33 "));
num.add(number);
else if (number.matches("\\+33 [6|7]( [0-9]2)4"))
num.add(number.replaceAll(" ", ""));
num.add(number.replaceAll(" ", "").replaceAll("\\+33", "0"));
num.add(number.replaceAll(" ", "").replaceAll("\\+33", "0").replaceAll(".2", "$0 "));
num.add(number);
else
num.add(numero);
num.add(numero);
num.add(numero);
num.add(numero);
return num;
public boolean edit(MenuItem item)
// actions
return true;
public boolean newMessage(MenuItem item)
startActivity(new Intent(this, NewMessage.class));
return true;
@Override
public boolean onCreateOptionsMenu(Menu menu)
getMenuInflater().inflate(R.menu.main_menu, menu);
return true;
@Override
public boolean onOptionsItemSelected(MenuItem item)
switch (item.getItemId())
case R.id.setting :
startActivity(new Intent(this, Apparence.class));
return true;
@Override
protected void onPause()
super.onPause();
runMain = false;
【问题讨论】:
【参考方案1】:尝试将以下内容添加到您的 styles.xml:
<style name="MyAppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<item name="colorPrimary">@color/my_primary_color</item>
<item name="colorPrimaryDark">@color/my_primary_dark_color</item>
<item name="colorAccent">@color/my_accent_color</item>
<item name="windowActionModeOverlay">true</item>
</style>
【讨论】:
我在我的菜单中添加了它<item android:name="android:windowActionModeOverlay">true</item>
你在你的答案中错过了“andrdoid”,但它不起作用
我的意思是我的 menu.xml。这次我将它添加到我的 style.xml 中,它也不起作用
好吧 .. 我在 style.xml 和我的工具栏中的“activity_main.xml”中为我的“custom_toolbar”找到了一个样式,我添加了android:theme="@style/custom_main"
然后我添加了你的行,但它不起作用,然后我在“AppTheme”(默认样式)中添加了您的行并删除了android:theme="@style/custom_main"
行,它就可以工作了!但我不知道为什么:/你能解释一下吗?你想要我的 style.xml 吗?以上是关于在列表视图中多选时我有 2 个工具栏的主要内容,如果未能解决你的问题,请参考以下文章
jquery easyui combobox多选时,选第一个,前面有分隔符
CheckedComboBoxEdit 多选时,值与值之间会多出一个空格
(this.internalValue || []).findIndex 不是在 v-select 上启用多选时的函数