如何在左角创建带有图像的simple_list_item_multiple_choice

时间:2012-07-24 04:33:30

标签: android

如何修改左侧角落的simple_list_item_multiple_choice。请帮我。我试过This例子。但是使用simple_list_item_multiple_choice做同样事情的任何可能的方法?

请帮帮我

编辑(所需格式)

enter image description here

4 个答案:

答案 0 :(得分:4)

你的问题没有准确显示你想要的是什么?请澄清一下,看看这个例子可能对你有帮助。 Custom ListView

multiple selection

答案 1 :(得分:1)

尝试以下代码,它可能会对您有所帮助。

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()+")");
}
}

答案 2 :(得分:1)

我想你想要一个ListView左边有一个ImageViewCheckBox和每行TextView。如果是这种情况,那么......不,你不能使用simple_list_item_multiple_choice。您必须使用BaseAdapter才能实现此目的。 根据您的要求,您可以调整已使用的example并添加 ImageView中每行的CheckBoxTextViewListView xml,并使用getView()在适配器的LayoutInflater中填充它们

答案 3 :(得分:0)

请尝试以下代码段。

Activity_name.this.runOnUiThread(new Runnable() {

  @Override
  public void run() {
  list = (ArrayList<Map<String, String>>) result;
  this.adapter.setList(list);
  this.adapter.notifyDataSetChanged();
}
});