如何修改simple_list_item_multiple_choice
左上角的图像。请帮我。我试过这个例子。但是有任何可能的方法来使用simple_list_item_multiple_choice
来做同样的事情吗?
请帮我
编辑(必填格式)
试试下面的代码,它可能对你有帮助。
public class ActionModeDemo extends ListActivity {
private static final String[] items=
{ "lorem", "ipsum", "dolor", "sit", "amet", "consectetuer",
"adipiscing", "elit", "morbi", "vel", "ligula", "vitae",
"arcu", "aliquet", "mollis", "etiam", "vel", "erat",
"placerat", "ante", "porttitor", "sodales", "pellentesque",
"augue", "purus" };
private ArrayList<String> words=null;
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
initAdapter();
if (Build.VERSION.SDK_INT>=Build.VERSION_CODES.HONEYCOMB) {
getListView().setChoiceMode(ListView.CHOICE_MODE_MULTIPLE_MODAL);
getListView()
.setMultiChoiceModeListener(new HCMultiChoiceModeListener(this,
getListView()));
}
else {
getListView().setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
registerForContextMenu(getListView());
}
}
@Override
public void onListItemClick(ListView l, View v, int position, long id) {
l.setItemChecked(position, true);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
new MenuInflater(this).inflate(R.menu.option, menu);
EditText add=null;
if (Build.VERSION.SDK_INT>=Build.VERSION_CODES.HONEYCOMB) {
View v=menu.findItem(R.id.add).getActionView();
if (v!=null) {
add=(EditText)v.findViewById(R.id.title);
}
}
if (add!=null) {
add.setOnEditorActionListener(onSearch);
}
return(super.onCreateOptionsMenu(menu));
}
@Override
public void onCreateContextMenu(ContextMenu menu, View v,
ContextMenu.ContextMenuInfo menuInfo) {
new MenuInflater(this).inflate(R.menu.context, menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.add:
add();
return(true);
case R.id.reset:
initAdapter();
return(true);
case R.id.about:
case android.R.id.home:
Toast
.makeText(this, "Action Bar Sample App", Toast.LENGTH_LONG)
.show();
return(true);
}
return(super.onOptionsItemSelected(item));
}
@Override
public boolean onContextItemSelected(MenuItem item) {
boolean result=performActions(item);
if (!result) {
result=super.onContextItemSelected(item);
}
return(result);
}
@SuppressWarnings("unchecked")
public boolean performActions(MenuItem item) {
ArrayAdapter<String> adapter=(ArrayAdapter<String>)getListAdapter();
SparseBooleanArray checked=getListView().getCheckedItemPositions();
switch (item.getItemId()) {
case R.id.cap:
for (int i=0;i<checked.size();i++) {
if (checked.valueAt(i)) {
int position=checked.keyAt(i);
String word=words.get(position);
word=word.toUpperCase();
adapter.remove(words.get(position));
adapter.insert(word, position);
}
}
return(true);
case R.id.remove:
ArrayList<Integer> positions=new ArrayList<Integer>();
for (int i=0;i<checked.size();i++) {
if (checked.valueAt(i)) {
positions.add(checked.keyAt(i));
}
}
Collections.sort(positions, Collections.reverseOrder());
for (int position : positions) {
adapter.remove(words.get(position));
}
getListView().clearChoices();
return(true);
}
return(false);
}
private void initAdapter() {
words=new ArrayList<String>();
for (String s : items) {
words.add(s);
}
setListAdapter(new ArrayAdapter<String>(
this,
android.R.layout.simple_list_item_checked,
words));
}
private void add() {
final View addView=getLayoutInflater().inflate(R.layout.add, null);
new AlertDialog.Builder(this)
.setTitle("Add a Word")
.setView(addView)
.setPositiveButton(
"OK",
new DialogInterface.OnClickListener() {
public void onClick(
DialogInterface dialog,
int whichButton) {
addWord((TextView)addView
.findViewById(R.id.title));
}
})
.setNegativeButton("Cancel", null)
.show();
}
@SuppressWarnings("unchecked")
private void addWord(TextView title) {
ArrayAdapter<String> adapter=(ArrayAdapter<String>)getListAdapter();
adapter.add(title.getText().toString());
}
private TextView.OnEditorActionListener onSearch=
new TextView.OnEditorActionListener() {
public boolean onEditorAction(TextView v, int actionId,
KeyEvent event) {
if (event==null||event.getAction()==KeyEvent.ACTION_UP) {
addWord(v);
InputMethodManager imm=
(InputMethodManager)getSystemService(INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
}
return(true);
}
};
}
public class HCMultiChoiceModeListener implements AbsListView.MultiChoiceModeListener {
ActionModeDemo host;
ActionMode activeMode;
ListView lv;
HCMultiChoiceModeListener(ActionModeDemo host, ListView lv) {
this.host=host;
this.lv=lv;
}
@Override
public boolean onCreateActionMode(ActionMode mode, Menu menu) {
MenuInflater inflater=host.getMenuInflater();
inflater.inflate(R.menu.context, menu);
mode.setTitle(R.string.context_title);
mode.setSubtitle("(1)");
activeMode=mode;
return(true);
}
@Override
public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
return(false);
}
@Override
public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
boolean result=host.performActions(item);
updateSubtitle(activeMode);
return(result);
}
@Override
public void onDestroyActionMode(ActionMode mode) {
activeMode=null;
}
@Override
public void onItemCheckedStateChanged(ActionMode mode, int position,
long id, boolean checked) {
updateSubtitle(mode);
}
private void updateSubtitle(ActionMode mode) {
mode.setSubtitle("("+lv.getCheckedItemCount()+")");
}
}
我猜你想要一个ListView
在ImageView
左角CheckBox
有一个TextView
,每行都有一个。如果是这种情况,那么...不,您不能使用simple_list_item_multiple_choice
. 您将不得不使用 BaseAdapter 来实现这一点。根据您的要求,您可以调整您使用
的示例ImageView
,并将 aCheckBox
和 a添加TextView
到您的 xml 中的每一行,ListView
并使用 a 将它们填充到getView()
适配器的LayoutInflater
试试下面的代码片段。
Activity_name.this.runOnUiThread(new Runnable() {
@Override
public void run() {
list = (ArrayList<Map<String, String>>) result;
this.adapter.setList(list);
this.adapter.notifyDataSetChanged();
}
});