0

how can i select an item in a ListView after i start an Custom Actionbar.

public class UserListActivityAdapter extends BaseAdapter {

String[] mNameList;
int[] imageID;
Context mContext;
LayoutInflater inflater;
boolean isLongPressed = false;

public UserListActivityAdapter(UserListAcitivty context,
        String[] nameList,
        int[] imageList) {

    this.mContext = context;
    this.imageID = imageList;
    this.mNameList = nameList;
    inflater = LayoutInflater.from(context);

}

@Override
public int getCount() {
    return mNameList.length;
}

@Override
public Object getItem(int position) {
    return position;
}

@Override
public long getItemId(int position) {
    return position;
}

public static class Holder {
    TextView tv;
    TextView desc;
    ImageView img;
}

@Override
public View getView(int position,
        View convertView,
        ViewGroup parent) {

    Holder holder = new Holder();
    View rowView;

    rowView = inflater.inflate(R.layout.row_layout_user, parent,    false);
    holder.tv = (TextView) rowView.findViewById(R.id.desc);
    holder.desc = (TextView) rowView.findViewById(R.id.name);
    holder.img = (ImageView) rowView.findViewById(R.id.icon);
    holder.desc.setText(mNameList[position]);
    holder.tv.setText("Blub");
    holder.img.setImageResource(imageID[position]);
    return rowView;
}

When i click on an item it change the background but when i start the actionbar it dont change anymore.

public class UserListAcitivty extends Activity implements
    MultiChoiceModeListener  {
private ListView mListView;
String[] list;
private int count;
UserListActivityAdapter user;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    list = new String[] { "Hans Schmidt", "Felix DieKatze", "Wolfgang  Müller" };
    int[] img = new int[] { R.drawable.ic_launcher,   R.drawable.abs__ab_bottom_solid_dark_holo, R.drawable.icon };
    setContentView(R.layout.userlistactivity_layout);

    mListView = (ListView) findViewById(R.id.userList);
    mListView.setEmptyView(findViewById(R.id.empty_userlist));
    user = new UserListActivityAdapter(this, list, img);
    mListView.setAdapter(user);
    mListView.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
    mListView.setMultiChoiceModeListener(this);
}


        @Override
        public boolean onPrepareActionMode(ActionMode mode,
                Menu menu) {

            return false;
        }

        @Override
        public void onDestroyActionMode(ActionMode mode) {

        }

        @Override
        public boolean onCreateActionMode(ActionMode mode,
                Menu menu) {
            MenuInflater inflater = mode.getMenuInflater();
            inflater.inflate(R.menu.my_context_menu, menu);
            return true;
        }

        @Override
        public boolean onActionItemClicked(ActionMode mode,
                MenuItem item) {
            return false;
        }

        @Override
        public void onItemCheckedStateChanged(ActionMode mode,
                int position,
                long id,
                boolean checked) {
            count = count + 1;
            mode.setTitle(count + " User selected");
    mListView.setItemChecked(position, checked);

        }
}

This is the selector for the view

<selector xmlns:android="http://schemas.android.com/apk/res/android" >
     <!-- Normal state. -->
<item android:drawable="@color/list_row_default_bg" 
    android:state_pressed="false" 
    android:state_selected="false"/>
<!-- pressed state. -->
<item android:drawable="@color/list_row_pressed_bg" 
    android:state_pressed="true"/>
<!-- Selected state. -->
<item android:drawable="@color/list_row_selected_bg" 
    android:state_selected="true"/>    

Elltz
  • 10,730
  • 4
  • 31
  • 59
Byakko
  • 21
  • 1
  • 3
  • I don't really understand what the problem is, where do you define your action bar actions? – Avrham Aton Apr 07 '15 at 16:51
  • The Actionbar appears when i presslong on a Item. mListView.setMultiChoiceModeListener(this); – Byakko Apr 07 '15 at 16:56
  • This might help you http://stackoverflow.com/questions/9754170/listview-selection-remains-persistent-after-exiting-choice-mode that is if I understood you correctly – Avrham Aton Apr 07 '15 at 17:11
  • Thanks, but this isnt what i search for. I have a Listview with same names. When i press an Item long the Actionbar should appear that works. In the ActionMode want now that i can selected some names so i can highlight the item. Like the gmail app select mutliple items to delete. – Byakko Apr 07 '15 at 17:16
  • are you saying when you just click it changes background, but when you long click it it doesnt? we have `onLongClickListener` that listens to long clicks and `onClickListener` that listens to just clicks, if you click it `onClick` is called, and that's what by default the selector you created in your xml does, that is why when you click it changes background,.. **BUT** not `onLongClick`.. (im unsure here going ->) you need to change the background, in maybe your `onItemCheckedStateChanged` hope it helps... – Elltz Apr 07 '15 at 18:07
  • Thanks for your answer.My problem is that i cant change a item to status "Selected" so that i can use my selector. I try it like you said in the onItemcheckedStateChange but it dont work – Byakko Apr 07 '15 at 21:54

0 Answers0