Android - ListView multiple choice only selecting 2 items - android

Anybody know why i'm only able to select 2 items after activating my ListView to Multiple Choice.
listView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
#Override
public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
//Activate CHOICE_MODE_MULTIPLE
multipleActivated = true;
listView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
listView.setItemChecked(position, true);
Log.w("Multiple Selection", "Activated");
return true;
}
});
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
if (multipleActivated == false) {
//Perform normal click and call new intent
.....
} else {
//set the clicked item to be checked since the Multiple Selection is activated
listView.setItemChecked(position, true);
Log.w("Selection", String.valueOf(position));
}
}
});
As you can see in my code, i only want to activate the Multiple Choice Mode once the LongPress is perform, this ok and working fine but im oly able to select 2 items, on the 3rd selection the first selected item just deselecting. i don't know why. anybody can help me here? Thank You!

Related

How to make listview selected one item element?

I am working on the custom Listview using BaseAdapter. I am facing one small issue after tap on listview item that particular row gets highlighted but after that if i'll tap one another list item it's highlighted but still the older one it remains same it's not going to it's previous state.
I want to see one item should select at one time.
MainActivity.java
if (musicRealmResults.get(currentIndex).isSelected() == false) {
musicRealmResults.get(currentIndex).setIsSelected(true);
playSong(currentIndex, true);
adapter.notifyDataSetChanged();
} else {
musicRealmResults.get(currentIndex).setIsSelected(false);
adapter.notifyDataSetChanged();
}
MusicAdapter.java
if (musicRealmResults.get(position).isSelected()) {
Typeface tf = Typeface.createFromAsset(mContext.getAssets(), "fonts/fonts_bold.otf");
holder1.albumsTextView.setTypeface(tf);
holder1.equalizerView.setVisibility(View.VISIBLE);
holder1.albumsImageView.setVisibility(View.INVISIBLE);
holder1.equalizerView.animateBars();
} else {
Typeface tf = Typeface.createFromAsset(mContext.getAssets(), "fonts/fonts_regular.otf");
holder1.albumsTextView.setTypeface(tf);
holder1.equalizerView.setVisibility(View.GONE);
holder1.albumsImageView.setVisibility(View.VISIBLE);
holder1.equalizerView.stopBars();
}
Please kindly go through my post and suggest me how to do select and deselect in a listview row.
Loop through all your array elements and set check state to false, then set currentIndex to true.
MainActivity
for(int i =0 ; i < musicRealmResults.size() ; ++i){
musicRealmResults.get(i).setIsSelected(false);
}
musicRealmResults.get(currentIndex).setIsSelected(true);
playSong(currentIndex, true);
adapter.notifyDataSetChanged();
Seems that you weren't able to setSelected(false) your previous item.
Please check setChoiceMode() of ListView or you may just reset your previous item to setSelected(false).
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
musicRealmResults.get(currentSelected).setIsSelected(false); //Reset previous
currentSelected = position; //Save currentPosition to for reset later
.....todos
}
This is a good alternative too, if u dont want to loop through the rest of positions to deselect them.
int prevSelection =-1; //nothing selected
listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
if (prevSelection==-1) //nothing selected
{
musicRealmResults.get(position).setIsSelected(true);
adapter.notifyDataSetChanged();
prevSelection = position;
}else if (prevSelection == position) //deselect previously selected
{
musicRealmResults.get(position).setIsSelected(false);
adapter.notifyDataSetChanged();
prevSelection = -1;
}else // Some other selection
{
musicRealmResults.get(prevSelection).setIsSelected(false);
musicRealmResults.get(position).setIsSelected(true);
adapter.notifyDataSetChanged();
prevSelection = position;
}
You can manage this in your model class. Just make a toggle Boolean isSelected with its getter and setter, When user tap on list item check whether its already selected or not, if not then mark it as selected and update the Boolean value in your model class.
mListLayout.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(list.get(position).getisSelected()){
// list item is already selected.
mListLayout.setBackgroundColor(Color.WHITE); // normal color
list.get(position).setisSelected(false);
}
else{
// list item is not selected, make it selected
mListLayout.setBackgroundColor(Color.GRAY); // selected color
list.get(position).setisSelected(true);
}
notifyDataSetChanged();
}
});
Try Below Code:
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id)
{
if (musicRealmResults.get(position).isSelected() == false)
{
musicRealmResults.get(position).setIsSelected(true);
playSong(position, true);
adapter.notifyDataSetChanged();
}
else
{
musicRealmResults.get(position).setIsSelected(false);
adapter.notifyDataSetChanged();
}
}
});

How Can I Load the Questions for different subject in 'listview' OnClick List Item

How Can I Load the Questions for different subject in listview item's click. For e.g; if Android is clicked it should load Android MCQ in another Activity
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
T item = parent.getAdapter().getItem(position);
switch (item) { //or if, else
// logic here
}
}
});

Knowing how many times an item is clicked in listview in android

Is there any way to find whether an item in a ListView is clicked for the first time in Android?
Initialize an arraylist with false for how many items in your listView, then try the following
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
if(arrayList.get(position) == false){ //Make sure you initialize your arraylist with false values (same amount as listView size)
arrayList.set(position, true); //set true to an arrayList in the same index of the listView that was pressed
}else{
//Has been clicked before, do something
}
}
});
If you need to, you could include this info to the model wrapper, for example if you display list of Items, so you will have wrapper:
class ItemWrapper {
Item mItem;
boolean mIsClicked;
}
In this case when you click on your item you could get info about if this item was already clicked.
If you need for example list of Checkable items (list of checkboxes) it's the right way to do, because this item is a part of your logic, you could definitely answer if you item is checked.
This will help you.
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int position,
long arg3) {
int count = 0;
try {
count = map.get(your_listview_value);
} catch (Exception e) {
e.printStackTrace();
}
map.put(your_listview_value, (count + 1));
Toast.makeText(getBaseContext(),
String.valueOf(count), Toast.LENGTH_LONG).show();
}
To know about the basic functionality of list view , Check this

Short/Long press in a item of ListView

I have a ListView with some items and I want to have different responses when I make an itemClick (short press) and an itemLongClick (long press):
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//play file
}
});
list.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
#Override
public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
//Message
Toast.makeText(getBaseContext(), "FILE: "+itemsFiles.get(position).getName(), Toast.LENGTH_LONG).show();
return false;
}
});
When I make a short press (onItemClick()) my app works well and it plays the file.
My problem is when I make a long press (onItemLongClick()) because it appears the message with Toast but it plays the file too, and I do not want to play the file in this case ... How I can solve it and correctly distinguish the two cases?
Thanks a lot.
you may want to have look upon this tutorial : http://android.konreu.com/developer-how-to/click-long-press-event-listeners-list-activity/

Disabling items in the list view

I have a requirement wherein I want to disable items in the list view.
Say e.g., I have 5 items in the listview out of which I want to only enable 1 item.
Note: disabling means greying out the item.
Following is my code:
ArrayAdapter<String> adapter = new ArrayAdapter<String>(activity,android.R.layout.simple_list_item_1, movies);
I don't want to go with the custom adapter wherein we get the getView().
Is there any other way to implement this functionality?
In the adapter there is a method name isEnabled which you can override. This is called for each row like getview. The onclicklistener will only fire if this function returns true. So try doing that in your custom adapter.
#Override
public boolean isEnabled(int position) {
if(YOUR CONDTITION){
return false;
}
return true;
}
Without adapter:
Then you need to disable the item by getting view at specific position.
Please implement the listener for this method setOnItemSelectedListener . So you can disable any item that you want.
You can also disable item using:
final Set<Integer> disabledPositions = new HashSet<Integer>();
disabledPositions.add(positionYouWantToDisable);
disabledPositions.add(positionYouWantToDisable);
disabledPositions.add(positionYouWantToDisable);
ListView listView = new ListView(this);
listView.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapter, View view, int position, long arg3) {
if(!disabledPositions.contains(position) {
// do what you want
}
}
});
try this:
final List<Integer> disabledItems = new ArrayList<Integer>();
disabledItems.add(0);
disabledItems.add(2);
lvMovies.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
if (disabledItems.contains(arg2)) {
Toast.makeText(getApplicationContext(), "DISABLED", Toast.LENGTH_SHORT).show();
arg1.setEnabled(false);
} else {
Toast.makeText(getApplicationContext(), "NOT DISABLED", Toast.LENGTH_SHORT).show();
arg1.setEnabled(true);
}
}
});

Categories

Resources